Page 1 of 1
Need help with basic Template trial script
Posted: Wed May 27, 2009 9:50 pm
by blue
Code: Select all
<?php
$text="Hello {name}, you are {age} years old.";
$name="ABCD";
$age=19;
function replace($str)
{
$sub=substr($str[0],1,-1);
$sub='$'.$sub;
return $sub;
}
$text=preg_replace_callback("|(\{)(\w+)(\})|","replace",$text);
echo "<br/>".$text;
?>
I expected the output to be "Hello ABCD, you are 19 years old."
But it is "Hello $name, you are $age years old."
Where am I going wrong?
Re: Need help with basic Template trial script
Posted: Thu May 28, 2009 9:48 am
by blue
eval() did the job. thanks. I was unaware of the function.
Can you tell me where can I learn more about php templates?
Re: Need help with basic Template trial script
Posted: Thu May 28, 2009 10:02 am
by anand
blue wrote:eval() did the job. thanks. I was unaware of the function.
Can you tell me where can I learn more about php templates?
Even i didn't knew that. I have learned a lot from this forum.
Re: Need help with basic Template trial script
Posted: Thu May 28, 2009 9:54 pm
by blue
this is my php file:
Code: Select all
<?php
$title="New Title";
$text=file_get_contents("temp1.html");
function replace($str)
{
$sub=substr($str[0],1,-1);
$sub='$'.$sub;
return $sub;
}
$text=preg_replace_callback("|(\{)(\w+)(\})|","replace",$text);
eval('echo $text;');
?>
and this is the included temp1.html:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{title}</title>
</head>
<body>
</body>
</html>
Problem is same as before, the page title is coming as $title and not "New Title".
Also please comment on the way I am trying to make templates, I don't quite like it. I need these to build 8-10 page websites.
Re: Need help with basic Template trial script
Posted: Fri May 29, 2009 8:46 am
by blue
Currently I am in my learning stages. I just want to know the right way of doing it.
Re: Need help with basic Template trial script
Posted: Fri May 29, 2009 9:00 am
by ashebrian
Try
Code: Select all
<title><?php print $title; ?></title>
or
Code: Select all
<title><?php echo $title; ?></title>
or this in ur temp1.html file
Code: Select all
<?php
$title = 'New Title';
echo '<title>' . $title . '</title>';
?>