diff --git a/CHANGELOG.md b/CHANGELOG.md index 380adad48..35ccca504 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ * **Build 24x** (2015-04-xx) - Protected files can now be downloaded by administrators using the `fileupload` form widget. + - The `{% content %}` tag now supports passing parameters, parsed by a basic template engine (see Cms > Content block docs). * **Build 247** (2015-04-23) - Added Media Manager feature. diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index 4b0a65d65..9caa21e6b 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -28,6 +28,7 @@ use October\Rain\Exception\AjaxException; use October\Rain\Exception\SystemException; use October\Rain\Exception\ValidationException; use October\Rain\Exception\ApplicationException; +use October\Rain\Parse\Template as TextParser; use Illuminate\Http\RedirectResponse; /** @@ -894,8 +895,11 @@ class Controller /** * Renders a requested content file. * The framework uses this method internally. + * @param string $name The content view to load. + * @param array $parameters Parameter variables to pass to the view. + * @return string */ - public function renderContent($name) + public function renderContent($name, $parameters = []) { /* * Extensibility @@ -915,6 +919,13 @@ class Controller $fileContent = $content->parsedMarkup; + /* + * Parse basic template variables + */ + if (!empty($parameters)) { + $fileContent = TextParser::parse($fileContent, $parameters); + } + /* * Extensibility */ diff --git a/modules/cms/twig/ContentNode.php b/modules/cms/twig/ContentNode.php index b64a75a7b..5413e35ca 100644 --- a/modules/cms/twig/ContentNode.php +++ b/modules/cms/twig/ContentNode.php @@ -1,8 +1,8 @@ $name], [], $lineno, $tag); + parent::__construct(['nodes' => $nodes], ['names' => $paramNames], $lineno, $tag); } /** @@ -24,11 +24,23 @@ class ContentNode extends Twig_Node */ public function compile(Twig_Compiler $compiler) { + $compiler->addDebugInfo($this); + + $compiler->write("\$context['__cms_content_params'] = [];\n"); + + for ($i = 1; $i < count($this->getNode('nodes')); $i++) { + $compiler->write("\$context['__cms_content_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')->contentFunction(") - ->subcompile($this->getNode('name')) + ->subcompile($this->getNode('nodes')->getNode(0)) + ->write(", \$context['__cms_content_params']") ->write(");\n") ; + + $compiler->write("unset(\$context['__cms_content_params']);\n"); } } diff --git a/modules/cms/twig/ContentTokenParser.php b/modules/cms/twig/ContentTokenParser.php index ee9973f65..a71128db0 100644 --- a/modules/cms/twig/ContentTokenParser.php +++ b/modules/cms/twig/ContentTokenParser.php @@ -1,13 +1,19 @@ * {% content "intro.htm" %} + * + * {% content "intro.md" name='John' %} + * + * {% content "intro/txt" name='John', year=2013 %} * * * @package october\cms @@ -24,10 +30,39 @@ class ContentTokenParser 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 ContentNode($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 content tag. Line %s', $lineno), + $stream->getCurrent()->getLine(), + $stream->getFilename() + ); + break; + } + } + + return new ContentNode(new Twig_Node($nodes), $paramNames, $token->getLine(), $this->getTag()); } /** diff --git a/modules/cms/twig/Extension.php b/modules/cms/twig/Extension.php index f04395630..1c20a66e3 100644 --- a/modules/cms/twig/Extension.php +++ b/modules/cms/twig/Extension.php @@ -118,11 +118,13 @@ class Extension extends Twig_Extension /** * Renders a content file. + * @param string $name Specifies the content block name. + * @param array $parameters A optional list of parameters to pass to the content. * @return string Returns the file contents. */ - public function contentFunction($name) + public function contentFunction($name, $parameters = []) { - return $this->controller->renderContent($name); + return $this->controller->renderContent($name, $parameters); } /**