Replacing Problem
Moderator: General Moderators
Replacing Problem
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.
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
Bump.. I really need help with this
I don't think it has anything to do with the replaced tags, but here is that function..
And my index.php that shows it all..
I can't see any reason as to why it doesn't work.. my template is a .tpl file coded in html.
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;
}
}
}
?>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;
}
}
?>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
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
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
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
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Replacing Problem
You could possibly eval() instead of echo. If $this->content has PHP tags, then you'll have to do something like: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
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.
Re: Replacing Problem
Where were you thinking of putting that?
In the show function in place of echo?
In the show function in place of echo?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Replacing Problem
Yes.Trahb wrote:Where were you thinking of putting that?
In the show function in place of echo?
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.
Re: Replacing Problem
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
Parse error: parse error in C:\xampp\htdocs\Template\class\obj\template.obj.php(29) : eval()'d code on line 2
Re: Replacing Problem
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/
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/
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Replacing Problem
What!?!?!? Works great and is used by many. Bad practice is subjective. I agree that most consider it bad practice with user supplied data: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).
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.
Re: Replacing Problem
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
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Replacing Problem
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.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
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.
Re: Replacing Problem
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>- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Replacing Problem
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.
Re: Replacing Problem
sources/nav
sources/main just has a test in it, like
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 \:
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>';
?>Code: Select all
<?php
$test = "test";
echo $test;
?>