Support variables parser: - Twig_Environment:addGlobal of container file
{% include '<template.html.twig>' %}
{% from '<template.html.twig>' %}
{% extends '<template.html.twig>' %}
{{ include('<template.html.twig>') }}
based on BundleInterface, addPath of container and custom path of settings
{% string '<template.html.twig>' %}
{{ your_function('<template.html.twig>') }}
all string ending with .twig provides goto
{% extends 'parent_blocks.html.twig' %}
{% block <block> %}
{% endblock %}
based on extends tag upwards
{% from 'Macro:File:file.html.twig' import <widget_panel> %}
{% from '::widgets.html.twig' import widget_panel %}
{{ widget_panel() }}
{% from '::widgets.html.twig' import widget_panel %}
{{ <widget_panel>() }}
{% import '::widgets.html.twig' as widget %}
{{ <widget.widget_panel()> }}
{% set var_name = 'Foo' %}
{{ <var_name> }}
{{ <Twig_Function_Method()> }}
{{ <Twig_Function_Node()> }}
{{ <Twig_SimpleFunction()> }}
based on Twig_ExtensionInterface and simple regular expression
{{ 'name'|<filter> }}
Note
not fully supported by PhpStorm, autocomplete dialog is a char type event
{% javascripts
'assets/jquery.min.js'
'@FooBundle/Resources/public/build-config.js'
%}
{% stylesheets
'assets/css/style.css'
'@FooBundle/Resources/public/style.css'
%}
{{ asset('assets/css/style.css') }}
{{ 'translation.key'|trans({}, '<Domain>') }}
{{ 'translation.key'|transchoice({}, 2, '<Domain>') }}
{{ '<translation.key>'|trans({}, 'Domain') }}
{{ '<translation.key>'|transchoice({}, 2, 'Domain') }}
{% trans_default_domain <Domain> %}
{{ path('_profiler') }}
{{ path('_profiler', {'parameter': '') }}
{{ controller('FooBundle:Bar:index') }}
{# @var variable_name \Foo\Bar #}
{{ variable_name.method.subMethod }}
Possible Variables Scopes
// block
{% block test %}
{# @var variable_name \Foo\Bar #}
{% endblock %}
// foreach
{% for ... %}
{# @var variable_name \Foo\Bar #}
{% endfor %}
// file
{% extends ... %}
{# @var variable_name \Foo\Bar #}
// macro
{% macro ... %}
{# @var variable_name \Foo\Bar #}
Variable parser support controller as source, just define it in root tree of twig file eg under extends tag
{# @controller FooBundle:Bar:index #}
{# @Controller FooBundle:BarFood:index #}
Controller also detect on twig path
// FooBundle/Resources/views/BarFood/index.html.twig
// FooBundle\BarFoodController\indexAction
Valid template variable detection in controller method
return $this->render('foo...', array(
'form' => $editForm->createView(),
));
// and all other template render calls ...
return $this->renderView('foo...', array(
'form' => $editForm->createView(),
));
$foo = array(
'form' => $editForm->createView();
);
$foo['form'] = $editForm->createView();
return $foo;
return $this->render('foo...', $foo);
Array values
{# entities \Foo\Bar[]
{% for entity in entities %}
{{ entity.completeMe }}
{% endfor %}
Whitelist for variable pattern
{{ "entity" }}
{% for entity in "entities" %}
{% if "entity" > "entity" %}
{% set var = "entity" %}
Include-Statement Context Variables
Pipes variable context from original include statement position
{% include 'template.html.twig' with {'foo': 'bar'} only %}
{% include 'template.html.twig' with {'foo': 'bar'} %}
{% include 'template.html.twig' %}
{% include 'template.html.twig' only %}
{% for entity in entities %}
{% include 'template.html.twig' %}
{% endfor %}
{# template.html.twig #}
{{ foo.method }}
{{ entity.method }}