Need help with basic Template trial script

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

Post Reply
blue
Forum Newbie
Posts: 5
Joined: Wed May 27, 2009 9:46 pm

Need help with basic Template trial script

Post 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?
Last edited by Benjamin on Fri May 29, 2009 10:37 am, edited 1 time in total.
Reason: Changed code type from text to php.
blue
Forum Newbie
Posts: 5
Joined: Wed May 27, 2009 9:46 pm

Re: Need help with basic Template trial script

Post 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?
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: Need help with basic Template trial script

Post 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.
blue
Forum Newbie
Posts: 5
Joined: Wed May 27, 2009 9:46 pm

Re: Need help with basic Template trial script

Post 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.
Last edited by Benjamin on Fri May 29, 2009 10:37 am, edited 1 time in total.
Reason: Changed code type from text to php.
blue
Forum Newbie
Posts: 5
Joined: Wed May 27, 2009 9:46 pm

Re: Need help with basic Template trial script

Post by blue »

Currently I am in my learning stages. I just want to know the right way of doing it.
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: Need help with basic Template trial script

Post 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>';
?>
Post Reply