element buffering for CakePHP 1.3/2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* for CakePHP 1.3 | |
* | |
* Copyright 2012, nojimage http://php-tips.com/ | |
* | |
* Licensed under The MIT License | |
* Redistributions of files must retain the above copyright notice. | |
* | |
* @copyright 2012 nojimage | |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | |
*/ | |
class AppHelper extends Helper { | |
// .. snip another helper methods | |
/** | |
* エレメントを読み込み、バッファリングする | |
* | |
* バッファリングしたエレメントはgetAttachedElements()で呼び出す。 | |
* | |
* @param $name | |
* @param $params | |
* @param $loadHelpers | |
*/ | |
function attachElement($name, $params = array(), $loadHelpers = false) { | |
/* @var $view ThemeView */ | |
$view =& ClassRegistry::getObject('view'); | |
$blockName = 'attached'; | |
if (!empty($params['blockName'])) { | |
$blockName = $params['blockName']; | |
unset($params['blockName']); | |
} | |
if (empty($this->__attachedElements[$blockName])) { | |
$this->__attachedElements[$blockName] = array(); | |
} | |
$this->__attachedElements[$blockName][] = $view->element($name, $params, $loadHelpers); | |
} | |
/** | |
* attachElementで追加したブロックを取得する | |
* | |
* @param string $blockName | |
* @param string $separeter | |
* @param array $wrap | |
*/ | |
function getAttachedElements($blockName = 'attached', $separeter = '', $wrap = array()) { | |
if (empty($this->__attachedElements[$blockName])) { | |
return; | |
} | |
$elements = $this->__attachedElements[$blockName]; | |
$wrap = Set::normalize($wrap); | |
foreach ($wrap as $tag => $options) { | |
if (empty($options)) { | |
$options = array(); | |
} | |
$elements = array_map( | |
array($this, '_wrapElement'), $elements, array_pad(array(), count($elements), $tag), array_pad(array(), count($elements), $options) | |
); | |
} | |
return join($separeter, $elements); | |
} | |
/** | |
* | |
* @param string $element | |
* @param string $tag | |
* @param array $attributes | |
*/ | |
function _wrapElement($element, $tag, $options = array()) { | |
return $this->tag($tag, $element, $options); | |
} | |
// .. snip another helper methods | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('Helper', 'View'); | |
/** | |
* for CakePHP 2.0 | |
* | |
* Copyright 2012, nojimage http://php-tips.com/ | |
* | |
* Licensed under The MIT License | |
* Redistributions of files must retain the above copyright notice. | |
* | |
* @copyright 2012 nojimage | |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | |
*/ | |
class AppHelper extends Helper { | |
// .. snip another helper methods | |
/** | |
* エレメントを読み込み、バッファリングする | |
* | |
* バッファリングしたエレメントはgetAttachedElements()で呼び出す。 | |
* | |
* @param $name | |
* @param $params | |
* @param $loadHelpers | |
*/ | |
public function attachElement($name, $params = array(), $loadHelpers = false) { | |
$blockName = 'attached'; | |
if (!empty($params['blockName'])) { | |
$blockName = $params['blockName']; | |
unset($params['blockName']); | |
} | |
if (empty($this->__attachedElements[$blockName])) { | |
$this->__attachedElements[$blockName] = array(); | |
} | |
$this->__attachedElements[$blockName][] = $this->_View->element($name, $params, $loadHelpers); | |
} | |
/** | |
* attachElementで追加したブロックを取得する | |
* | |
* @param string $blockName | |
* @param string $separeter | |
* @param array $wrap | |
*/ | |
public function getAttachedElements($blockName = 'attached', $separeter = '', $wrap = array()) { | |
if (empty($this->__attachedElements[$blockName])) { | |
return; | |
} | |
$elements = $this->__attachedElements[$blockName]; | |
$wrap = Set::normalize($wrap); | |
foreach ($wrap as $tag => $options) { | |
if (empty($options)) { | |
$options = array(); | |
} | |
$elements = array_map( | |
array($this, '_wrapElement'), $elements, array_pad(array(), count($elements), $tag), array_pad(array(), count($elements), $options) | |
); | |
} | |
return join($separeter, $elements); | |
} | |
/** | |
* | |
* @param string $element | |
* @param string $tag | |
* @param array $attributes | |
*/ | |
protected function _wrapElement($element, $tag, $options = array()) { | |
return $this->tag($tag, $element, $options); | |
} | |
// .. snip another helper methods | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
buffering in view.ctp
flush buffered content in layout.ctp
using another block name
flush buffered content in layout.ctp