Evaluating variables in included file

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
Martin Strand
Forum Newbie
Posts: 4
Joined: Sun Nov 14, 2004 3:12 pm

Evaluating variables in included file

Post 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
Martin Strand
Forum Newbie
Posts: 4
Joined: Sun Nov 14, 2004 3:12 pm

Post 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?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

&lt;h1&gt;&lt;?php echo $header; ?&gt;&lt;/h1&gt;

&lt;p&gt;&lt;?php echo $paragraph; ?&gt;&lt;/p&gt;

Code: Select all

&lt;?php

$header = 'i am a header';

$paragraph = 'i am a paragraph';

include('template.php');

?&gt;
Martin Strand
Forum Newbie
Posts: 4
Joined: Sun Nov 14, 2004 3:12 pm

Post by Martin Strand »

Thanks, but the thing is that I don't want any php code in the template file.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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.
Martin Strand
Forum Newbie
Posts: 4
Joined: Sun Nov 14, 2004 3:12 pm

Post 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. ;)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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'));
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

Code: Select all

<?
function set($tag, $value) {
    $this->tags[] = $this->tagprefix . $field . $this->tagsuffix;
$field should be $tag? ;)
Post Reply