Components have a new override method `onRender()` called before a component is rendered.
The `{% component %} tag now supports passing parameters that override the component properties when they are rendered.
Calling `addJs()` and `addCss()` in components without a starting slash (/) will now reference the component directory, instead of the theme.
This commit is contained in:
parent
d0a1cc87fb
commit
6788a32e8c
|
|
@ -1,3 +1,8 @@
|
|||
* **Build 89** (2014-05-22)
|
||||
- Components have a new override method `onRender()` called before a component is rendered.
|
||||
- The `{% component %} tag now supports passing parameters that override the component properties when they are rendered.
|
||||
- Calling `addJs()` and `addCss()` in components without a starting slash (/) will now reference the component directory, instead of the theme.
|
||||
|
||||
* **Build 87** (2014-05-21)
|
||||
- Plugins can now be disabled manually by config (see config cms.disablePlugins).
|
||||
- Plugins with missing dependancies are disabled by the system.
|
||||
|
|
|
|||
|
|
@ -87,9 +87,9 @@ class ReportContainer extends WidgetBase
|
|||
$this->addJs('js/reportcontainer.js');
|
||||
}
|
||||
|
||||
/*
|
||||
* Event handelrs
|
||||
*/
|
||||
//
|
||||
// Event handlers
|
||||
//
|
||||
|
||||
public function onUpdateWidget()
|
||||
{
|
||||
|
|
@ -97,8 +97,8 @@ class ReportContainer extends WidgetBase
|
|||
|
||||
$widget = $this->findWidgetByAlias($alias);
|
||||
$this->saveWidgetProperties($alias, $widget->setProperties(
|
||||
json_decode(Request::input('fields'), true)
|
||||
));
|
||||
json_decode(Request::input('fields'), true)
|
||||
));
|
||||
|
||||
return [
|
||||
'#'.$alias => $widget->render()
|
||||
|
|
@ -204,9 +204,9 @@ class ReportContainer extends WidgetBase
|
|||
$this->setWidgetsToUserPreferences($widgets);
|
||||
}
|
||||
|
||||
/*
|
||||
* Methods for the internal use
|
||||
*/
|
||||
//
|
||||
// Methods for the internal use
|
||||
//
|
||||
|
||||
protected function loadWidgets()
|
||||
{
|
||||
|
|
@ -286,16 +286,17 @@ class ReportContainer extends WidgetBase
|
|||
'validationPattern' => '^[0-9]+$',
|
||||
'validationMessage' => 'Please enter the widget width as a number between 1 and 10.',
|
||||
'options' => [
|
||||
1=>'1 column',
|
||||
2=>'2 columns',
|
||||
3=>'3 columns',
|
||||
4=>'4 columns',
|
||||
5=>'5 columns',
|
||||
6=>'6 columns',
|
||||
7=>'7 columns',
|
||||
8=>'8 columns',
|
||||
9=>'9 columns',
|
||||
10=>'10 columns']
|
||||
1 => '1 column',
|
||||
2 => '2 columns',
|
||||
3 => '3 columns',
|
||||
4 => '4 columns',
|
||||
5 => '5 columns',
|
||||
6 => '6 columns',
|
||||
7 => '7 columns',
|
||||
8 => '8 columns',
|
||||
9 => '9 columns',
|
||||
10 => '10 columns'
|
||||
]
|
||||
];
|
||||
$result[] = $property;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ use Lang;
|
|||
use Config;
|
||||
use Cms\Classes\CodeBase;
|
||||
use Cms\Classes\CmsException;
|
||||
use System\Traits\PropertyContainer;
|
||||
use October\Rain\Extension\Extendable;
|
||||
|
||||
/**
|
||||
|
|
@ -16,7 +15,8 @@ use October\Rain\Extension\Extendable;
|
|||
*/
|
||||
abstract class ComponentBase extends Extendable
|
||||
{
|
||||
use PropertyContainer;
|
||||
use \System\Traits\AssetMaker;
|
||||
use \System\Traits\PropertyContainer;
|
||||
|
||||
/**
|
||||
* @var string A unique identifier for this component.
|
||||
|
|
@ -69,6 +69,7 @@ abstract class ComponentBase extends Extendable
|
|||
|
||||
$className = Str::normalizeClassName(get_called_class());
|
||||
$this->dirName = strtolower(str_replace('\\', '/', $className));
|
||||
$this->assetPath = Config::get('cms.pluginsDir') . dirname(dirname($this->dirName));
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
|
@ -83,7 +84,7 @@ abstract class ComponentBase extends Extendable
|
|||
*/
|
||||
public function getPath()
|
||||
{
|
||||
return base_path() . Config::get('cms.pluginsDir') . '/' . $this->dirName;
|
||||
return base_path() . Config::get('cms.pluginsDir') . $this->dirName;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -91,6 +92,11 @@ abstract class ComponentBase extends Extendable
|
|||
*/
|
||||
public function onRun() {}
|
||||
|
||||
/**
|
||||
* Executed when this component is rendered on a page or layout.
|
||||
*/
|
||||
public function onRender() {}
|
||||
|
||||
/**
|
||||
* Dynamically handle calls into the controller instance.
|
||||
* @param string $method
|
||||
|
|
|
|||
|
|
@ -536,8 +536,14 @@ class Controller extends BaseController
|
|||
* Renders a component's default content.
|
||||
* @return string Returns the component default contents.
|
||||
*/
|
||||
public function renderComponent($name)
|
||||
public function renderComponent($name, $parameters = [])
|
||||
{
|
||||
if ($componentObj = $this->findComponentByName($name)) {
|
||||
$componentObj->setProperties(array_merge($componentObj->getProperties(), $parameters));
|
||||
if ($result = $componentObj->onRender())
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $this->renderPartial($name.'::default');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
<?php namespace Cms\Twig;
|
||||
|
||||
use Twig_Node;
|
||||
use Twig_Node_Expression;
|
||||
use Twig_Compiler;
|
||||
use Twig_NodeInterface;
|
||||
|
||||
/**
|
||||
* Represents a component node
|
||||
|
|
@ -12,9 +12,9 @@ use Twig_Compiler;
|
|||
*/
|
||||
class ComponentNode extends Twig_Node
|
||||
{
|
||||
public function __construct(Twig_Node_Expression $name, $lineno, $tag = 'component')
|
||||
public function __construct(Twig_NodeInterface $nodes, $paramNames, $lineno, $tag = 'component')
|
||||
{
|
||||
parent::__construct(['name'=>$name], [], $lineno, $tag);
|
||||
parent::__construct(['nodes' => $nodes], ['names' => $paramNames], $lineno, $tag);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -24,11 +24,23 @@ class ComponentNode extends Twig_Node
|
|||
*/
|
||||
public function compile(Twig_Compiler $compiler)
|
||||
{
|
||||
$compiler->addDebugInfo($this);
|
||||
|
||||
$compiler->write("\$context['__cms_component_params'] = [];\n");
|
||||
|
||||
for ($i = 1; $i < count($this->getNode('nodes')); $i++) {
|
||||
$compiler->write("\$context['__cms_component_params']['".$this->getAttribute('names')[$i-1]."'] = ");
|
||||
$compiler->subcompile($this->getNode('nodes')->getNode($i));
|
||||
$compiler->write(";\n");
|
||||
}
|
||||
|
||||
$compiler
|
||||
->addDebugInfo($this)
|
||||
->write("echo \$this->env->getExtension('CMS')->componentFunction(")
|
||||
->subcompile($this->getNode('name'))
|
||||
->subcompile($this->getNode('nodes')->getNode(0))
|
||||
->write(", \$context['__cms_component_params']")
|
||||
->write(");\n")
|
||||
;
|
||||
|
||||
$compiler->write("unset(\$context['__cms_component_params']);\n");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
<?php namespace Cms\Twig;
|
||||
|
||||
use Twig_Node;
|
||||
use Twig_Token;
|
||||
use Twig_TokenParser;
|
||||
use Twig_Error_Syntax;
|
||||
|
||||
/**
|
||||
* Parser for the {% component %} Twig tag.
|
||||
|
|
@ -24,10 +26,35 @@ class ComponentTokenParser extends Twig_TokenParser
|
|||
*/
|
||||
public function parse(Twig_Token $token)
|
||||
{
|
||||
$lineno = $token->getLine();
|
||||
$stream = $this->parser->getStream();
|
||||
|
||||
$name = $this->parser->getExpressionParser()->parseExpression();
|
||||
$stream->expect(Twig_Token::BLOCK_END_TYPE);
|
||||
return new ComponentNode($name, $token->getLine(), $this->getTag());
|
||||
$paramNames = [];
|
||||
$nodes = [$name];
|
||||
|
||||
$end = false;
|
||||
while (!$end) {
|
||||
$current = $stream->next();
|
||||
|
||||
switch ($current->getType()) {
|
||||
case Twig_Token::NAME_TYPE:
|
||||
$paramNames[] = $current->getValue();
|
||||
$stream->expect(Twig_Token::OPERATOR_TYPE, '=');
|
||||
$nodes[] = $this->parser->getExpressionParser()->parseExpression();
|
||||
break;
|
||||
|
||||
case Twig_Token::BLOCK_END_TYPE:
|
||||
$end = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Twig_Error_Syntax(sprintf('Invalid syntax in the partial tag. Line %s', $lineno), $stream->getCurrent()->getLine(), $stream->getFilename());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new ComponentNode(new Twig_Node($nodes), $paramNames, $token->getLine(), $this->getTag());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -167,11 +167,13 @@ class Extension extends Twig_Extension
|
|||
|
||||
/**
|
||||
* Renders a component's default content.
|
||||
* @param string $name Specifies the component name.
|
||||
* @param array $parameters A optional list of parameters to pass to the component.
|
||||
* @return string Returns the component default contents.
|
||||
*/
|
||||
public function componentFunction($name)
|
||||
public function componentFunction($name, $parameters = [])
|
||||
{
|
||||
return $this->controller->renderComponent($name);
|
||||
return $this->controller->renderComponent($name, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
<?php namespace Cms\Twig;
|
||||
|
||||
use Twig_Node;
|
||||
use Twig_Token;
|
||||
use Twig_TokenParser;
|
||||
use Twig_Node;
|
||||
use Twig_Error_Syntax;
|
||||
|
||||
/**
|
||||
|
|
@ -52,9 +52,8 @@ class PartialTokenParser extends Twig_TokenParser
|
|||
$end = true;
|
||||
break;
|
||||
|
||||
default :
|
||||
default:
|
||||
throw new Twig_Error_Syntax(sprintf('Invalid syntax in the partial tag. Line %s', $lineno), $stream->getCurrent()->getLine(), $stream->getFilename());
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ trait PropertyContainer
|
|||
}
|
||||
|
||||
/**
|
||||
* Defines the properties used by this class.
|
||||
* Defines the properties used by this class.
|
||||
* This method should be used as an override in the extended class.
|
||||
*/
|
||||
public function defineProperties()
|
||||
|
|
|
|||
Loading…
Reference in New Issue