diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php index 8f4f66c..ec27d7e 100644 --- a/core/lib/Drupal/Core/CoreServiceProvider.php +++ b/core/lib/Drupal/Core/CoreServiceProvider.php @@ -125,7 +125,6 @@ public static function registerTwig(ContainerBuilder $container) { // When in the installer, twig_cache must be FALSE until we know the // files folder is writable. 'cache' => drupal_installation_attempted() ? FALSE : settings()->get('twig_cache', TRUE), - 'base_template_class' => 'Drupal\Core\Template\TwigTemplate', // @todo Remove in followup issue // @see http://drupal.org/node/1712444. 'autoescape' => FALSE, diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 9cd17b0..636816b 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -4,7 +4,8 @@ * @file * Definition of 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,37 +13,51 @@ namespace Drupal\Core\Template; /** - * A class for providing Twig extensions (specific Twig_NodeVisitors, filters and functions). + * A class for providing Twig extension. + * + * Specifically twig node visitors, filters and functions. * * @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. - 'url' => new \Twig_Function_Function('url'), - // These functions will receive a TwigReference object, if a render array is detected - 'hide' => new TwigReferenceFunction('twig_hide'), - 'render_var' => new TwigReferenceFunction('twig_render_var'), - 'show' => new TwigReferenceFunction('twig_show'), + // @todo Remove URL function once http://drupal.org/node/1778610 + // is resolved. + 'url' => new \Twig_SimpleFunction('url', 'url'), + // This function will receive a renderable array, if an array is detected. + 'render_var' => new \Twig_SimpleFunction('render_var', 'twig_render_var'), ); } + /** + * {@inheritdoc} + */ public function getFilters() { return array( - 't' => new \Twig_Filter_Function('t'), - 'trans' => new \Twig_Filter_Function('t'), + // Translation filters. + new \Twig_SimpleFilter('t', 't'), + new \Twig_SimpleFilter('trans', 't'), // The "raw" filter is not detectable when parsing "trans" tags. To detect // which prefix must be used for translation (@, !, %), we must clone the // "raw" filter and give it identifiable names. These filters should only // be used in "trans" tags. // @see TwigNodeTrans::compileString() - 'passthrough' => new \Twig_Filter_Function('twig_raw_filter'), - 'placeholder' => new \Twig_Filter_Function('twig_raw_filter'), + 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. @@ -51,17 +66,19 @@ public function getNodeVisitors() { ); } + /** + * {@inheritdoc} + */ public function getTokenParsers() { return array( - new TwigFunctionTokenParser('hide'), - new TwigFunctionTokenParser('show'), new TwigTransTokenParser(), ); } - public function getName() - { + /** + * {@inheritdoc} + */ + public function getName() { return 'drupal_core'; } } - diff --git a/core/lib/Drupal/Core/Template/TwigNodeExpressionNameReference.php b/core/lib/Drupal/Core/Template/TwigNodeExpressionNameReference.php index c66d73f..aa01bbb 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeExpressionNameReference.php +++ b/core/lib/Drupal/Core/Template/TwigNodeExpressionNameReference.php @@ -21,7 +21,7 @@ class TwigNodeExpressionNameReference extends \Twig_Node_Expression_Name { public function compile(\Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $compiler - ->raw('$this->getContextReference($context, ') + ->raw('twig_render_var($context, ') ->string($name) ->raw(')'); } diff --git a/core/lib/Drupal/Core/Template/TwigNodeVisitor.php b/core/lib/Drupal/Core/Template/TwigNodeVisitor.php index 710faa0..75f1dce 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeVisitor.php +++ b/core/lib/Drupal/Core/Template/TwigNodeVisitor.php @@ -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 @@ -19,74 +19,32 @@ class TwigNodeVisitor implements \Twig_NodeVisitorInterface { /** - * TRUE when this node is a function getting arguments by reference. - * - * For example: 'hide' or 'render' are such functions. - * - * @var bool - */ - protected $isReference = FALSE; - - /** - * Implements Twig_NodeVisitorInterface::enterNode(). + * {@inheritdoc} */ function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) { - if ($node instanceof \Twig_Node_Expression_Function) { - $name = $node->getAttribute('name'); - $func = $env->getFunction($name); - - // Optimization: Do not support nested functions. - if ($this->isReference && $func instanceof \Twig_Function_Function) { - $this->isReference = FALSE; - } - if ($func instanceof TwigReferenceFunction) { - // We need to create a TwigReference - $this->isReference = TRUE; - } - } - if ($node instanceof \Twig_Node_Print) { - // Our injected render_var needs arguments passed by reference -- in case of render array - $this->isReference = TRUE; - } - return $node; } /** - * 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) { - $this->isReference = FALSE; - $class = get_class($node); + $line = $node->getLine(); return new $class( - new \Twig_Node_Expression_Function('render_var', new \Twig_Node(array($node->getNode('expr'))), $node->getLine()), + new \Twig_Node_Expression_Function('render_var', new \Twig_Node(array($node->getNode('expr'))), $line), $node->getLine() ); } - if ($this->isReference) { - if ($node instanceof \Twig_Node_Expression_Name) { - $name = $node->getAttribute('name'); - return new TwigNodeExpressionNameReference($name, $node->getLine()); - } - elseif ($node instanceof \Twig_Function_Function) { - // Do something! - $this->isReference = FALSE; - } - } - return $node; } /** - * Implements Twig_NodeVisitorInterface::getPriority(). + * {@inheritdoc} */ function getPriority() { // We want to run before other NodeVisitors like Escape or Optimizer diff --git a/core/lib/Drupal/Core/Template/TwigReference.php b/core/lib/Drupal/Core/Template/TwigReference.php deleted file mode 100644 index 0b8b506..0000000 --- a/core/lib/Drupal/Core/Template/TwigReference.php +++ /dev/null @@ -1,131 +0,0 @@ -setReference($variable); - * @endcode - * - * When a TwigReference is accessed via the offsetGet method the resulting - * reference is again wrapped within a TwigReference. Therefore references to - * render arrays within render arrays are also retained. - * - * To unwrap TwigReference objects the reference can be retrieved out of the - * object by calling the getReference() method like this: - * @code - * $variable = &$obj->getReference(); - * @endcode - * This allows render(), hide() and show() to access the original variable and - * change it. The process of unwrapping and passing by reference to this - * functions is done transparently by the TwigReferenceFunctions helper class. - * - * @see TwigReferenceFunction - * @see TwigReferenceFunctions - */ -class TwigReference extends \ArrayObject { - - /** - * Holds an internal reference to the original array. - * - * @var array - */ - protected $writableRef = array(); - - /** - * Constructs a \Drupal\Core\Template\TwigReference object. - * - * The argument to the constructor is ignored as it is not safe that this will - * always be a reference. - * - * To set a reference use: - * @code - * $obj = new TwigReference(); - * $obj->setReference($variable); - * @endcode - * - * @param $array - * The array parameter is ignored and not passed to the parent - */ - public function __construct($array = NULL) { - parent::__construct(); - } - - /** - * Sets a reference in the internal storage. - * - * @param $array - * The array to set as internal reference. - */ - public function setReference(&$array) { - $this->exchangeArray($array); - $this->writableRef = &$array; - } - - /** - * Gets a reference to the internal storage. - * - * Should be called like: - * @code - * $reference = &$obj->getReference(); - * @endcode - * - * @return - * Returns the stored internal reference. - */ - public function &getReference() { - return $this->writableRef; - } - - /** - * Sets offset in internal reference and internal storage to value. - * - * This is just for completeness, but should never be used, because - * twig cannot set properties and should not. - * - * @link http://php.net/manual/en/arrayaccess.offsetset.php - * @param mixed $offset - * The offset to assign the value to. - * @param mixed $value - * The value to set. - */ - public function offsetSet($offset, $value) { - $this->writableRef[$offset] = $value; - parent::offsetSet($offset, $value); - } - - /** - * Retrieves offset from internal reference. - * - * In case of a render array, it is wrapped again within a TwigReference - * object. - * - * @param mixed $offset - * The offset to retrieve. - * - * @return mixed - * Returns a TwigReference object wrapping the array if the retrieved offset - * is a complex array (i.e. not an attribute). Else it returns the retrived - * offset directly. - */ - public function offsetGet($offset) { - if (!is_array($this->writableRef[$offset]) || $offset[0] == '#') { - return $this->writableRef[$offset]; - } - - // Wrap the returned array in a new TwigReference. - $x = clone $this; // clone is faster than new - $x->setReference($this->writableRef[$offset]); - return $x; - } -} diff --git a/core/lib/Drupal/Core/Template/TwigReferenceFunction.php b/core/lib/Drupal/Core/Template/TwigReferenceFunction.php deleted file mode 100644 index 6ca4cbd..0000000 --- a/core/lib/Drupal/Core/Template/TwigReferenceFunction.php +++ /dev/null @@ -1,14 +0,0 @@ -setReference($content); - * return $obj; - * } - * - * // [...] - * // Simplified, generated twig code - * $_content_ = getContextReference($content); - * - * Drupal\Core\Template\TwigReferenceFunctions::hide( - * getAttribute($_content_, 'links') - * ); - * @endcode - * A TwigReference object is passed to the __callStatic function of - * TwigReferenceFunctions. The method unwraps the TwigReference and calls the - * hide() method essentially with a reference to $content['links']. - * - * Therefore the hidden property is correctly set and a successive call to - * render() will not render the content twice. - * - * @see TwigReference - * @see TwigReferenceFunction - * @see TwigFactory - * - */ -class TwigReferenceFunctions { - - /** - * Magic function to call functions called from twig templates with a - * reference to the original variable. - * - * This checks if the array provided by value is containing a reference to - * the original version. If yes it replaces the argument with its reference. - * - * @param $name - * The name of the function to call. - * @param $arguments - * The arguments to process and pass to the called function. - * - * @return mixed - * Returns the output of the called function. - * - * @see TwigReference - */ - public static function __callStatic($name, $arguments) { - foreach ($arguments as $key => $val) { - if (is_object($val) && $val instanceof TwigReference) { - $arguments[$key] = &$val->getReference(); - } - } - - // Needed to pass by reference -- could also restrict to maximum one - // argument instead - $args = array(); - foreach ($arguments as $key => &$arg) { - $args[$key] = &$arg; - } - - return call_user_func_array($name, $args); - } -} diff --git a/core/lib/Drupal/Core/Template/TwigTemplate.php b/core/lib/Drupal/Core/Template/TwigTemplate.php deleted file mode 100644 index cfa0e15..0000000 --- a/core/lib/Drupal/Core/Template/TwigTemplate.php +++ /dev/null @@ -1,89 +0,0 @@ - $item))); - trigger_error($msg->getMessage(), E_USER_WARNING); - return NULL; - } - - // Return item instead of its reference inside a loop. - // @todo 'hide' and 'show' are not supported inside a loop for now. - // This should be a non-issue as soon as this lands: - // @see http://drupal.org/node/1922304 - if (isset($context['_seq'])) { - return $context[$item]; - } - - // The first test also finds empty / null render arrays - if (!$context[$item] || isset($this->is_no_reference[$item])) { - return $context[$item]; - } - - if (isset($context['_references'][$item])) { - return $context['_references'][$item]; - } - - // @todo Check if this is a render array (existence of #theme?) - if ((!isset($this->is_reference[$item])) && ($context[$item] instanceof \TwigMarkup || !is_array($context[$item]))) { - $this->is_no_reference[$item] = TRUE; - return $context[$item]; - } - - if ($this->twig_reference == NULL) { - $this->twig_reference = new TwigReference(); - } - $ref = clone $this->twig_reference; // clone is _much_ faster than new - $ref->setReference($context[$item]); - - // Save that this is a reference - $context['_references'][$item] = $ref; - $this->is_reference[$item] = TRUE; - - return $ref; - } -} diff --git a/core/modules/block/templates/block-list.html.twig b/core/modules/block/templates/block-list.html.twig index 873d192..474c654 100644 --- a/core/modules/block/templates/block-list.html.twig +++ b/core/modules/block/templates/block-list.html.twig @@ -13,10 +13,9 @@ * @ingroup themeable */ #} -{% hide(form.place_blocks) %}
- {{ form }} + {{ form|without('place_blocks') }}
{{ form.place_blocks }} diff --git a/core/modules/comment/templates/comment.html.twig b/core/modules/comment/templates/comment.html.twig index 570448f..e8c5bb7 100644 --- a/core/modules/comment/templates/comment.html.twig +++ b/core/modules/comment/templates/comment.html.twig @@ -7,8 +7,11 @@ * - author: Comment author. Can be a link or plain text. * - content: The content-related items for the comment display. Use * {{ content }} to print them all, or print a subset such as - * {{ content.field_example }}. Use hide(content.field_example) to temporarily - * suppress the printing of a given element. + * {{ content.field_example }}. Use the following code to temporarily suppress + * the printing of a given child element: + * @code + * {{ content|without('field_example') }} + * @endcode * - created: Formatted date and time for when the comment was created. * Preprocess functions can reformat it by calling format_date() with the * desired parameters on the 'comment.created' variable. @@ -93,8 +96,7 @@ {# We hide the links now so that we can render them later. #} - {% hide(content.links) %} - {{ content }} + {{ content|without('links') }} {% if signature %}
diff --git a/core/modules/node/templates/node-edit-form.html.twig b/core/modules/node/templates/node-edit-form.html.twig index eee9ecc..c208cf5 100644 --- a/core/modules/node/templates/node-edit-form.html.twig +++ b/core/modules/node/templates/node-edit-form.html.twig @@ -15,11 +15,9 @@ * @ingroup themeable */ #} -{% hide(form.advanced) %} -{% hide(form.actions) %}
- {{ form }} + {{ form|without('advanced', 'actions') }}
{{ form.advanced }} diff --git a/core/modules/node/templates/node.html.twig b/core/modules/node/templates/node.html.twig index 1f8c823..55d5589 100644 --- a/core/modules/node/templates/node.html.twig +++ b/core/modules/node/templates/node.html.twig @@ -18,8 +18,8 @@ * - label: The title of the node. * - content: All node items. Use {{ content }} to print them all, * or print a subset such as {{ content.field_example }}. Use - * {% hide(content.field_example) %} to temporarily suppress the printing - * of a given element. + * {{ content|without('field_example') %} to temporarily suppress the printing + * of a given child element. * - user_picture: The node author's picture from user-picture.html.twig. * - date: Formatted creation date. Preprocess functions can reformat it by * calling format_date() with the desired parameters on @@ -94,8 +94,7 @@ {# We hide links now so that we can render them later. #} - {% hide(content.links) %} - {{ content }} + {{ content|without('links') }}
{{ content.links }} diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigFilterTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigFilterTest.php new file mode 100644 index 0000000..a80598c --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigFilterTest.php @@ -0,0 +1,85 @@ + 'Twig Filters', + 'description' => 'Test Drupal\'s Twig filters.', + 'group' => 'Theme', + ); + } + + /** + * Test Twig "without" filter. + */ + public function testTwigWithoutFilter() { + $this->drupalGet('/twig-theme-test/filter'); + + $elements = array( + array( + 'expected' => '
No author: You can only find truth with logic if you have already found truth without it.1874-1936.
', + 'message' => '"No author" was successfully rendered.', + ), + array( + 'expected' => '
Complete quote after without: You can only find truth with logic if you have already found truth without it.Gilbert Keith Chesterton1874-1936.
', + 'message' => '"Complete quote after without" was successfully rendered.', + ), + array( + 'expected' => '
Only author: Gilbert Keith Chesterton.
', + 'message' => '"Only author:" was successfully rendered.', + ), + array( + 'expected' => '
No author or date: You can only find truth with logic if you have already found truth without it..
', + 'message' => '"No author or date" was successfully rendered.', + ), + array( + 'expected' => '
Only date: 1874-1936.
', + 'message' => '"Only date" was successfully rendered.', + ), + array( + 'expected' => '
Complete quote again for good measure: You can only find truth with logic if you have already found truth without it.Gilbert Keith Chesterton1874-1936.
', + 'message' => '"Complete quote again for good measure" was successfully rendered.', + ), + array( + 'expected' => '
Marked-up: +
+

You can only find truth with logic if you have already found truth without it.

+ +
', + 'message' => '"Marked-up quote" was successfully rendered.', + ), + ); + + foreach ($elements as $element) { + $this->assertRaw($element['expected'], $element['message']); + } + } + +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigReferenceObjectTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigReferenceObjectTest.php deleted file mode 100644 index 88a4d22..0000000 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigReferenceObjectTest.php +++ /dev/null @@ -1,18 +0,0 @@ -nid = $nid; - $this->title = $title; - } -} diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigReferenceUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigReferenceUnitTest.php deleted file mode 100644 index 0f9534e..0000000 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigReferenceUnitTest.php +++ /dev/null @@ -1,147 +0,0 @@ - 'Theme Twig References', - 'description' => 'Tests TwigReference functions', - 'group' => 'Theme', - ); - } - - function setUp() { - parent::setUp(); - $this->variables = array( - 'foo' => 'bar', - 'baz' => array( - 'foo' => '42', - 'bar' => '23', - ), - 'node' => new TwigReferenceObjectTest( - 42, - 'test node' - ) - ); - } - - /** - * Test function for TwigReference class - */ - function testTwigReference() { - // Create a new TwigReference wrapper - $wrapper = new TwigReference(); - $wrapper->setReference($this->variables); - - // Check that strings are returned as strings - $foo = $wrapper['foo']; - $this->assertEqual($foo, $this->variables['foo'], 'String returned from TwigReference is the same'); - $this->assertTrue(is_string($foo), 'String returned from TwigReference is of type string'); - - // Check that arrays are wrapped again as TwigReference objects - $baz = $wrapper['baz']; - $this->assertTrue(is_object($baz), 'Array returned from TwigReference is of type object'); - $this->assertTrue($baz instanceof TwigReference, 'Array returned from TwigReference is instance of TwigReference'); - - // Check that getReference is giving back a reference to the original array - - $ref = &$baz->getReference(); - $this->assertTrue(is_array($ref), 'getReference returns an array'); - - // Now modify $ref - $ref['#hidden'] = TRUE; - $this->assertEqual($ref['#hidden'], $this->variables['baz']['#hidden'], 'Property set on reference is passed to original array.'); - $this->assertEqual($ref['#hidden'], $baz['#hidden'], 'Property set on reference is passed to wrapper.'); - - // Now modify $baz - $baz['hi'] = 'hello'; - - $this->assertEqual($baz['hi'], $this->variables['baz']['hi'], 'Property set on TwigReference object is passed to original array.'); - $this->assertEqual($baz['hi'], $ref['hi'], 'Property set on TwigReference object is passed to reference.'); - - // Check that an object is passed through directly - $node = $wrapper['node']; - $this->assertTrue(is_object($node), 'Object returned from TwigReference is of type object'); - $this->assertTrue($node instanceof TwigReferenceObjectTest, 'Object returned from TwigReference is instance of TwigReferenceObjectTest'); - } - - /** - * Test function for TwigReferenceFunctions class - */ - function testTwigReferenceFunctions() { - - // Create wrapper - $content = &$this->variables; - - // Use twig nomenclature - $context['content'] = $content; - - // Twig converts {{ hide(content.baz) }} to the following code - - // This will have failed, because getAttribute returns a value and not a reference - - try { - if (isset($context["content"])) { - $_content_ = $context["content"]; - } - else { - $_content_ = NULL; - } - TwigReferenceFunctions::hide($this->getAttribute($_content_, "baz")); - } - catch (Exception $e) { - // Catch the critical warning that a value was passed by reference - } - $this->assertFalse(isset($content['baz']['#printed']), 'baz is not hidden in content after hide() via value'); - - // Now lets do the same with some TwigReference magic! - - $content_wrapper = new TwigReference(); - $content_wrapper->setReference($content); - $context['content'] = $content_wrapper; - - // Twig converts {{ hide(content.baz) }} to the following code - - // This will succeed, because getAttribute returns a value, but it is an object - - if (isset($context["content"])) { - $_content_ = $context["content"]; - } - else { - $_content_ = NULL; - } - TwigReferenceFunctions::hide($this->getAttribute($_content_, "baz")); - - $this->assertTrue(isset($content['baz']['#printed']), 'baz is hidden in content after hide() via TwigReference object'); - - $type = TwigReferenceFunctions::gettype($this->getAttribute($_content_, "baz")); - $this->assertEqual($type, 'array', 'Type returned via TwigReferenceFunctions:: is an array.'); - - $type = gettype($this->getAttribute($_content_, "baz")); - $this->assertEqual($type, 'object', 'Type returned without TwigReferenceFunctions:: is an object.'); - } - - /** - * Helper function to somehow simulate Twigs getAttribute function - */ - public function getAttribute($array, $offset) { - if (isset($array[$offset])) { - return $array[$offset]; - } - - return NULL; - } -} diff --git a/core/modules/system/tests/modules/twig_extension_test/lib/Drupal/twig_extension_test/TwigExtension/TestExtension.php b/core/modules/system/tests/modules/twig_extension_test/lib/Drupal/twig_extension_test/TwigExtension/TestExtension.php index 93da633..3c2b851 100644 --- a/core/modules/system/tests/modules/twig_extension_test/lib/Drupal/twig_extension_test/TwigExtension/TestExtension.php +++ b/core/modules/system/tests/modules/twig_extension_test/lib/Drupal/twig_extension_test/TwigExtension/TestExtension.php @@ -8,7 +8,6 @@ namespace Drupal\twig_extension_test\TwigExtension; use Drupal\Core\Template\TwigExtension; -use Drupal\Core\Template\TwigReferenceFunction; /** * A test Twig extension that adds a custom function and a custom filter. diff --git a/core/modules/system/tests/modules/twig_theme_test/lib/Drupal/twig_theme_test/TwigThemeTestController.php b/core/modules/system/tests/modules/twig_theme_test/lib/Drupal/twig_theme_test/TwigThemeTestController.php index 45654ea..ba2a718 100644 --- a/core/modules/system/tests/modules/twig_theme_test/lib/Drupal/twig_theme_test/TwigThemeTestController.php +++ b/core/modules/system/tests/modules/twig_theme_test/lib/Drupal/twig_theme_test/TwigThemeTestController.php @@ -28,4 +28,19 @@ public function transBlockRender() { ); } + /** + * Menu callback for filters in a Twig template. + */ + public function testFilterRender() { + return array( + '#theme' => 'twig_theme_test_filter', + '#quote' => array( + 'content' => array('#markup' => 'You can only find truth with logic if you have already found truth without it.'), + 'author' => array('#markup' => 'Gilbert Keith Chesterton'), + 'date' => array('#markup' => '1874-1936'), + ), + ); + } + } + diff --git a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.filter.html.twig b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.filter.html.twig new file mode 100644 index 0000000..ce4c405 --- /dev/null +++ b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.filter.html.twig @@ -0,0 +1,14 @@ +
No author: {{ quote|without('author') }}.
+
Complete quote after without: {{ quote }}.
+
Only author: {{ quote.author }}.
+
No author or date: {{ quote|without('date', 'author') }}.
+
Only date: {{ quote.date }}.
+
Complete quote again for good measure: {{ quote }}.
+
Marked-up: +
+

{{ quote.content }}

+ +
+
diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module index 6edb6f6..e47b30c 100644 --- a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module +++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module @@ -4,6 +4,10 @@ * Implements hook_theme(). */ function twig_theme_test_theme($existing, $type, $theme, $path) { + $items['twig_theme_test_filter'] = array( + 'variables' => array('quote' => array()), + 'template' => 'twig_theme_test.filter', + ); $items['twig_theme_test_php_variables'] = array( 'template' => 'twig_theme_test.php_variables', ); diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml index 96befd4..c187bc2 100644 --- a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml +++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml @@ -11,3 +11,10 @@ twig_theme_test.trans: _content: '\Drupal\twig_theme_test\TwigThemeTestController::transBlockRender' requirements: _access: 'TRUE' + +twig_theme_test.filter: + path: '/twig-theme-test/filter' + defaults: + _content: '\Drupal\twig_theme_test\TwigThemeTestController::testFilterRender' + requirements: + _access: 'TRUE' diff --git a/core/modules/taxonomy/templates/taxonomy-term.html.twig b/core/modules/taxonomy/templates/taxonomy-term.html.twig index a3986fa..2ecc713 100644 --- a/core/modules/taxonomy/templates/taxonomy-term.html.twig +++ b/core/modules/taxonomy/templates/taxonomy-term.html.twig @@ -8,10 +8,10 @@ * - name: Name of the current term. * - content: Items for the content of the term (fields and description). * Use 'content' to print them all, or print a subset such as - * 'content.description'. Use the following code to temporarily suppress the - * printing of a given element: + * 'content.description'. Use the following code to exclude the + * printing of a given child element: * @code - * {% hide(content.description) %} + * {{ content|without('description') }} * @endcode * - attributes: HTML attributes for the wrapper. The 'class' attribute * contains the following classes by default: diff --git a/core/themes/bartik/templates/comment.html.twig b/core/themes/bartik/templates/comment.html.twig index ad03eb6..f66b14b 100644 --- a/core/themes/bartik/templates/comment.html.twig +++ b/core/themes/bartik/templates/comment.html.twig @@ -7,8 +7,11 @@ * - author: Comment author. Can be a link or plain text. * - content: The content-related items for the comment display. Use * {{ content }} to print them all, or print a subset such as - * {{ content.field_example }}. Use hide(content.field_example) to temporarily - * suppress the printing of a given element. + * {{ content.field_example }}. Use the following code to temporarily suppress + * the printing of a given child element: + * @code + * {{ content|without('field_example') }} + * @endcode * - created: Formatted date and time for when the comment was created. * Preprocess functions can reformat it by calling format_date() with the * desired parameters on the 'comment.created' variable. @@ -107,8 +110,7 @@ {# We hide the links now so that we can render them later. #} - {% hide(content.links) %} - {{ content }} + {{ content|without('links') }}