Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
element buffering for CakePHP 1.3/2.0
<?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
}
<?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
}
@nojimage

This comment has been minimized.

Copy link
Owner Author

commented Mar 13, 2012

buffering in view.ctp

$this->Html->appendElement('element_name', $params);

flush buffered content in layout.ctp

echo $this->Html-> getAttachedElements();

using another block name

$this->Html->appendElement('element_first', array('blockName' => 'sidebar'));
$this->Html->appendElement('element_second', array('blockName' => 'sidebar'));

flush buffered content in layout.ctp

echo $this->Html-> getAttachedElements('sidebar');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.