Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Extended Plugins (Widgets)

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
TakingSides
Smarty Rookie


Joined: 11 Dec 2010
Posts: 30

PostPosted: Thu Sep 29, 2011 9:19 am    Post subject: Extended Plugins (Widgets) Reply with quote

I have been working on a Lightweight PHP Framework (soon to be released into the open source community). There are many reasons for using Smarty, but two absolute advantages of using Smarty are:

1. Template Inheritence.
2. Plugins and Hooks.

I am proposing a potentially new standard plug-in for Smarty. A quick overview about how this plug-in would work...

~/Smarty/generated/
~/Smarty/plugins/
~/Smarty/sysplugins/
~/Smarty/widgets/<widget_name_lower_case>/
~/Smarty/widgets/<widget_name_lower_case>/controller.php
~/Smarty/widgets/<widget_name_lower_case>/view.php
~/Smarty/debug.tpl
~/Smarty/Smarty.class.php

I will use the following scenario in order to example the plug-in. Assume there is a template called standard.tpl in your $template_dir:

Code:
<!DOCTYPE HTML>
<html>
   <head>
      <!-- headers, etc. -->
   </head>
   <body>
      <div id="header">
         <!-- header here -->
      </div>
      
      <div id="body">
         <div id="leftcol">
            {block 'pageContent'}{/block}
         </div>
         <div class="rightcol">
            {widget name='Categories'}
         </div>   
         <div class="rightcol">
            <!-- adverts here perhaps? -->
         </div>
      </div>
      
      <div id="footer">
         <!-- Footer Goes Here -->
      </div>
   </body>
</html>


And then you have lot's of pages, but I will showcase one example; home.tpl

Code:
{extends 'standard.tpl'}
{block 'pageContent'}
    {widget name='Top5Rated'}
{/block}


The Smarty Plug-in (at current) is function.widget.php:

Code:
<?php
function smarty_function_widget($params, $template) {
   if (empty($params['name'])) {
      user_error('Widget is missing name.', E_USER_ERROR);
   }
   
   if (file_exists($controller = sprintf('%s/controller.php', CONF_DIR_WIDGETS . strtolower($params['name'])))) {
      require_once ($controller);
   } else {
      eval(sprintf('class %s extends Smarty_Widget {}', $params['name']));
   }
   
   $widget = call_user_func($params['name'] . '::factory', $params);
   $widget->loadView($template->smarty)->execute();
}


The standard Smarty Widget class is:

Code:
class Smarty_Widget {
   
   protected $smarty;
   protected $view = 'view';
   
   private $_vars   = array();
   
   final public static function factory($params = array()) {
      $className = get_called_class();
      return new $className($params);
   }
   
   final private function __construct($params) {
      $this->_vars = $params;
      unset($params, $this->_vars['name']);
   }
   
   final public function loadView($smarty) {
      $this->smarty = $smarty;
      $this->smarty->assign($this->_vars);
      
      return $this;
   }
   
   final protected function display() {
      $className = strtolower(get_called_class());
      $this->smarty->display(CONF_DIR_WIDGETS . $className . '/' . $this->view);
   }
   
   public function execute() {
      return $this->display();
   }
}


Then the widgets would work in the following way:

~/Smarty/widgets/Categories/controller.php

Code:
<?php
class Categories extends Smarty_Widget {
   
   public function execute() {
      // your widget runs, happily ever after...
      // this is where you can load in models (if you do MVC).
      
      return $this->display();
   }
   
}


~/Smarty/widgets/Categories/view.tpl

The only requirement is a view.tpl, however controller.php is optional I can however shoew you a way of adding multiple views into your widget and switch between them appropriately...

./widgets/tabs/controller.php
./widgets/tabs/view_accounts.tpl
./widgets/tabs/view_blog.tpl
./widgets/tabs/view_contacts.tpl
./widgets/tabs/view_site.tpl

Code:
<?php
class Tabs extends Smarty_Widget {
   
   public function execute() {
      $className = strtolower(get_called_class());
      $this->_vars['type'] = 'site';
      
      if (!file_exists(CONF_DIR_WIDGETS . $className . '/view_' . strtolower($this->_vars['type'])))
         user_error(sprintf('View does not exist within widget %s.', $className), E_USER_ERROR);
      
      $this->view .= '_' . strtolower($this->_vars['type']);
      
      return $this->display();
   }
   
}


Quickly implement this using...

Code:
{widget name='Tabs' type='accounts'}


Finally, you're clearly thinking "...and how does this differ from the standard plug-ins Smarty already enables?

I haven't really figured a seriously strong argument to this question myself, in terms of improvements... Only environmental benefits however:

1. A more sophisticated MVC environment (unlike some poorly implemented MVC which aren't true MVC).
2. Using widgets reduces the need for includes (which i find a bad implementation of templated environments any way...).
3. Widgets are clean and group the views templates, keeping things tidy.a
4. Designers can create site widgets without needing ANY php usage or knowledge.
5. Developers can work inline with designers on the widgets.

But, what does everyone else think? Perhaps a standard Smarty feature is just not necessary, i'd be interested to know your views though.
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Thu Sep 29, 2011 10:15 am    Post subject: Reply with quote

I like the general idea and your approach (mostly). I do feel this widget thing is something the framework should provide. Imho there is more to widgets than what Smarty can (or should) handle.

As such I would love to see your idea being worked into a nice blog post or some article we may provide in a howto/tips section.
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins 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