Page 1 of 1
simple template engine
Posted: Mon Jun 16, 2008 5:27 pm
by Jerry6565
hello guys, [i'm very sorry for my bad english but i'll try to write better then possible.]
I'm searching a simple made template engine that allows me to insert vars, loops, and blocks (of other tpl) into my html file using a simple markup. I was trying to make it by me, but after I wroten this i didn't know more...
Code: Select all
class template {
private $__file;
function __construct($f){
$this->__file = file_get_contents($f);
}
function set($t) {
foreach($t as $tag => $valore) {
$this->__file = str_replace("{".$tag."}", $valore, $this->__file);
}
}
function display() {
return $this->__file;
}
function __destruct() {
unset($this->__file);
}
}
I've no ideas. can someone explain or help me me, how i can implement loops and other functions.

??
thanks a lot!!
[ sorry for my english... im italian]
Re: simple template engine
Posted: Mon Jun 16, 2008 5:58 pm
by alex.barylski
Read this and use it, pretty much most developers use something very similar.
http://www.massassi.com/php/articles/template_engines/
Re: simple template engine
Posted: Mon Jun 16, 2008 6:12 pm
by Jerry6565
thanks for answer!
I know this template engine and i kown a few other but all those do much more that i want. so i would like to try it by me.
I really only need vars submitting and loops.
not caching a lots of other things... only vars and loops
edit : i've only no ideas otherwise it would had already done.
Re: simple template engine
Posted: Mon Jun 16, 2008 6:18 pm
by alex.barylski
Look at bTemplate again...it's a simple engine without any caching in the base class (I don't think).
I use a derivative of bTemplate myself without caching and it's light weight and does everything I need it to.
Cheers
Re: simple template engine
Posted: Mon Jun 16, 2008 6:30 pm
by Jerry6565
ok you are right there is no caching in the base-class but you can't deny that the tpl syntax is really bad. In my opinion a graphic-men (i don't know the word

) in this way has control over to much. And if i must write in this way, its bether when something is procedural

you don't believe it?
I like this type of syntax (similar to smarty - but smarty it's to big

):
{varname} {loop = loopname} {load = tplname}
and i need only there 3 things!
Re: simple template engine
Posted: Tue Jun 17, 2008 1:14 am
by Christopher
There was a thread here a while back that covered simple nested blocks in templates. The thread what here:
viewtopic.php?f=1&t=76278
I don't know if they actually got a good design from that discussion. I sometimes use a template system just like you describe. It is a little more OO -- much like the one in the article Hockey linked to. But it only supports one level of blocks. that is good more most things, but I would like to expand it to nested blocks. I would be interested in the design of something very simple like you describe: tag replacement, nested blocks, basic if and foreach.
Re: simple template engine
Posted: Tue Jun 17, 2008 3:58 am
by Jerry6565
i think there are easyer ways to do the same like in the other post. For example if we use the regExp it can be so:
$tpl = preg_replace('#\{\$([a-zA-Z0-9\._]+)\}#seU', 'class::function_1("\1")', $tpl);
$tpl = preg_replace('#\{loop\=\(.*)\}(.*)\{\/loop\}#seU', 'class::function_2("\1", \'\2\')', $tpl);
$tpl = preg_replace('#\{if \((.+)\)\}(.*)\{else\}(.*)\{\/if\}#seU', 'class::function_3("\1", "\2", "\3")', $tpl);

Re: simple template engine
Posted: Tue Jun 17, 2008 7:40 am
by Jerry6565
is that makeble what i mean? are this regExps right?
Re: simple template engine
Posted: Tue Jun 17, 2008 11:57 am
by Christopher
I am not sure what you are doing. Are you going to replace the tags with PHP and then eval() the whole thing. If so then why not just use PHP templates. I think that thread and others actually attempt to split the template into a tree of sub-templates that can be iterated over, logically selected, and have tags str_replace()'d.
Re: simple template engine
Posted: Tue Jun 17, 2008 2:39 pm
by Jerry6565
No im not trying to eval the code, the $ is removed by regexp.... if i have enought time tonight i'll post my source!
Re: simple template engine
Posted: Tue Jun 17, 2008 5:15 pm
by Jerry6565
hello ^^
i had not so much time but i try to write something. if someone now understand what i mean.... i need a little bit help to expand my class for loops.
Code: Select all
class template
{
public $__file,
$__tags = array();
function __construct($file = null)
{
$this->__file ='<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>{$header.title}</title>
</head>
<body>
<h2>{$header->autor} <font size="2">[{$header-time}]</font></h2>
<div style="border:dashed 1px #6C6C6C;">{$user.nome}</div>
<h3>{$foo}</h3>
</body>
</html>';
# to do : if file exitst...
}
public function set($nome, $variabile)
{
if(is_array($variabile))
{
foreach($variabile as $k => $v)
$this->__tags[$nome][$k] = $v;
}
else if(is_object($variabile))
{
$variabile = get_object_vars( $variabile );
foreach($variabile as $k => $v)
$this->__tags[$nome][$k] = $v;
}
else
$this->__tags[$nome] = $variabile;
}
public function display()
{
$this->__file = preg_replace('#\{\$([a-zA-Z0-9\.\-\>_]+)\}#seU', 'self::phrase("\1")', $this->__file);
}
private function phrase($code)
{
$code = preg_split('/\.|\-\>|\-/', $code);
if (count($code) == 1)
return $this->__tags[$code[0]];
else
{
$temp = $this->__tags[$code[0]];
for ($i = 1; $i < count($code); $i++)
$temp = $temp[$code[$i]];
return $temp;
}
}
}