tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file tangra_simple_tple.class.php

Documentation is available at tangra_simple_tple.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. // $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Tangra_Simple_Tple
  8.  * @package  tangra_lib
  9.  * @subpackage  core
  10.  */
  11.  
  12. /**
  13.  * Loading parent class
  14.  */
  15. require_once(TANGRA_MAIN_DIR.'core/tangra_class.class.php');
  16.  
  17. /**
  18.  * Loading interface
  19.  */
  20. require_once(TANGRA_MAIN_DIR.'interfaces/i_tple.class.php');
  21.  
  22. /**
  23.  * Tangra Simple_Tple version
  24.  *
  25.  */
  26. define('TANGRA_MODULE_SIMPLE_TPLE''1.0');
  27.  
  28. /**
  29.  * Contains detected value for Tangra_Simple_Tple directory.
  30.  *
  31.  */
  32. define('TANGRA_MODULE_SIMPLE_TPLE_DIR'dirname(__FILE__).'/');
  33.  
  34. /**
  35.  * Minimal version of Tangra_Lib that is required by Tangra_Simple_Tple.
  36.  * This may become deprecated - it was inteded to be used if Tangra_Simple_Tple is separate module.
  37.  *
  38.  */
  39. define('TANGRA_MODULE_SIMPLE_TPLE_MIN_TANGRA_VERSION''2.0.0');
  40.  
  41.  
  42. require_once(TANGRA_MAIN_DIR.'exceptions/te_tple_path_not_exists.class.php');
  43. require_once(TANGRA_MAIN_DIR.'exceptions/te_tple_file_not_exists.class.php');
  44. require_once(TANGRA_MAIN_DIR.'exceptions/te_var_already_exists.class.php');
  45.  
  46. /**
  47.  * Very basic template engine.
  48.  *
  49.  * Used by installers of site templates (we don't want to configure "mastodont" template engine every time when we do installaton, do we?)
  50.  * @package  tangra_lib
  51.  * @subpackage  core
  52.  */
  53. class Tangra_Simple_Tple extends Tangra_Class implements I_Tple {
  54.     /**
  55.      * Path to templates directory.
  56.      *
  57.      * @var string 
  58.      * @access private
  59.      */
  60.     private $tpl_path;
  61.  
  62.     /**
  63.      * Contains assigned by Tangra_Simple_Tple::assign() values. This values will be used to replace all tags in the template.
  64.      * It is simple associative array $key => $value
  65.      *
  66.      * @var array Simple associative array
  67.      * @access private
  68.      */
  69.     private $vars = array();
  70.  
  71.     /**
  72.      * Left delimiter for tags. Default is {@ 
  73.      *
  74.      * @var string 
  75.      *  $access private
  76.      */
  77.     private $left_delimiter// left sequence for tags
  78.  
  79.     /**
  80.      * Right delimiter for tags. Default is @}
  81.      *
  82.      * @var string 
  83.      */
  84.     private $right_delimiter// right sequence for tag
  85.  
  86.     /**
  87.      * Sets template paths and throws exception if the path is not existing.
  88.      *
  89.      * @param string $tpl_path path to template directory
  90.      * @param string $ld  left delimiter for tags. Default is {@ 
  91.      * @param string $rd right delimiter for tags. Default is @}
  92.      * @throws Tangra_Exception
  93.      */
  94.     function __construct($tpl_path NULL$ld '{@'$rd '@}'{
  95.             throw new Tangra_Exception('Can not initialize Tangra_Simple_Tple - Tangra lib version '.TANGRA_MODULE_SIMPLE_TPLE_MIN_TANGRA_VERSION.' or newer required. Current version is: '.TANGRA_VERSION);
  96.         }
  97.  
  98.         $this->set_tpl_dir($tpl_path);
  99.         $this->set_left_delimiter($ld);
  100.         $this->set_right_delimiter($rd);
  101.     }
  102.  
  103.     /**
  104.      * Parses and displays template file.
  105.      *
  106.      * @param string $tpl file to be showed
  107.      */
  108.     public function display($tpl{
  109.         print($this->fetch($tpl));
  110.     }
  111.  
  112.     /**
  113.      * Parses $content and returns it with all tags replaced with assigned values.
  114.      *
  115.      * @param string $content content to be parsed
  116.      * @param boolean $strip_unused_tags if true will strip tags that are not replaced (this happens when there are tags in the $content  but no values are provided for them with assign())
  117.      * @return string parsed content
  118.      */
  119.     public function _fetch($content$strip_unused_tags true{
  120.         $ld $this->get_left_delimiter();
  121.         $rd $this->get_right_delimiter();
  122.         foreach($this->vars as $key => $value{
  123.             $content str_replace($ld.'$'.$key.$rd$value$content);
  124.         }
  125.  
  126.         if ($strip_unused_tags{
  127.             //striping unused var's placeholders
  128.             $content ereg_replace($ld.'\$[a-zA-Z0-9_]+'.$rd''$content);
  129.         }
  130.  
  131.  
  132.         return $content;
  133.     }
  134.  
  135.  
  136.     /**
  137.      * Loads file specified by $tpl, parses it and returns it with all tags replaced with assigned values.
  138.      *
  139.      * @param string $tpl 
  140.      * @return string 
  141.      */
  142.     public function fetch($tpl{
  143.         $ret false;
  144.  
  145.         if (file_exists($this->get_tpl_dir().$tpl)) {
  146.             $content file_get_contents($this->get_tpl_dir().$tpl);
  147.  
  148.             $ret $this->_fetch($content);
  149.         else {
  150.             throw new TE_Tple_File_Not_Exists($this->get_tpl_dir().$tpl);
  151.         }
  152.  
  153.         return $ret;
  154.     }
  155.  
  156.  
  157.     /**
  158.      * Assignes value to a key/tag.
  159.      * Will throw exception if $tpl_var does not conform ereg("[a-zA-Z0-9_]+"), i.e. is not alphanumeric (with optional underscore)
  160.      *
  161.      * @param string $tpl_var 
  162.      * @param string $value 
  163.      * @throws Tangra_Exception
  164.      */
  165.     public function assign($tpl_var$value null{
  166.         if ($this->check_tpl_var_name($tpl_var)) {
  167.             $this->vars[$tpl_var$value;
  168.         else {
  169.             throw new Tangra_Exception('Variable name is not valid. $tpl_var = '.$tpl_var);
  170.         }
  171.     }
  172.  
  173.     /**
  174.      * Assignes value to a key/tag.  Similar to Tangra_Simple_Tple::assign() but will throw exception if there is already added value with the same key.
  175.      * Will throw exception if $tpl_var does not conform ereg("[a-zA-Z0-9_]+"), i.e. is not alphanumeric (with optional underscore)
  176.      *
  177.      * @param string $tpl_var key/tag name
  178.      * @param any $value 
  179.      * @throws  TE_Var_Already_Exists
  180.      */
  181.     public function assign_strict($tpl_var$value{
  182.         if ($this->check_tpl_var_name($tpl_var)) {
  183.             if (!array_key_exists($tpl_var$this->vars)) {
  184.                 $this->vars[$tpl_var$value;
  185.             else {
  186.                 throw new TE_Var_Already_Exists('Variable "'.$tpl_var.'" already exists.');
  187.             }
  188.         else {
  189.             throw new Tangra_Exception('Variable name is not valid. $tpl_var = '.$tpl_var);
  190.         }
  191.     }
  192.  
  193.     /**
  194.      * Clears all asigments.
  195.      *
  196.      */
  197.     public function clear_all_assigns({
  198.         $this->vars = array();
  199.     }
  200.  
  201.  
  202.     /**
  203.      * Sets entire assigment array at once. Useful when array is loaded from file. Normally is not used by the users.
  204.      * Throws exception if parameter is not an array.
  205.      * @param unknown_type $vars_array 
  206.      * @throws Tangra_Exception
  207.      */
  208.     public function assign_set_array($vars_array{
  209.         if (is_array($vars_array)) {
  210.             $this->vars = $vars_array;
  211.         else {
  212.             throw new Tangra_Exception('$vars_array is not an array');
  213.         }
  214.     }
  215.  
  216.     /**
  217.      * Sets template dir that will be used to compose the full path for the template file
  218.      *
  219.      * @param string $tpl_path 
  220.      */
  221.     public function set_tpl_dir($tpl_path{
  222.         if ($tpl_path{
  223.             if (file_exists($tpl_path)) {
  224.                 $this->tpl_path = tangra_normalize_path($tpl_path);
  225.             else {
  226.                 throw new TE_Tple_Path_Not_Exists(tangra_normalize_path($tpl_path));
  227.             }
  228.         else {
  229.             $this->tpl_path = tangra_normalize_path($tpl_path);
  230.         }
  231.     }
  232.  
  233.  
  234.     /**
  235.      * Gets template path
  236.      *
  237.      * @return string 
  238.      */
  239.     public function get_tpl_dir({
  240.         return $this->tpl_path;
  241.     }
  242.  
  243.  
  244.     /**
  245.      * Sets left delimiter for the tags in the template
  246.      *
  247.      * @param string $left_delimiter 
  248.      */
  249.     public function set_left_delimiter($left_delimiter '{@$'{
  250.         $this->left_delimiter = $left_delimiter;
  251.     }
  252.  
  253.     /**
  254.      * Gets left delimiter for the tags in the template
  255.      *
  256.      * @param string $left_delimiter 
  257.      */
  258.  
  259.     public function get_left_delimiter({
  260.         return $this->left_delimiter;
  261.     }
  262.  
  263.  
  264.     /**
  265.      * Sets right delimiter for the tags in the template
  266.      *
  267.      * @return unknown 
  268.      */
  269.     public function set_right_delimiter($right_delimiter '@}'{
  270.         $this->right_delimiter = $right_delimiter;
  271.     }
  272.  
  273.     /**
  274.      * Gets right delimiter for the tags in the template
  275.      *
  276.      * @return unknown 
  277.      */
  278.     public function get_right_delimiter({
  279.         return $this->right_delimiter;
  280.     }
  281.  
  282.  
  283.     /**
  284.      * Checks if $tpl_var is "valid" key identifier
  285.      *
  286.      * @param string $tpl_var 
  287.      * @return boolean 
  288.      * @access  private
  289.      */
  290.     private function check_tpl_var_name($tpl_var{
  291.         $ret false;
  292.  
  293.         if (ereg"[a-zA-Z0-9_]+"$tpl_var)) {
  294.             $ret true;
  295.         }
  296.  
  297.         return $ret;
  298.     }
  299. }