* 1.8.0 (2012-XX-XX)
- * added an inline tag
+ * added an embed tag
* 1.7.0 (2012-XX-XX)
-``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
│ │
└─────────────────────────────────────┘
-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:
* 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
{% extends page %}
{% block base %}
- {% inline %}
+ {% embed %}
{% extends "base_A.twig" %}
{% block content1 %}
{% block content2 %}
Content 2 for page 2
{% endblock %}
- {% endinline %}
+ {% endembed %}
{% endblock %}
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>`
flush
do
sandbox
- inline
+ embed
* 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
*/
* 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
*/
new Twig_TokenParser_Spaceless(),
new Twig_TokenParser_Flush(),
new Twig_TokenParser_Do(),
- new Twig_TokenParser_Inline(),
+ new Twig_TokenParser_Embed(),
);
}
*/
/**
- * Represents an include node for an inlined template.
+ * Represents an embed node.
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
-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)
->write("\$this->env->loadTemplate(")
->string($this->getAttribute('filename'))
->raw(', ')
- ->string($this->getAttribute('inline'))
+ ->string($this->getAttribute('index'))
->raw(")")
;
}
*/
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);
}
/**
{
$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('<?php');
}
->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()
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;
protected $importedFunctions;
protected $tmpVarCount;
protected $traits;
- protected $inlinedTemplates = array();
+ protected $embeddedTemplates = array();
/**
* Constructor.
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);
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)
*/
/**
- * 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.
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');
}
/**
*/
public function getTag()
{
- return 'inline';
+ return 'embed';
}
}
--TEST--
-"inline" tag
+"embed" tag
--TEMPLATE--
FOO
-{% inline %}
+{% embed %}
{% extends "foo.twig" %}
{% block c1 %}
{{ parent() }}
block1extended
{% endblock %}
-{% endinline %}
+{% endembed %}
BAR
--TEMPLATE(foo.twig)--
--TEST--
-"inline" tag
+"embed" tag
--TEMPLATE--
{% extends "base.twig" %}
{% endblock %}
{% block c2 %}
- {% inline %}
+ {% embed %}
{% extends "foo.twig" %}
{% block c1 %}
{{ parent() }}
block1extended
{% endblock %}
- {% endinline %}
+ {% endembed %}
{% endblock %}
--TEMPLATE(base.twig)--
A