From: Fabien Potencier Date: Tue, 24 Apr 2012 05:34:14 +0000 (+0200) Subject: renamed the inline tag to embed X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=fab21d8a7641349dc390f153ef62431f836d989c;p=web%2Fkonrad%2Ftwig.git renamed the inline tag to embed --- diff --git a/CHANGELOG b/CHANGELOG index a6a8d08..6a0030f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.8.0 (2012-XX-XX) - * added an inline tag + * added an embed tag * 1.7.0 (2012-XX-XX) diff --git a/doc/tags/inline.rst b/doc/tags/embed.rst similarity index 82% rename from doc/tags/inline.rst rename to doc/tags/embed.rst index 312d872..34b4b43 100644 --- a/doc/tags/inline.rst +++ b/doc/tags/embed.rst @@ -1,21 +1,21 @@ -``inline`` -========== +``embed`` +========= .. versionadded:: 1.8 - The ``inline`` tag was added in Twig 1.8. + The ``embed`` tag was added in Twig 1.8. -The ``inline`` statement allows you to inline a template instead of including -it from an external file (like with the ``include`` statement): +The ``embed`` statement allows you to embed a template instead of including it +from an external file (like with the ``include`` statement): .. code-block:: jinja - {% inline %} + {% embed %} {% extends "sidebar.twig" %} {% block content %} Some content for the sidebar {% endblock %} - {% endinline %} + {% endembed %} As it's not easy to understand in which circumstances it might come in handy, let's take an example; imagine a base template shared by many pages with a @@ -64,15 +64,14 @@ While other pages (page a, b, ...) share a different structure for the block:: │ │ └─────────────────────────────────────┘ -Without the ``inline`` tag, you have two ways to design your templates: +Without the ``embed`` tag, you have two ways to design your templates: * Create two base templates (one for 1, 2, ... blocks and another one for a, b, ... blocks) to factor out the common template code, then one template for each page that inherits from one of the base template; - * Inline the each custom page content directly into each page without any use - of external templates (you need to repeat the common code for all - templates). + * Embed each custom page content directly into each page without any use of + external templates (you need to repeat the common code for all templates). These two solutions do not scale well because they each have a major drawback: @@ -83,8 +82,8 @@ These two solutions do not scale well because they each have a major drawback: * The second solution makes you duplicate some common code from one template to another (so it fails to obey the "Don't repeat yourself" principle). -In such a situation, the ``inline`` tag fixes all these issues. The common -code can be factored out in base templates (as in solution 1), and the custom +In such a situation, the ``embed`` tag fixes all these issues. The common code +can be factored out in base templates (as in solution 1), and the custom content is kept in each page (as in solution 2): .. code-block:: jinja @@ -94,7 +93,7 @@ content is kept in each page (as in solution 2): {% extends page %} {% block base %} - {% inline %} + {% embed %} {% extends "base_A.twig" %} {% block content1 %} @@ -104,7 +103,7 @@ content is kept in each page (as in solution 2): {% block content2 %} Content 2 for page 2 {% endblock %} - {% endinline %} + {% endembed %} {% endblock %} And here is the code for ``base_A.twig``: @@ -128,21 +127,21 @@ And here is the code for ``base_A.twig``: The goal of the ``base_a.twig`` base template being to factor out the ``Some code``, ``Some other code``, and ``Yet some other code`` parts. -The ``inline`` tag can be customized with the same options (``with``, -``only``, ``ignore missing``) as the ``include`` tag: +The ``embed`` tag can be customized with the same options (``with``, ``only``, +``ignore missing``) as the ``include`` tag: .. code-block:: jinja - {% inline with {'foo': 'bar'} %} + {% embed with {'foo': 'bar'} %} ... - {% endinline %} + {% endembed %} - {% inline with {'foo': 'bar'} only %} + {% embed with {'foo': 'bar'} only %} ... - {% endinline %} + {% endembed %} - {% inline ignore missing %} + {% embed ignore missing %} ... - {% endinline %} + {% endembed %} .. seealso:: :doc:`include<../tags/include>` diff --git a/doc/tags/index.rst b/doc/tags/index.rst index 52d0205..fe0a00f 100644 --- a/doc/tags/index.rst +++ b/doc/tags/index.rst @@ -21,4 +21,4 @@ Tags flush do sandbox - inline + embed diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 0d869fe..b321e13 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -252,7 +252,7 @@ class Twig_Environment * Gets the template class associated with the given string. * * @param string $name The name for which to calculate the template class name - * @param integer $index The index for inline templates (null for main templates) + * @param integer $index The index if it is an embedded template * * @return string The template class name */ @@ -299,7 +299,7 @@ class Twig_Environment * Loads a template by name. * * @param string $name The template name - * @param integer $index The index for inline templates (null for main templates) + * @param integer $index The index if it is an embedded template * * @return Twig_TemplateInterface A template instance representing the given template name */ diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 52ed531..813261f 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -109,7 +109,7 @@ class Twig_Extension_Core extends Twig_Extension new Twig_TokenParser_Spaceless(), new Twig_TokenParser_Flush(), new Twig_TokenParser_Do(), - new Twig_TokenParser_Inline(), + new Twig_TokenParser_Embed(), ); } diff --git a/lib/Twig/Node/Inline.php b/lib/Twig/Node/Embed.php similarity index 71% rename from lib/Twig/Node/Inline.php rename to lib/Twig/Node/Embed.php index ab26b40..5edb953 100644 --- a/lib/Twig/Node/Inline.php +++ b/lib/Twig/Node/Embed.php @@ -10,20 +10,20 @@ */ /** - * Represents an include node for an inlined template. + * Represents an embed node. * * @package twig * @author Fabien Potencier */ -class Twig_Node_Inline extends Twig_Node_Include +class Twig_Node_Embed extends Twig_Node_Include { // we don't inject the module to avoid node visitors to traverse it twice (as it will be already visited in the main module) - public function __construct($filename, $inline, Twig_Node_Expression $variables = null, $only = false, $ignoreMissing = false, $lineno, $tag = null) + public function __construct($filename, $index, Twig_Node_Expression $variables = null, $only = false, $ignoreMissing = false, $lineno, $tag = null) { parent::__construct(new Twig_Node_Expression_Constant('not_used', $lineno), $variables, $only, $ignoreMissing, $lineno, $tag); $this->setAttribute('filename', $filename); - $this->setAttribute('inline', $inline); + $this->setAttribute('index', $index); } protected function addGetTemplate(Twig_Compiler $compiler) @@ -32,7 +32,7 @@ class Twig_Node_Inline extends Twig_Node_Include ->write("\$this->env->loadTemplate(") ->string($this->getAttribute('filename')) ->raw(', ') - ->string($this->getAttribute('inline')) + ->string($this->getAttribute('index')) ->raw(")") ; } diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index ba960f0..8a5b002 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -18,14 +18,14 @@ */ class Twig_Node_Module extends Twig_Node { - public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, Twig_NodeInterface $inlinedTemplates, $filename) + public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, Twig_NodeInterface $embeddedTemplates, $filename) { - parent::__construct(array('parent' => $parent, 'body' => $body, 'blocks' => $blocks, 'macros' => $macros, 'traits' => $traits, 'inlined_templates' => $inlinedTemplates), array('filename' => $filename, 'inline' => null), 1); + parent::__construct(array('parent' => $parent, 'body' => $body, 'blocks' => $blocks, 'macros' => $macros, 'traits' => $traits, 'embedded_templates' => $embeddedTemplates), array('filename' => $filename, 'index' => null), 1); } - public function setInline($index) + public function setIndex($index) { - $this->setAttribute('inline', $index); + $this->setAttribute('index', $index); } /** @@ -37,14 +37,14 @@ class Twig_Node_Module extends Twig_Node { $this->compileTemplate($compiler); - foreach ($this->getNode('inlined_templates') as $template) { + foreach ($this->getNode('embedded_templates') as $template) { $compiler->subcompile($template); } } protected function compileTemplate(Twig_Compiler $compiler) { - if (!$this->getAttribute('inline')) { + if (!$this->getAttribute('index')) { $compiler->write('write("\n\n") // if the filename contains */, add a blank to avoid a PHP parse error ->write("/* ".str_replace('*/', '* /', $this->getAttribute('filename'))." */\n") - ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->getAttribute('filename'), $this->getAttribute('inline'))) + ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->getAttribute('filename'), $this->getAttribute('index'))) ->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass())) ->write("{\n") ->indent() diff --git a/lib/Twig/Node/SandboxedModule.php b/lib/Twig/Node/SandboxedModule.php index 8a9ed31..464819a 100644 --- a/lib/Twig/Node/SandboxedModule.php +++ b/lib/Twig/Node/SandboxedModule.php @@ -24,9 +24,9 @@ class Twig_Node_SandboxedModule extends Twig_Node_Module public function __construct(Twig_Node_Module $node, array $usedFilters, array $usedTags, array $usedFunctions) { - parent::__construct($node->getNode('body'), $node->getNode('parent'), $node->getNode('blocks'), $node->getNode('macros'), $node->getNode('traits'), $node->getNode('inlined_templates'), $node->getAttribute('filename'), $node->getLine(), $node->getNodeTag()); + parent::__construct($node->getNode('body'), $node->getNode('parent'), $node->getNode('blocks'), $node->getNode('macros'), $node->getNode('traits'), $node->getNode('embedded_templates'), $node->getAttribute('filename'), $node->getLine(), $node->getNodeTag()); - $this->setAttribute('inline', $node->getAttribute('inline')); + $this->setAttribute('index', $node->getAttribute('index')); $this->usedFilters = $usedFilters; $this->usedTags = $usedTags; diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 7120ea4..cb8dab3 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -32,7 +32,7 @@ class Twig_Parser implements Twig_ParserInterface protected $importedFunctions; protected $tmpVarCount; protected $traits; - protected $inlinedTemplates = array(); + protected $embeddedTemplates = array(); /** * Constructor. @@ -109,7 +109,7 @@ class Twig_Parser implements Twig_ParserInterface throw $e; } - $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), new Twig_Node($this->inlinedTemplates), $this->stream->getFilename()); + $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), new Twig_Node($this->embeddedTemplates), $this->stream->getFilename()); $traverser = new Twig_NodeTraverser($this->env, $this->visitors); @@ -272,11 +272,11 @@ class Twig_Parser implements Twig_ParserInterface return count($this->traits) > 0; } - public function addInlinedTemplate(Twig_Node_Module $template) + public function embedTemplate(Twig_Node_Module $template) { - $template->setInline(count($this->inlinedTemplates) + 1); + $template->setIndex(count($this->embeddedTemplates) + 1); - $this->inlinedTemplates[] = $template; + $this->embeddedTemplates[] = $template; } public function addImportedFunction($alias, $name, Twig_Node_Expression $node) diff --git a/lib/Twig/TokenParser/Inline.php b/lib/Twig/TokenParser/Embed.php similarity index 70% rename from lib/Twig/TokenParser/Inline.php rename to lib/Twig/TokenParser/Embed.php index 9c5a0a5..4c8ba40 100644 --- a/lib/Twig/TokenParser/Inline.php +++ b/lib/Twig/TokenParser/Embed.php @@ -10,9 +10,9 @@ */ /** - * Includes an inline template. + * Embeds a template. */ -class Twig_TokenParser_Inline extends Twig_TokenParser_Include +class Twig_TokenParser_Embed extends Twig_TokenParser_Include { /** * Parses a token and returns a node. @@ -26,16 +26,16 @@ class Twig_TokenParser_Inline extends Twig_TokenParser_Include list($variables, $only, $ignoreMissing) = $this->parseArguments(); $module = $this->parser->parse($this->parser->getStream(), array($this, 'decideBlockEnd'), true); - $this->parser->addInlinedTemplate($module); + $this->parser->embedTemplate($module); $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); - return new Twig_Node_Inline($module->getAttribute('filename'), $module->getAttribute('inline'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag()); + return new Twig_Node_Embed($module->getAttribute('filename'), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag()); } public function decideBlockEnd(Twig_Token $token) { - return $token->test('endinline'); + return $token->test('endembed'); } /** @@ -45,6 +45,6 @@ class Twig_TokenParser_Inline extends Twig_TokenParser_Include */ public function getTag() { - return 'inline'; + return 'embed'; } } diff --git a/test/Twig/Tests/Fixtures/tags/inline/basic.test b/test/Twig/Tests/Fixtures/tags/embed/basic.test similarity index 89% rename from test/Twig/Tests/Fixtures/tags/inline/basic.test rename to test/Twig/Tests/Fixtures/tags/embed/basic.test index 46d28f2..78260d8 100644 --- a/test/Twig/Tests/Fixtures/tags/inline/basic.test +++ b/test/Twig/Tests/Fixtures/tags/embed/basic.test @@ -1,15 +1,15 @@ --TEST-- -"inline" tag +"embed" tag --TEMPLATE-- FOO -{% inline %} +{% embed %} {% extends "foo.twig" %} {% block c1 %} {{ parent() }} block1extended {% endblock %} -{% endinline %} +{% endembed %} BAR --TEMPLATE(foo.twig)-- diff --git a/test/Twig/Tests/Fixtures/tags/inline/with_extends.test b/test/Twig/Tests/Fixtures/tags/embed/with_extends.test similarity index 92% rename from test/Twig/Tests/Fixtures/tags/inline/with_extends.test rename to test/Twig/Tests/Fixtures/tags/embed/with_extends.test index b7509f9..04f0275 100644 --- a/test/Twig/Tests/Fixtures/tags/inline/with_extends.test +++ b/test/Twig/Tests/Fixtures/tags/embed/with_extends.test @@ -1,5 +1,5 @@ --TEST-- -"inline" tag +"embed" tag --TEMPLATE-- {% extends "base.twig" %} @@ -9,14 +9,14 @@ {% endblock %} {% block c2 %} - {% inline %} + {% embed %} {% extends "foo.twig" %} {% block c1 %} {{ parent() }} block1extended {% endblock %} - {% endinline %} + {% endembed %} {% endblock %} --TEMPLATE(base.twig)-- A