TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
HTML Templating system
  • Hi, I'm starting trying Kohana. There is one thing I don't like and that is:
  • i have heard its easy enough to implement it with Kohana, try this link
  • alexsancho created a really handy template parser called Temper. Because each tag is a new object it's very easy to extend and add your own tags. I've used it in a few different projects that required user accessible templates.

    There is also a smarty module, but I haven't tried it so I'm not sure how well it works.
  • It can look pretty good with use of short tags

    <dl class="videos">
    <? foreach ($product->videos as $video): ?>
    
        <dt>Video: <?=$video->track ?></dt>
        <dd><?=$video->title ?>. <?=$video->description ?></dd>
    
    <? endforeach; ?>
    </dl>
    

    with in my editor it looks like this:

    img

  • If you want portable PHP code, prevent short tags. I never use them.

    Nothing is wrong with good old <?php echo $var ?> imo
    
  • Discussions about template engine always make good troll. Let's feed it ;)

    I definitely agree with yinkei : in a server environment where you have full control over configuration (like Geert says), using php short tag is the way to go. PHP is a template engine by itself.
  • PHP is a template engine by itself.

    I agree! However there are still times when you need a template system. Templates are very useful when you want to limit what someone can do, for example I recently finished a Kohana based project that allowed the user to customize an email template before sending it. I don't want to give them full access to the php interpreter, so I used the Temper library to give them access to a few different tags.
  • Posted By: Isaiah

    PHP is a template engine by itself.

    I agree! However there are still times when you need a template system. Templates are very useful when you want to limit what someone can do, for example I recently finished a Kohana based project that allowed the user to customize an email template before sending it. I don't want to give them full access to the php interpreter, so I used the Temper library to give them access to a few different tags.



    Yes, in that case a tempting system would make sense.

    @Delapouite: I could be wrong, but don't most web hosts allow you to either set php ini options either via .htaccess or ini_set() ? I am not familiar with shared hosting.
  • There is Smarty module for Kohana 2.2 and it works OK.

    Based on this module I recently created simple Dwoo module, but it's for Kohana 2.3 as I use this version. If you want to try check this post.

  • Thanks groadin, Dwoo looks very interesting.
    Just one question (or simply an observation). It seems that Dwoo can handle arrays rather than objects; so we should pass/convert objects into arrays to be treaten in Dwoo, isn't it?
  • Dwoo can handle objects too. I just assign object to my Kohana View, and then I can use it as {$objectname->prop}. You can use arrays if you wish. You can also implement ArrayAccess in your object so you access object like array :-p. It's all up to you.

  • Ah, OK. So, the "{$array.element}" form is simply a shortcut served by Dwow to access handle arrays.
  • Just another template system you may want to look at
    http://www.tinybutstrong.com/
  • Posted By: alle

    Ah, OK. So, the "{$array.element}" form is simply a shortcut served by Dwow to access handle arrays.



    Yes, Dwoo is very similar to Smarty in syntax, but also supports PHP syntax {$array['element']}, or {foo($bar)} for functions. You can read more in Dwoo wiki docs, they are not comprehensive though.
  • The moral is: RTFM. ;)
  • I'm looking at a way to offer some 'create your own template' functionality in my app. Users would be able to create their own HTML/CSS and the app would insert the output into user defined places in the HTML. Allowing users to write PHP in their templates would be a bit of a security risk :-) so I'm looking at some sort of templating.

    The main thing is; there would be a limited list of tags that the user can place in their HTML ([%%author], [%%date], [%%blogpost], [%%home_link] etc) so there's no need for anything complicated.

    Now I'm thinking of two options:

    1) Upon request: Load template. Run a preg_replace_callback and have the callback return the right code. Push the template into an empty Kohana view file ( <?php echo $view; ?>) and have the view rendered as normal.

    2) Upon uploading template: turn template into Kohana View supported code - remove any user inserted PHP and replace all [%%..] code by actual PHP code ([%%author] would become <?php echo $author; ?>. Upon request: use view as a regular Kohana view.

    What about these approaches?! Would the 2nd be significantly faster because it doesn't use any pregs on rendering?! Any other suggestions? I'm looking for simple and fast :-).
  • I also prefer second approach.
    But it means that you must keep an original copy of what the user submited or do reverse process, in case user wants to edit its template.