Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 58 additions & 22 deletions plugins/block.begin_widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,82 @@
/**
* Allows to use Yii beginWidget() and endWidget() methods in a simple way.
* There is a variable inside a block wich has 'widget' name and represent widget object
*
*
* Example:
* {begin_widget name="activeForm" foo="" bar="" otherParam="" [...]}
* {$widget->some_method_or_variable}
* {/begin_widget}
* {/begin_widget}
*
* Yii syntax:
* {begin_widget className="some.widget.path" properties=['someProperty'=>$someValue]}
* {$widget->some_method_or_variable}
* {/begin_widget}
*
* Shorted syntax:
* {begin_widget c="some.widget.path" p=['someProperty'=>$someValue]}
* {$widget->some_method_or_variable}
* {/begin_widget}
*
* @param array $params parameters
* @param string $content contents of the block
* @param Smarty_Internal_Template $template template object
* @param boolean &$repeat repeat flag
* @return string
* @return string
* @throws CException
* @author t.yacenko (thekip)
*/
function smarty_block_begin_widget($params, $content, $template, &$repeat) {
function smarty_block_begin_widget($params, $content, $template, &$repeat)
{
// aliases
$aliases = array(
'c' => 'className',
'name' => 'className',
'p' => 'properties',
);

$controller_object = $template->tpl_vars['this']->value;
foreach ($aliases as $alias => $original) {
if (array_key_exists($alias, $params) && !array_key_exists($original, $params)) {
$params[$original] = &$params[$alias];
}
}

if ($controller_object == null) {
/** @var $controller CController */
$controller = $template->getTemplateVars('this');
if (!is_object($controller) || !($controller instanceof CController)) {
throw new CException("Can't get controller object from template. Error.");
}

if ($repeat) { //tag opened
if (!isset($params['name'])) {
throw new CException("Name parameter should be specified.");
if (!isset($params['className'])) {
throw new CException("className, name or c parameter should be specified.");
}

$widgetName = $params['name'];
unset($params['name']);

//some widgets has 'name' as property. You can pass it by '_name' parameter

// transfer params to variables with default values
$className = $params['className'];
$properties = empty($params['properties']) ? array() : $params['properties'];

// unset widget input params
unset(
$params['className'], $params['properties'],
$params['c'], $params['p'], $params['name']
);

// some widgets has 'name' property. You can pass it by '_name' parameter
if (isset($params['_name'])) {
$params['name'] = $params['_name'];
$properties['name'] = $params['_name'];
unset($params['_name']);
}

$template->assign('widget', $controller_object->beginWidget($widgetName, $params, false));
} else { //tag closed
echo $content;

$controller_object->endWidget();
$template->clearAssign('widget');

// params which are left are moved into widget properties
$properties = array_merge($properties, $params);
unset($params);

$template->assign('widget', $controller->beginWidget($className, $properties));
}
else { //tag closed
echo $content;

$controller->endWidget();
$template->clearAssign('widget');
}
}
81 changes: 64 additions & 17 deletions plugins/function.widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,80 @@
* Allows to render a widget
*
* Syntax:
* {widget name="path.to.widget.Class"}
* {widget name="path.to.widget.Class" propertyName="propertyValue" anotherProperty=['arrayKey'=>'arrayValue']}
*
* Yii syntax:
* {widget className="some.widget.path" properties=['someProperty'=>$someValue] captureOutput="assignVar"}
*
* Shorted syntax:
* {widget c="some.widget.path"}
* {widget c="some.widget.path" p=['someProperty'=>$someValue]}
* {widget c="some.widget.path" p=['someProperty'=>$someValue] assign="assignVar"}
*
* Examples:
* {widget name="bootstrap.widgets.TbBreadcrumbs" links=['Library'=>'#', 'Data']}
* {widget className="zii.widgets.CListView" properties=['dataProvider'=>$dataProvider, 'itemView'=>'_view']}
* {widget c="zii.widgets.CListView" p=['dataProvider'=>$dataProvider, 'itemView'=>'_view']}
*
* @see CBaseController::widget().
*
*
* @param array $params
* @param Smarty $smarty
* @return string
* @param Smarty_Internal_Template $smarty
* @return string Widget output
* @throws CException
*/
function smarty_function_widget($params, &$smarty){

$controller_object = $smarty->tpl_vars['this']->value; //extract the controller from template vars
if ($controller_object == null) {
function smarty_function_widget($params, Smarty_Internal_Template &$smarty)
{
// aliases
$aliases = array(
'c' => 'className',
'name' => 'className',
'p' => 'properties',
'assign' => 'captureOutput',
);

foreach ($aliases as $alias => $original) {
if (array_key_exists($alias, $params) && !array_key_exists($original, $params)) {
$params[$original] = &$params[$alias];
}
}

/** @var $controller CController */
$controller = $smarty->getTemplateVars('this');
if (!is_object($controller) || !($controller instanceof CController)) {
throw new CException("Can't get controller object from template. Error.");
}
if (!isset($params['name'])) {
throw new CException("Name parameter should be specified.");

if (!isset($params['className'])) {
throw new CException("className, name or c parameter should be specified.");
}
$widgetName = $params['name'];
unset($params['name']);

//some widgets has 'name' property. You can pass it by '_name' parameter
// transfer params to variables with default values
$className = $params['className'];
$properties = empty($params['properties']) ? array() : $params['properties'];
$captureOutput = empty($params['captureOutput']) ? false : $params['captureOutput'];

// unset widget input params
unset(
$params['className'], $params['properties'], $params['captureOutput'],
$params['c'], $params['p'], $params['name'], $params['assign']
);

// some widgets has 'name' property. You can pass it by '_name' parameter
if (isset($params['_name'])) {
$params['name'] = $params['_name'];
$properties['name'] = $params['_name'];
unset($params['_name']);
}

return $controller_object->widget($widgetName, $params, true);

// params which are left are moved into widget properties
$properties = array_merge($properties, $params);
unset($params);

$output = $controller->widget($className, $properties, true);
if ($captureOutput !== FALSE && !empty($captureOutput)) {
$smarty->assign($captureOutput, $output);
}
else {
return $output;
}
}