Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Template inheritance with Smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
matthiask
Smarty n00b


Joined: 18 Sep 2006
Posts: 2

PostPosted: Mon Sep 18, 2006 11:50 am    Post subject: Template inheritance with Smarty Reply with quote

I developed a small Smarty wrapper for my own web application SDK ( SWISDK 2 ) which allows me to use template inheritance similar to the django template language. I thought maybe someone here is interested Smile

As a bonus, the Wrapper runs smarty in a less strict error_reporting setting, so I can develop the rest of my websites with E_ALL|E_STRICT under PHP5.

This works:

base.tpl
Code:

< h1 >{block name="title"}default title{/block}< / h1 >

<p>{block name="content"}some default content{/block}</p>
<p>{block name="empty"}{/block}</p>


derived.tpl
Code:

{extends file="base.tpl"}
{block name="title"}derived title{/block}
{block name="content"}something else{/block}


derived2.tpl
Code:

{extends file="derived.tpl"}
{block name="title"}derived2 title{/block}
{block name="empty"}not empty anymore{/block}


The result will be:

Code:

< h1 >derived2 title< / h1 >

<p>something else</p>
<p>not empty anymore</p>



If you want the code, go to
http://spinlock.ch/pub/git/?p=swisdk2/swisdk.git;a=tree, modules, inc.smarty.php (License is GPL, although I will change it if there is a good reason to do so.)


Or here:

Code:

   class SwisdkSmarty {
      public function __construct()
      {
         // make sure E_STRICT is turned off
         $er = error_reporting(E_ALL^E_NOTICE);
         require_once SWISDK_ROOT . 'lib/smarty/libs/Smarty.class.php';
         $this->smarty = new Smarty();
         $this->smarty->compile_dir = CACHE_ROOT.'smarty';
         $this->smarty->template_dir = CONTENT_ROOT;
         $this->smarty->caching = false;
         $this->smarty->security = false;
         $this->smarty->register_block('block', '_smarty_swisdk_process_block');
         $this->smarty->register_function('extends', '_smarty_swisdk_extends');
         $this->smarty->assign_by_ref('_swisdk_smarty_instance', $this);
         error_reporting($er);
      }

      public function __call($method, $args)
      {
         $er = error_reporting(E_ALL^E_NOTICE);
         $ret = call_user_func_array(
            array(&$this->smarty, $method),
            $args);
         error_reporting($er);
         return $ret;
      }

      public function __get($var)
      {
         $er = error_reporting(E_ALL^E_NOTICE);
         $ret = $this->smarty->$var;
         error_reporting($er);
         return $ret;
      }

      public function __set($var, $value)
      {
         $er = error_reporting(E_ALL^E_NOTICE);
         $ret = ($this->smarty->$var = $value);
         error_reporting($er);
         return $ret;
      }

      public function display($resource_name)
      {
         echo $this->fetch($resource_name);
      }

      public function fetch($resource_name)
      {
         $ret = $this->smarty->fetch($resource_name);
         while($resource = $this->_derived) {
            $this->_derived = null;
            $ret = $this->smarty->fetch($resource);
         }
         return $ret;
      }

      protected $smarty;

      // template inheritance
      public $_blocks = array();
      public $_derived = null;
   }

   function _smarty_swisdk_process_block($params, $content, &$smarty, &$repeat)
   {
      if($content===null)
         return;
      $name = $params['name'];
      $ss = $smarty->get_template_vars('_swisdk_smarty_instance');
      if(!isset($ss->_blocks[$name]))
         $ss->_blocks[$name] = $content;
      return $ss->_blocks[$name];
   }

   function _smarty_swisdk_extends($params, &$smarty)
   {
      $ss = $smarty->get_template_vars('_swisdk_smarty_instance');
      $ss->_derived = $params['file'];
   }

Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP