CodeIgniter Forums
Using boolean data in CIs template Parser - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Using boolean data in CIs template Parser (/showthread.php?tid=25064)



Using boolean data in CIs template Parser - El Forum - 11-30-2009

[eluser]Unknown[/eluser]
Hi there

I made a small modification to the template parser class to make it possible to use boolean data to control {tag}{/tag} pairs like:

Code:
{itemspresent}
<h1>This block is only present, if "itemspresent" is true</h1>
<ol>
  &lt;!-- the li element is only green, if the item entry has a true value for "choosen" --&gt;
  {items}
  <li {choosen}style="color: green"{/choosen}>{name}</li>
  {/items}
</ol>
{/itemspresent}

It isn't thoroughly tested and may have a speed penalty when working with boolean data not intended for this use case, but it works quite well for me and was the one feature I needed to allow me to use the build-in template parser for all my views.

I have attached the full patched version as well as the patch file to the 1.7.2 version.


Using boolean data in CIs template Parser - El Forum - 11-30-2009

[eluser]wiredesignz[/eluser]
Nice idea cylab, Thanks for this. Wink


Using boolean data in CIs template Parser - El Forum - 11-30-2009

[eluser]Phil Sturgeon[/eluser]
Someone did this about a year ago with this same syntax. Someone else did something similar even more recently.

People really should search before they write code, just to make sure they arent duplicating efforts.


Using boolean data in CIs template Parser - El Forum - 11-30-2009

[eluser]Unknown[/eluser]
@wiredesignz
Glad you like it Smile

@phil
Actually I was just experimenting with CI and Template library, since I just started doing php again. I learned a lot about CI on the way and as I came to the point where I needed this feature, the modification took me about half an hour...

Also the code you linked is way to generic for my taste. I don't want to have logic in my templates.

@CodeIgniter team
Feel free to include this in the core codebase ;-)


Using boolean data in CIs template Parser - El Forum - 11-30-2009

[eluser]Phil Sturgeon[/eluser]
Good man. Jumping into the core is the best way to learn not only how CodeIgniter works, but will teach you some great methodologies and clean syntax.

The one I linked to was a bit dodgy, the one I mentioned first was however identical. That said if I cant find it that explains why you didn't either. :-p