CodeIgniter Forums
Using <!--- ---> instead of <?php ?> in view files - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Using <!--- ---> instead of <?php ?> in view files (/showthread.php?tid=11958)

Pages: 1 2


Using <!--- ---> instead of <?php ?> in view files - El Forum - 09-30-2008

[eluser]Dready[/eluser]
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 :
Code:
Hello {username}
{if_logged}You are logged in{/if_logged}

And in your controller :

Code:
$tpl = array('username'=>$this->user->name, 'if_logged' => array());
if ( $this->user->name != 'Anonymous' ) {
  $tpl['if_logged'][] = array();
}
$this->parser->parse('myview',$tpl);

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


Using <!--- ---> instead of <?php ?> in view files - El Forum - 09-30-2008

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


Using <!--- ---> instead of <?php ?> in view files - El Forum - 09-30-2008

[eluser]EEssam[/eluser]
Hi,

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


Using <!--- ---> instead of <?php ?> in view files - El Forum - 09-30-2008

[eluser]Dready[/eluser]
Why isn't that possible ?

Code:
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 :
Code:
$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();
}
$this->parser->parse('myview',$tpl);

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.


Using <!--- ---> instead of <?php ?> in view files - El Forum - 09-30-2008

[eluser]EEssam[/eluser]
But in this case I can only have 1 {else} branch per document!


Using <!--- ---> instead of <?php ?> in view files - El Forum - 09-30-2008

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


Using <!--- ---> instead of <?php ?> in view files - El Forum - 09-30-2008

[eluser]EEssam[/eluser]
No, I'm not stupid Smile

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

{if_blue}
You you have blue eyes
{else}
You have sunglasses
{/if_blue}


Using <!--- ---> instead of <?php ?> in view files - El Forum - 09-30-2008

[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.