Page 1 of 2
Replacing Problem
Posted: Sat Jan 30, 2010 9:18 pm
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.
Re: Replacing Problem
Posted: Sun Jan 31, 2010 11:21 am
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.
Re: Replacing Problem
Posted: Sun Jan 31, 2010 11:24 am
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.
Re: Replacing Problem
Posted: Sun Jan 31, 2010 12:15 pm
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
tjayjay510@hotmail.com
Thanks
Re: Replacing Problem
Posted: Sun Jan 31, 2010 1:55 pm
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
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:
Re: Replacing Problem
Posted: Sun Jan 31, 2010 2:03 pm
by Trahb
Where were you thinking of putting that?
In the show function in place of echo?
Re: Replacing Problem
Posted: Sun Jan 31, 2010 2:06 pm
by AbraCadaver
Trahb wrote:Where were you thinking of putting that?
In the show function in place of echo?
Yes.
Re: Replacing Problem
Posted: Sun Jan 31, 2010 2:09 pm
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
Re: Replacing Problem
Posted: Sun Jan 31, 2010 3:37 pm
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/
Re: Replacing Problem
Posted: Sun Jan 31, 2010 3:47 pm
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);
Re: Replacing Problem
Posted: Sun Jan 31, 2010 3:50 pm
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
Re: Replacing Problem
Posted: Sun Jan 31, 2010 4:01 pm
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.
Re: Replacing Problem
Posted: Sun Jan 31, 2010 4:03 pm
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>
Re: Replacing Problem
Posted: Sun Jan 31, 2010 4:09 pm
by AbraCadaver
Well, it seems that "sources/nav.php" or "sources/main.php" contain invalid PHP or don't start with <?php.
Re: Replacing Problem
Posted: Sun Jan 31, 2010 4:12 pm
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 \: