Welcome Guest, Not a member yet? Register   Sign In
Using <!--- ---> instead of <?php ?> in view files

About the built-in parser and conditonnal statements : in fact you can do it, but in your controller. Here is the trick : let's imagine you have this template :
Hello {username}
{if_logged}You are logged in{/if_logged}

And in your controller :

$tpl = array('username'=>$this->user->name, 'if_logged' => array());
if ( $this->user->name != 'Anonymous' ) {
  $tpl['if_logged'][] = array();

The condition check has to be done in the controller through...

I like that trick Smile And i think it's good practice too, moving the control structures out of the template.


That is very cool, Dready but there's no way to have an elseif and else branches.

Why isn't that possible ?

Hello {username}
{if_blue}You you have blue eyes{/if_blue}
{if_red}You you have red eyes{/if_red}
{else}You have sunglasses{/else}

And in your controller :
$tpl = array('username'=>$this->user->name, 'if_blue' => array(),'if_red'=>array(),'else'=>array());
if ( $this->user->eyes->color == 'blue' ) {
  $tpl['if_blue'][] = array();
} elseif ( $this->user->too_much_smoke ) {
  $tpl['if_red'][] = array();
} else  {
  $tpl['else'][] = array();

It's really a matter of taste, I like my template files without any PHP code, so web designers can work on design, and php developer (including me) can work on code.

But in this case I can only have 1 {else} branch per document!

Are you kidding ? Call it 'second_else', 'elseelse' or 'whatever', it's only the name of the key of an array...

No, I'm not stupid Smile

But it's not too elegant. This is how it should look in a perfect world:

You you have blue eyes
You have sunglasses

[eluser]Rick Jolly[/eluser]
You guys shouldn't even be debating the template parser. It's great for the one-off email template, but don't create an application around it. It parses the view on every request. Smarty will compile the view into native php on the first load. Smarty also has caching support.

Theme © iAndrew 2016 - Forum software by © MyBB