Source for file tangra_simple_tple.class.php
Documentation is available at tangra_simple_tple.class.php
// *** Tangra (Application Framework and Tools for PHP)
* Contains class Tangra_Simple_Tple
require_once(TANGRA_MAIN_DIR. 'core/tangra_class.class.php');
require_once(TANGRA_MAIN_DIR. 'interfaces/i_tple.class.php');
* Tangra Simple_Tple version
define('TANGRA_MODULE_SIMPLE_TPLE', '1.0');
* Contains detected value for Tangra_Simple_Tple directory.
define('TANGRA_MODULE_SIMPLE_TPLE_DIR', dirname(__FILE__ ). '/');
* Minimal version of Tangra_Lib that is required by Tangra_Simple_Tple.
* This may become deprecated - it was inteded to be used if Tangra_Simple_Tple is separate module.
define('TANGRA_MODULE_SIMPLE_TPLE_MIN_TANGRA_VERSION', '2.0.0');
require_once(TANGRA_MAIN_DIR. 'exceptions/te_tple_path_not_exists.class.php');
require_once(TANGRA_MAIN_DIR. 'exceptions/te_tple_file_not_exists.class.php');
require_once(TANGRA_MAIN_DIR. 'exceptions/te_var_already_exists.class.php');
* Very basic template engine.
* Used by installers of site templates (we don't want to configure "mastodont" template engine every time when we do installaton, do we?)
* Path to templates directory.
* Contains assigned by Tangra_Simple_Tple::assign() values. This values will be used to replace all tags in the template.
* It is simple associative array $key => $value
* @var array Simple associative array
* Left delimiter for tags. Default is {@
* Right delimiter for tags. Default is @}
* Sets template paths and throws exception if the path is not existing.
* @param string $tpl_path path to template directory
* @param string $ld left delimiter for tags. Default is {@
* @param string $rd right delimiter for tags. Default is @}
* @throws Tangra_Exception
function __construct($tpl_path = NULL, $ld = '{@', $rd = '@}') {
* Parses and displays template file.
* @param string $tpl file to be showed
print ($this->fetch($tpl));
* Parses $content and returns it with all tags replaced with assigned values.
* @param string $content content to be parsed
* @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())
* @return string parsed content
public function _fetch($content, $strip_unused_tags = true) {
foreach($this->vars as $key => $value) {
$content = str_replace($ld. '$'. $key. $rd, $value, $content);
if ($strip_unused_tags) {
//striping unused var's placeholders
$content = ereg_replace($ld. '\$[a-zA-Z0-9_]+'. $rd, '', $content);
* Loads file specified by $tpl, parses it and returns it with all tags replaced with assigned values.
public function fetch($tpl) {
$ret = $this->_fetch($content);
throw new TE_Tple_File_Not_Exists($this->get_tpl_dir(). $tpl);
* Assignes value to a key/tag.
* Will throw exception if $tpl_var does not conform ereg("[a-zA-Z0-9_]+"), i.e. is not alphanumeric (with optional underscore)
* @throws Tangra_Exception
public function assign($tpl_var, $value = null) {
$this->vars[$tpl_var] = $value;
throw new Tangra_Exception('Variable name is not valid. $tpl_var = '. $tpl_var);
* 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.
* Will throw exception if $tpl_var does not conform ereg("[a-zA-Z0-9_]+"), i.e. is not alphanumeric (with optional underscore)
* @param string $tpl_var key/tag name
* @throws TE_Var_Already_Exists
$this->vars[$tpl_var] = $value;
throw new TE_Var_Already_Exists('Variable "'. $tpl_var. '" already exists.');
throw new Tangra_Exception('Variable name is not valid. $tpl_var = '. $tpl_var);
* Sets entire assigment array at once. Useful when array is loaded from file. Normally is not used by the users.
* Throws exception if parameter is not an array.
* @param unknown_type $vars_array
* @throws Tangra_Exception
$this->vars = $vars_array;
throw new Tangra_Exception('$vars_array is not an array');
* Sets template dir that will be used to compose the full path for the template file
* @param string $tpl_path
* Sets left delimiter for the tags in the template
* @param string $left_delimiter
* Gets left delimiter for the tags in the template
* @param string $left_delimiter
* Sets right delimiter for the tags in the template
* Gets right delimiter for the tags in the template
* Checks if $tpl_var is "valid" key identifier
if (ereg( "[a-zA-Z0-9_]+", $tpl_var)) {
|