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

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

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

Post 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).
Image Image
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

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

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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.
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post 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.)
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

That's pretty good, but I was hoping to have nothing but template code, nothing hybrid php/tpl. Thanks anyway.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Something like vBulletin's template conditionals would be fantastic. Maybe I'll figure out how to find myself into a vBulletin installation.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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:
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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>.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :)
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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>
Post Reply