Rearranging scripts to reduce the hassle of updating local application whenever scrip...
[akelos.git] / lib / AkActionView / helpers / capture_helper.php
blob96fd6c12ebe66e956aec4b2f10e0a8124d530f58
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
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 // +----------------------------------------------------------------------+
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>
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.
24 * == Capturing a block into an instance variable
26 * <?php $capture_helper->begin (); ?>
27 * [some html...]
28 * <?php $script = $capture_helper->end (); ?>
31 * == Add javascript to header using content_for
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.
36 * layout.tpl:
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>
50 * view.tpl
52 * This page shows an alert box!
54 * <?php $capture_helper->begin ('script'); ?>
55 * alert('hello world');
56 * <?php $capture_helper->end (); ?>
58 * Normal view text
60 class CaptureHelper
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.
68 * Example:
70 * <?php $capture_helper->begin(); ?>
71 * Welcome To my shiny new web page!
72 * <% $greeting = $capture_helper->end(); ?>
74 function begin ($var_name = '')
76 ob_start();
77 $this->_stack[] = $var_name;
80 function end($add_to_view = true)
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);
87 return $result;
90 function _addVarToView($var_name, $content)
92 AkActionView::_addGlobalVar($var_name, $content);
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.
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
104 * Example:
106 * <?php $capture_helper->content_for('header'); ?>
107 * alert('hello world');
108 * <?php $capture_helper->end(); ?>
110 * You can use $content_for_header anywhere in your templates.
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.
115 function content_for($name)
117 $this->begin($name);