/tags/1.0.1/lib/AkActionView/helpers/capture_helper.php

https://github.com/akelos/v1 · PHP · 121 lines · 28 code · 8 blank · 85 comment · 2 complexity · 17311c2dcfef1996b9a24ccda7030f8e MD5 · raw file

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