[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActionView/helpers/ -> capture_helper.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActionView
  13   * @subpackage Helpers
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  
  20  /**
  21   * Capture lets you extract parts of code into instance variables which
  22  * can be used in other points of the template or even layout file.
  23  *
  24  * == Capturing a block into an instance variable
  25  *
  26  *   <?php $capture_helper->begin (); ?>
  27  *     [some html...]
  28  *   <?php $script = $capture_helper->end (); ?>
  29  *  
  30  *
  31  * == Add javascript to header using content_for
  32  *
  33  * $capture_helper->content_for("name"); is a wrapper for capture which will store the 
  34  * fragment in a instance variable similar to $content_for_layout.
  35  *
  36  * layout.tpl:
  37  *
  38  *   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  39  *   <head>
  40  *        <title>layout with js</title>
  41  *        <script type="text/javascript">
  42  *        {content_for_script}
  43  *       </script>
  44  *   </head>
  45  *   <body>
  46  *     {content_for_layout}
  47  *   </body>
  48  *   </html>
  49  *
  50  * view.tpl
  51  *   
  52  *   This page shows an alert box!
  53  *
  54  *   <?php $capture_helper->begin ('script'); ?>
  55  *     alert('hello world');
  56  *   <?php $capture_helper->end (); ?>
  57  *
  58  *   Normal view text
  59  */
  60  class CaptureHelper extends AkObject 
  61  {
  62      var $_stack = array();
  63      /**
  64       * Capture allows you to extract a part of the template into an 
  65       * instance variable. You can use this instance variable anywhere
  66       * in your templates and even in your layout. 
  67       * 
  68       * Example:
  69       * 
  70       *   <?php $capture_helper->begin(); ?>
  71       *     Welcome To my shiny new web page!
  72       *   <% $greeting = $capture_helper->end(); ?>      
  73       */
  74      function begin ($var_name = '')
  75      {
  76          ob_start();
  77          $this->_stack[] = $var_name;
  78      }
  79  
  80      function end($add_to_view = true)
  81      {
  82          $var_name = array_pop($this->_stack);
  83          $result = ob_get_clean();
  84          if($add_to_view && !empty($var_name)){
  85              $this->_addVarToView('content_for_'.$var_name, $result);
  86          }
  87          return $result;
  88      }
  89  
  90      function _addVarToView($var_name, $content)
  91      {
  92          AkActionView::_addGlobalVar($var_name, $content);
  93      }
  94  
  95      /**
  96      * Content_for will store the given block
  97      * in an instance variable for later use in another template
  98      * or in the layout. 
  99      * 
 100      * The name of the instance variable is content_for_<name> 
 101      * to stay consistent with $content_for_layout which is used 
 102      * by ActionView's layouts
 103      * 
 104      * Example:
 105      * 
 106      *   <?php $capture_helper->content_for('header'); ?>
 107      *     alert('hello world');
 108      *   <?php $capture_helper->end(); ?>
 109      *
 110      * You can use $content_for_header anywhere in your templates.
 111      *
 112      * NOTE: Beware that content_for is ignored in caches. So you shouldn't use it
 113      * for elements that are going to be fragment cached. 
 114      */
 115      function content_for($name)
 116      {
 117          $this->begin($name);
 118      }
 119  }
 120  
 121  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6