Commit a178ac60 authored by webchick's avatar webchick

Issue #2212309 by joelpittet: Drupal\Core\Template and twig.engine docs,...

Issue #2212309 by joelpittet: Drupal\Core\Template and twig.engine docs, coding standards, and unused code cleanup.
parent 24789e4e
......@@ -2,12 +2,12 @@
/**
* @file
* Definition of Drupal\Core\Template\Attribute.
* Contains \Drupal\Core\Template\Attribute.
*/
namespace Drupal\Core\Template;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\String;
/**
* A class that can be used for collecting then rendering HTML attributtes.
......
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\Core\Template\AttributeArray.
* Contains \Drupal\Core\Template\AttributeArray.
*/
namespace Drupal\Core\Template;
......
......@@ -2,10 +2,11 @@
/**
* @file
* Definition of Drupal\Core\Template\AttributeBoolean.
* Contains \Drupal\Core\Template\AttributeBoolean.
*/
namespace Drupal\Core\Template;
use Drupal\Component\Utility\String;
/**
......
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\Core\Template\AttributeString.
* Contains \Drupal\Core\Template\AttributeString.
*/
namespace Drupal\Core\Template;
......
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\Core\Template\AttributeValueBase.
* Contains \Drupal\Core\Template\AttributeValueBase.
*/
namespace Drupal\Core\Template;
......
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\Core\Template\TwigEnvironment.
* Contains \Drupal\Core\Template\TwigEnvironment.
*/
namespace Drupal\Core\Template;
......
......@@ -2,9 +2,10 @@
/**
* @file
* Definition of Drupal\Core\Template\TwigExtension.
* Contains \Drupal\Core\Template\TwigExtension.
*
* This provides a Twig extension that registers various Drupal specific extensions to Twig.
* This provides a Twig extension that registers various Drupal specific
* extensions to Twig.
*
* @see \Drupal\Core\CoreServiceProvider
*/
......@@ -12,23 +13,31 @@
namespace Drupal\Core\Template;
/**
* A class for providing Twig extensions (specific Twig_NodeVisitors, filters and functions).
* A class providing Drupal Twig extensions.
*
* Specifically Twig functions, filter and node visitors.
*
* @see \Drupal\Core\CoreServiceProvider
*/
class TwigExtension extends \Twig_Extension {
/**
* {@inheritdoc}
*/
public function getFunctions() {
// @todo re-add unset => twig_unset if this is really needed
return array(
// @todo Remove URL function once http://drupal.org/node/1778610 is resolved.
new \Twig_SimpleFunction('url', 'url'),
// This function will receive a renderable array, if an array is detected.
new \Twig_SimpleFunction('render_var', 'twig_render_var'),
);
}
/**
* {@inheritdoc}
*/
public function getFilters() {
return array(
// Translation filters.
new \Twig_SimpleFilter('t', 't'),
new \Twig_SimpleFilter('trans', 't'),
// The "raw" filter is not detectable when parsing "trans" tags. To detect
......@@ -38,10 +47,15 @@ public function getFilters() {
// @see TwigNodeTrans::compileString()
new \Twig_SimpleFilter('passthrough', 'twig_raw_filter'),
new \Twig_SimpleFilter('placeholder', 'twig_raw_filter'),
// Array filters.
new \Twig_SimpleFilter('without', 'twig_without'),
);
}
/**
* {@inheritdoc}
*/
public function getNodeVisitors() {
// The node visitor is needed to wrap all variables with
// render_var -> twig_render_var() function.
......@@ -50,15 +64,20 @@ public function getNodeVisitors() {
);
}
/**
* {@inheritdoc}
*/
public function getTokenParsers() {
return array(
new TwigTransTokenParser(),
);
}
public function getName()
{
/**
* {@inheritdoc}
*/
public function getName() {
return 'drupal_core';
}
}
}
<?php
/**
* @file
* Definition of Drupal\Core\Template\TwigFunctionTokenParser.
*/
namespace Drupal\Core\Template;
/**
* A class that defines the Twig token parser for Drupal.
*
* The token parser converts a token stream created from template source
* code into an Abstract Syntax Tree (AST). The AST will later be compiled
* into PHP code usable for runtime execution of the template.
*
* @see core\vendor\twig\twig\lib\Twig\TokenParser.php
*/
class TwigFunctionTokenParser extends \Twig_TokenParser {
/**
* The name of tag. Can be 'hide' or 'show'.
*
* @var string
*/
protected $tag;
/**
* Constructor for TwigFunctionTokenParser.
*
* Locally scope variables.
*/
public function __construct($tag = 'hide') {
$this->tag = $tag;
}
/**
* Parses a token and returns a node.
*
* @param Twig_Token $token A Twig_Token instance.
*
* @return Twig_Node_Print A Twig_Node_Print instance.
*/
public function parse(\Twig_Token $token) {
$lineno = $token->getLine();
$expr = $this->parser->getExpressionParser()->parseExpression();
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
return new \Twig_Node_Print(new \Twig_Node_Expression_Function($this->tag, new \Twig_Node(array($expr)), $lineno), $lineno);
}
/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag() {
return $this->tag;
}
}
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\Core\Template\TwigNodeVisitor.
* Contains \Drupal\Core\Template\TwigNodeVisitor.
*/
namespace Drupal\Core\Template;
......@@ -10,8 +10,8 @@
/**
* Provides a Twig_NodeVisitor to change the generated parse-tree.
*
* This is used to ensure that everything that is printed is wrapped via
* twig_render_var() function so that we can write for example just {{ content }}
* This is used to ensure that everything printed is wrapped via the
* twig_render_var() function in order to just write {{ content }}
* in templates instead of having to write {{ render_var(content) }}.
*
* @see twig_render
......@@ -26,14 +26,11 @@ function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) {
}
/**
* Implements Twig_NodeVisitorInterface::leaveNode().
*
* We use this to inject a call to render_var -> twig_render_var()
* before anything is printed.
*
* @see twig_render
* {@inheritdoc}
*/
function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) {
// We use this to inject a call to render_var -> twig_render_var()
// before anything is printed.
if ($node instanceof \Twig_Node_Print) {
$class = get_class($node);
$line = $node->getLine();
......@@ -52,4 +49,5 @@ function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) {
function getPriority() {
return 1;
}
}
......@@ -122,12 +122,12 @@ function twig_render_var($arg) {
return 0;
}
// == is true also for empty arrays
// Return early for NULL and also true for empty arrays.
if ($arg == NULL) {
return NULL;
}
// Keep Twig_Markup objects intact to prepare for later autoescaping support
// Keep Twig_Markup objects intact to prepare for later autoescaping support.
if ($arg instanceOf Twig_Markup) {
return $arg;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment