Replacing Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Replacing Problem

Post by Trahb »

I'm working on a project and basing the design on a template. The file is read and the tags designated by {}'s are replaced with files including what i want to be shown.

If any php is in the files replacing the tags, it either has no effect at all or is completely echoed out.
Any ideas as to what may be happening? And if you feel you may have a solution but will need to see the files I can show ya.

Thanks in advance.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Replacing Problem

Post by Trahb »

Bump.. I really need help with this

Code: Select all

<?php
if (basename($_SERVER["PHP_SELF"]) == "style.obj.php")
{
    die("The page you are attempting to view is private.");
}
require_once('class/obj/template.obj.php');
class TJJStyle 
{
    public function setTitle($page) {
        return $page;
    }
    
    public function showPage($page) {
        $tpl = new TJJ_Template();
        switch ($page) {
            case "home":
                $tpl->replaceTags(array(
                "title" => $this->setTitle('Home'),
                "nav" => "sources/nav.php",
                "content" => "sources/main.php"
                ));
            $tpl->show();
            break;
            default:
                $tpl->replaceTags(array(
                "title" => $this->setTitle('Home'),
                "nav" => "sources/nav.php",
                "content" => "sources/main.php"
            ));
            $tpl->show();
            break;
        }
    }
}
?>
I don't think it has anything to do with the replaced tags, but here is that function..

Code: Select all

<?php
if (basename($_SERVER["PHP_SELF"]) == "template.obj.php")
{
    die("The page you are attempting to view is private.");
}
 
class TJJ_Template
{
    var $content;
    
    public function __construct($template = "style/TJJ.tpl") {
        if (file_exists($template))
            $this->content = join("", file($template));
        else 
            die("Template $template cannot be found.");
    }
    
    public function replaceTags($tags = array()) {
        if (sizeof($tags) > 0)
            foreach ($tags as $tag => $data) {
                $data = (file_exists($data)) ? join("", file($data)) : $data;
                $this->content = str_replace("{" .$tag. "}", $data, $this->content);
    }   else
                die("There are no tags to be replaced.");
    }
    
    public function show() {
        echo $this->content;
    }
}
?>
And my index.php that shows it all..

Code: Select all

<?php
include('class/obj/style.obj.php');
$getPage = isset($_GET['tjj']) ? $_GET['tjj'] : "";
    if ($getPage == NULL) {
        header('Location: ?tjj=home');
    }
$page = new TJJStyle();
$page->showPage($getPage);
?>

I can't see any reason as to why it doesn't work.. my template is a .tpl file coded in html.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Replacing Problem

Post by Eran »

The file is probably read as a string. Code contained in a string is not automatically evaluated. You need to include the file instead and capture the output using output-buffering functions. This will require a modification of the template system you are using, as it was probably not designed to include PHP code in templates.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Replacing Problem

Post by Trahb »

Alright I understand what you're saying, $data is read as a string.. I'm not quite sure how to have it included efficiently..

I've had it include the file, but it's not echoed correctly..
If you could add me on msn or suggest any other things to do, by all means :D
tjayjay510@hotmail.com

Thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Replacing Problem

Post by AbraCadaver »

Trahb wrote:Alright I understand what you're saying, $data is read as a string.. I'm not quite sure how to have it included efficiently..

I've had it include the file, but it's not echoed correctly..
If you could add me on msn or suggest any other things to do, by all means :D
tjayjay510@hotmail.com

Thanks
You could possibly eval() instead of echo. If $this->content has PHP tags, then you'll have to do something like:

Code: Select all

eval('?>' . $this->content);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Replacing Problem

Post by Trahb »

Where were you thinking of putting that?
In the show function in place of echo?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Replacing Problem

Post by AbraCadaver »

Trahb wrote:Where were you thinking of putting that?
In the show function in place of echo?
Yes.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Replacing Problem

Post by Trahb »

I get this error:

Parse error: parse error in C:\xampp\htdocs\Template\class\obj\template.obj.php(29) : eval()'d code on line 2
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Replacing Problem

Post by Eran »

You can't eval a string that contains both PHP code and regular HTML, as you would get the parse error you did. eval() can only evaluate pure PHP code (and it's generally considered bad practice). You will need to change the part of the templating system that includes files from a file reading operation (such as file_get_contents() ) to a real include statement (such as include() or require()). You could have a look at templating systems that employ such tactics, such as savant or Zend_View.

I wrote a post some time ago on how those templating systems work in a simplified manner, which you might find useful - http://www.techfounder.net/2008/11/18/o ... emplating/
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Replacing Problem

Post by AbraCadaver »

pytrin wrote:You can't eval a string that contains both PHP code and regular HTML, as you would get the parse error you did. eval() can only evaluate pure PHP code (and it's generally considered bad practice).
What!?!?!? Works great and is used by many. Bad practice is subjective. I agree that most consider it bad practice with user supplied data:

Code: Select all

$string = <<<HTML
<html>
<head>
<title>Test</title>
</head>
<body>
<?php echo "This is a test page."; ?>
</body>
</html>
HTML;
 
eval('?>' . $string);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Replacing Problem

Post by Eran »

I admit I don't really use eval() in any sort of capacity, so if I'm mistaken on how it actually works, my bad. As I understood it, it parses clean PHP only
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Replacing Problem

Post by AbraCadaver »

pytrin wrote:I admit I don't really use eval() in any sort of capacity, so if I'm mistaken on how it actually works, my bad. As I understood it, it parses clean PHP only
No worries. eval() is the PHP parser, so it parses PHP and HTML. The only difference is that eval() assumes the <?php opening tag so you have to close it before using HTML, just like in a PHP file.

Trahb

We would need to see the template file. Either it starts with a PHP tag or you have invalid PHP in it.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Replacing Problem

Post by Trahb »

my .tpl

Code: Select all

<html>
    <head>
        <title>{title}</title>
        
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1;" />
 
        <link type="text/css" href="style/style.css" rel="stylesheet" />    
        
        <link rel="shortcut icon" href="images/favicon.ico"> 
    </head>
    <body>
        <div id="nav_container">
            <div id="nav_btns">
                <ul class="nav">
                    {nav}
                </ul>
            </div>
        </div>
        <div id="container">
            <div id="banner"></div>
            <div id="under_banner"></div>
            <div id="sidebar_body">
                <div id="sidebar_header"></div>
                    <div id="sidebar_text">
                        Test
                    </div>
                <div id="sidebar_bottom"></div>
            </div>
            <div id="txt_container">
                {content}
            </div>
        </div>
    </body>
</html>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Replacing Problem

Post by AbraCadaver »

Well, it seems that "sources/nav.php" or "sources/main.php" contain invalid PHP or don't start with <?php.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Replacing Problem

Post by Trahb »

sources/nav

Code: Select all

<?php
echo '
<li class="home"><a href="?tjj=home">home</a></li>
<li class="home"><a href="?tjj=home">home</a></li>
<li class="register"><a href="?tjj=register">register</a></li>
<li class="download"><a href="?tjj=downloads">download</a></li>
<li class="forum"><a href="?tjj=home">forum</a></li>
<li class="rankings"><a href="?tjj=rankings">rankings</a></li>';
?>
sources/main just has a test in it, like

Code: Select all

<?php
$test = "test";
echo $test;
?>
I think it's cause it's being read as a string and being echoed out as one rather than included as a file. but i can't figure out how to include it \:
Post Reply