File: docs/

Recommend this page to a friend!
  Classes of Jin Nguyen  >  Puja  >  docs/  >  Download  
File: docs/
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Puja
Template engine that compiles templates into PHP
Author: By
Last change:
Date: 7 years ago
Size: 2,017 bytes



Class file image Download
Custom template tags
Puja’s template system comes with a wide variety of <a href="">built-in tags</a> and <a href="">filters</a> designed to address the presentation logic needs of your application. 
Hoever, sometime you may find yourself needing functionality that is not covered by the core set of template primitives. You can extend the template engine by defining custom tags and filters using PHP, and then make them available to your template.

- <strong>I. Writing custom template filters</strong><br />
Custom filters are just method of PHP class.<br />
For example, you need a new filter <strong>cutend</strong> to cut a number end characters of the string.<br />
Here’s an example filter definition in PHP:<br />
  class CustomerFilter{
      function filter_cutend($value, $arg = null){
        if(!$arg) return $value;
        return substr($value, - $arg);
<strong>main.php</strong><br />
  include 'customer_filters.php';
  $tpl = new Puja;
  $tpl->customer_filter = new CustomerFilter;
And here’s an example of how that filter would be used:
<pre>{{ name|cutend:4 }} // have argument
  {{ name|cutend }} // no argument
If <strong>name</strong> is "I like puja", the output will be "puja" and "I like puja".<br />
** You can overwrite all built-in filter (even filter <a href="">main</a>) by set the customer filter name same with built-in filter name.

- <strong>II. Writing custom template tags</strong><br />

** <i>Filter and tag methods should always return something. They shouldn’t raise exceptions. They should fail silently. In case of error, they should return either the original input or an empty string – whichever makes more sense.</i>

For more information send a message to info at phpclasses dot org.