mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-31 20:47:28 +00:00
renamed the inline tag to embed
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
* 1.8.0 (2012-XX-XX)
|
||||
|
||||
* added an inline tag
|
||||
* added an embed tag
|
||||
|
||||
* 1.7.0 (2012-XX-XX)
|
||||
|
||||
|
||||
@@ -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>`
|
||||
+1
-1
@@ -21,4 +21,4 @@ Tags
|
||||
flush
|
||||
do
|
||||
sandbox
|
||||
inline
|
||||
embed
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,20 +10,20 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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)
|
||||
@@ -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(")")
|
||||
;
|
||||
}
|
||||
@@ -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('<?php');
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ class Twig_Node_Module extends Twig_Node
|
||||
->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()
|
||||
|
||||
@@ -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;
|
||||
|
||||
+5
-5
@@ -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)
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -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)--
|
||||
+3
-3
@@ -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
|
||||
Reference in New Issue
Block a user