Page 1 of 1
Evaluating variables in included file
Posted: Fri Dec 03, 2004 11:00 pm
by Martin Strand
Hi all. I want a very basic template system where the templates look like this:
Code: Select all
<h1>{$header}</h1>
<p>{$paragraph}</p>
and the php code:
Code: Select all
<?php
$header = "Headline";
$paragraph = "content, content, content";
// Here, somehow include the template and let php evaluate the variables
?>
How can I make this work? Feels like there should be a trivial solution.
Thanks,
Martin
Posted: Fri Dec 03, 2004 11:39 pm
by Martin Strand
Ok, I did this:
Code: Select all
$page = "print <<< END_OF_TEMPLATE\n" . file_get_contents("template.html") . "\nEND_OF_TEMPLATE;\n";
eval($page);
Perhaps there's a better solution?
Posted: Sat Dec 04, 2004 12:09 am
by rehfeld
Code: Select all
<h1><?php echo $header; ?></h1>
<p><?php echo $paragraph; ?></p>
Code: Select all
<?php
$header = 'i am a header';
$paragraph = 'i am a paragraph';
include('template.php');
?>
Posted: Sat Dec 04, 2004 12:14 am
by Martin Strand
Thanks, but the thing is that I don't want any php code in the template file.
Posted: Sat Dec 04, 2004 12:30 am
by rehfeld
can i ask why?
your origional solution wasnt exactly php free
if you really must not, you could use
<p><!--paragraph--></p>
and then use
str_replace('<!--paragraph-->', $paragraph, $template);
but why go through all the trouble,
added complexity,
consuming extra resources,
slowing the script down,
just to have something that is LESS function?
to me it seems like reinventing the wheel, w/ little benefit and lots of drawbacks.
Posted: Sat Dec 04, 2004 1:00 am
by Martin Strand
Mostly cause the templates will be edited by a very limited htmlarea-like editor and I wanna keep them as simple as possible.
Perhaps I'll go with some kind of string replace anyway, I'm not sure yet. Speed is not an issue since this script simply outputs an html file when the content changes, instead of being run every time someone browses to it.
Well, thanks for your input. I'll be back with more stupid questions soon.

Posted: Sat Dec 04, 2004 1:37 am
by Christopher
A very small, very simple template class like the following can handle most of your basic needs. Here is an example:
Code: Select all
class Template {
var $tags;
var $values;
var $tagprefix = '{';
var $tagsuffix - '}';
function set($tag, $value) {
$this->tags[] = $this->tagprefix . $field . $this->tagsuffix;
$this->values[] = $value;
}
function clear() {
unset($this->tags);
unset($this->values);
}
function parse($template) {
return str_replace($this->tags, $this->values, $template);
}
function readFile($filename) {
return @file_get_contents($filename);
}
}
// template.html
// <h1>{header}</h1>
// <p>{paragraph}</p>
$template = new Template();
$template->set('header', $header);
$template->set('paragraph', $paragraph);
echo $template->parse($template->readFile('template.html'));
Posted: Sat Dec 04, 2004 2:46 am
by mudkicker
Code: Select all
<?
function set($tag, $value) {
$this->tags[] = $this->tagprefix . $field . $this->tagsuffix;
$field should be $tag?
