2009
20
Dec

Extending Template Parser

I am working on some stuff for work (yes, on the weekend) that uses the Codeigniter Template Parser class to display content with pseudo variables. I have a pseudo variable parser that allows me to call up a certain function from a certain helper file. To do this I would do something similar to this:

{ns:gallery['foo']}

Where “df” is the name of the helper file (minus “_helper”) that gets loaded in if it hasn’t been already, “gallery” is the name of the function, and everything inside the brackets are values that get passed to the function.

I also thought about doing something more like HTML tags with pseudo variables. So the above example could be:

{ns:gallery name='foo'}

Similar concept but this way all of the attributes could be thrown into an object when it gets o the function so I can have as many or as few settings in it as I want instead of having to pass a certain number of arguments in a certain order. I would have to have default values for everything in case an attribute I needed wasn’t passed. Same idea as the way HTML tags work.

This is basically like creating my own pseudo HTML tags with { and } instead of < and >. So then I thought, why not make it a namespace tag? I would just have to replace the { and } with < and > and I get:

<ns:gallery name='foo' />

To do this I would so something similar to this post titled ‘Parsing Custom Tags With PHP‘.

There are advantages and disadvantages to all three. Right now I am leaning more toward the second or third options because if someone goes in after me and needs to edit the file, I think that makes more sense than the first way. Any way I go it just means extending the Codeigniter Template Parser class. Maybe I’ll do all three and release it to the Codeigniter community.

2 Responses to Extending Template Parser

  1. Yoke says:

    Hi, I’m facing an issue where I need to extend the template parser class of CI for the sake of designers. That’s how I bumped into this article. Can u enlighten ur way of doing it? I’ve been exposed to CI for just 3 weeks and this seems like mission impossible to me. Thank you for any lead…

  2. Unfortunately for me I had to abandon my template parser. I was doing the same thing and building it for designers. They just couldn’t grasp the concept of them. So I had to resort to making a series of helpers with a bunch of methods in them that could be called from the theme files that did the same thing as the short codes would have done.

    Phil Sturgeon has a library that allows you to call helper methods as a short code. It works on both PHP functions as well as functions from your custom helpers files. You can download it at https://github.com/philsturgeon/codeigniter-helpfulparser

    Dan Horrigan has a library called Frisker that basically does the same thing except it only does PHP functions. It’s also only for CI2. You can get it from https://github.com/dhorrigan/codeigniter-frisker With a little customization you an probably get it to call helper methods as well.

    Then of course there is Simpletags at http://hg.dhorrigan.com/simpletags/src This is probably more along the lines of what you are wanting.

    Hope this helps!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>