Page 1 of 2

Templating/Expression - if/elseif/else (possible for, while)

Posted: Sun May 01, 2005 8:06 pm
by phice
I'm slowly developing a new version of my templating system and am very interested in implimenting a if/elseif/else statement structure in it. IPB and a few other software packages include this feature but are so integrated into the kernel that I can't find anything useful. Something like this:

Code: Select all

{if $this eq &quote;that&quote;}
 -- template code --
{elseif $that eq $this}
 -- template code --
{else}
 -- template code --
{/if}
Btw, if you have a better structure for that scheme then let me know. I'd like to keep it as simple as possible, but if it will keep the processing time down then do whatever works. I'd like to keep everything in Perl Expressions (preg_match, preg_replace, preg_replace_callback). Obviously this feature should include a callback function so I'd advise using preg_replace_callback(), but if you have a better way of doing it, by all means go ahead.

If I could get a working templating class that would process this it'd really make my day. :) I've been wondering how to do it for ages (literally).

Re: Templating/Expression - if/elseif/else (possible for, wh

Posted: Sun May 01, 2005 8:40 pm
by Roja
phice wrote: If I could get a working templating class that would process this it'd really make my day.
The smarty template system handles if/then/else almost exactly that way.

Posted: Sun May 01, 2005 8:41 pm
by phice
I should have known someone would have suggested Smarty (or the like). I want to develop my own and others, namely smarty, are too bloated for what I like.

Posted: Sun May 01, 2005 9:14 pm
by The Monkey
I modified this guy's idea somewhat, to make it more oop friendly. (Store vars in a class instead of globally, basically.)

Posted: Sun May 01, 2005 9:36 pm
by phice
That's pretty good, but I was hoping to have nothing but template code, nothing hybrid php/tpl. Thanks anyway.

Posted: Sun May 01, 2005 9:40 pm
by phice
Something like vBulletin's template conditionals would be fantastic. Maybe I'll figure out how to find myself into a vBulletin installation.

Posted: Mon May 02, 2005 12:57 am
by Roja
phice wrote:I should have known someone would have suggested Smarty (or the like). I want to develop my own and others, namely smarty, are too bloated for what I like.
Sorry, you weren't specific enough. You said you wanted a working template class that worked the way you specified. I did that, but it didnt "make your day". :)

I can understand the desire - good luck with it.

Posted: Mon May 02, 2005 7:14 am
by vigge89
I've done this for my own templating system, here's what I did:

Code: Select all

// starting and ending delimeters
$sd = '<\{';
$ed = '\}>';

// conditional statements [ if($statement) : else : endif ]
$string = preg_replace_callback ('#'.$sd.'if\(([^\(\)\n]*?)\)([\:a-z0-9]*?)?'.$ed.'(.*?)'.$sd.'else\\2'.$ed.'(.*?)'.$sd.'endif\\2'.$ed.'#s', 'parser_if', $string);
// conditional statements [ if($statement) : endif ]
$string = preg_replace_callback ('#'.$sd.'if\(([^\(\)\n]*?)\)([\:a-z0-9]*?)?'.$ed.'(.*?)'.$sd.'endif\\2'.$ed.'#s', 'parser_if', $string);

//###########################
//## { parser_if - handling conditional statements
function parser_if ($input) {
	if (parser_ret ($input[1])) return $input[3]; // return anything between if true
	return $input[4]; // return else (or nothing)
} ## } parser_if
//###########################

//###########################
//## { parser_var2globals - replace any variables in string with variables encapsled in $GLOBALS[]
function parser_var2globals ($string) {
	$string = preg_replace ('#\$([a-zA-Z0-9_]+)([^\s]*?)#', '$GLOBALS[\\1]\\2', $string);
	return $string;
} ## } parser_var
//###########################

//###########################
//## { parser_ret - return the result of a statement or the value of a variable
function parser_ret ($input) {
	return eval (parser_var2globals ('return '.$input.';'));
} ## } parser_ret
//###########################
The ([\:a-z0-9]*?)? part is needed for nested matches, where an key is added to the end of the tags.

Example:

Code: Select all

&lt;{if($x &gt; 10):x10}&gt;$x is more than 10. &lt;{if($x == 12)}&gt;$x equals to 12!&lt;{else}&gt;$x is not 12.&lt;{endif}&gt;&lt;{endif:x10}&gt;
Edit: if you find the part of vB where conditional statements is parsed, could you please give me that part? I'm interested in how vB handles nested statements :wink:

Posted: Mon May 02, 2005 6:52 pm
by McGruff
Personally, I don't like to put any kind of code into templates. The way I see it, html design is a separate skill set to programming and html designers should be allowed to work exactly the same way they are used to ie:

(1) no requirement to learn a new language - even a simplified, custom syntax

(2) templates which render properly with the normal Dreamweaver preview command - no requirement to run the page past a server (which can lead you into all kinds of problems)

That requires some code generation. The designer can be presented with a list of simple, xml-like tags for a particular page (but no loops or conditionals). When they're done designing, the template can be "compiled" ie the tags are swapped out for some php presentation code.

Posted: Mon May 02, 2005 8:29 pm
by phice
This isn't for a designer, but a production-level, company-owned website. The reason I want an advanced templating system is that I would like to be able to run the whole website through an advanced templating system without having to make .php files left and right. This would allow my partner(s) to learn the language very quickly (no xml, php knowledge required) and not waste any time.

I've managed to make <if condition="--">---</if> and <if condition="--">---<else>---</if> work properly, and am working on <if condition="--">---<elseif condition="--">---<else>---</if>.

Posted: Tue May 03, 2005 4:24 am
by vigge89
phice wrote:....and am working on <if condition="--">---<elseif condition="--">---<else>---</if>.
With multiple elseifs-support? If you finish it, I'd be more than interested in the code :)

Posted: Tue May 03, 2005 9:37 am
by phice
Indeed, multiple elseif support. I woke up to thinking how I'm going to do all of it properly and it's going to be a task to say the least.

Posted: Tue May 03, 2005 2:38 pm
by Ambush Commander
Here's what I'm thinking: take Smarty, and start getting rid of functionalities until you get what you want (aka heavy modding). In fact, it's possible the Smarty, with all its extra features, probably has a better track record than your code: it's battletested, and have been tuned. It's obvious that you have no clue how you are going to implement this stuff: more times than not, you're going to choose the wrong path, and that's going to impact performance.

Posted: Tue May 03, 2005 2:55 pm
by phice
I typically create code that runs much faster in smaller amounts of lines than most conventional software packages. I don't need anything that will work with PHP4, nor worry about security (it's a closed templating system, no one beside my staff will touch it).

But thanks for the post, I've taken that route into consideration before but I believe it's faster for me to just create my own (and learn from it, too) than just hack away at someone else's work.

Posted: Wed May 04, 2005 7:38 pm
by neophyte
I think I know what you are getting at Phice. I had to use a similar templating system in a chat system -- Global Chat (formerly iChat). It's old and written in C but it could be configured in just about any way imaginable using <IfTemplate> style tags in the html. Never had to touch any code. Something like this:

Code: Select all

<TemplateIf $User.email>	
| <a href=&quote;emailmsg.html?Toolbar=1&Subject=$Message.Subject^&quote;>Email Message </a>
<TemplateElse>
| <a href=&quote;../system/error.shtml&quote; errorcontinue=&quote;../user/modaccount.html?Toolbar=1&quote; Error=&quote;You must have an email address to send email&quote;>Email Message</a>
</TemplateIf>