Add parameter support to {% content %} tags

This commit is contained in:
Samuel Georges 2015-04-25 13:58:05 +10:00
parent 87644166ad
commit ab1272bea9
5 changed files with 71 additions and 10 deletions

View File

@ -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.

View File

@ -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
*/

View File

@ -1,8 +1,8 @@
<?php namespace Cms\Twig;
use Twig_Node;
use Twig_Node_Expression;
use Twig_Compiler;
use Twig_NodeInterface;
/**
* Represents a content node
@ -12,9 +12,9 @@ use Twig_Compiler;
*/
class ContentNode extends Twig_Node
{
public function __construct(Twig_Node_Expression $name, $lineno, $tag = 'content')
public function __construct(Twig_NodeInterface $nodes, $paramNames, $lineno, $tag = 'content')
{
parent::__construct(['name' => $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");
}
}

View File

@ -1,13 +1,19 @@
<?php namespace Cms\Twig;
use Twig_Node;
use Twig_Token;
use Twig_TokenParser;
use Twig_Error_Syntax;
/**
* Parser for the {% content %} Twig tag.
*
* <pre>
* {% content "intro.htm" %}
*
* {% content "intro.md" name='John' %}
*
* {% content "intro/txt" name='John', year=2013 %}
* </pre>
*
* @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());
}
/**

View File

@ -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);
}
/**