# `use`
New in version 1.1: Horizontal reuse was added in Twig 1.1.
Note
Horizontal reuse is an advanced Twig feature that is hardly ever needed inregular templates. It is mainly used by projects that need to maketemplate blocks reusable without using inheritance.
Template inheritance is one of the most powerful Twig's feature but it islimited to single inheritance; a template can only extend one other template.This limitation makes template inheritance simple to understand and easy todebug:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4</pre></div></td><td class="code"><div class="highlight"><pre>{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
</pre></div></td></tr></table>
Horizontal reuse is a way to achieve the same goal as multiple inheritance,but without the associated complexity:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6</pre></div></td><td class="code"><div class="highlight"><pre>{% extends "base.html" %}
{% use "blocks.html" %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
</pre></div></td></tr></table>
The `use` statement tells Twig to import the blocks defined in`blocks.html` into the current template (it's like macros, but for blocks):
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>{# blocks.html #}
{% block sidebar %}{% endblock %}
</pre></div></td></tr></table>
In this example, the `use` statement imports the `sidebar` block into themain template. The code is mostly equivalent to the following one (theimported blocks are not outputted automatically):
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre>{% extends "base.html" %}
{% block sidebar %}{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
</pre></div></td></tr></table>
Note
The `use` tag only imports a template if it does not extend anothertemplate, if it does not define macros, and if the body is empty. But itcan *use* other templates.
Note
Because `use` statements are resolved independently of the contextpassed to the template, the template reference cannot be an expression.
The main template can also override any imported block. If the templatealready defines the `sidebar` block, then the one defined in `blocks.html`is ignored. To avoid name conflicts, you can rename imported blocks:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7</pre></div></td><td class="code"><div class="highlight"><pre>{% extends "base.html" %}
{% use "blocks.html" with sidebar as base_sidebar, title as base_title %}
{% block sidebar %}{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
</pre></div></td></tr></table>
New in version 1.3: The `parent()` support was added in Twig 1.3.
The `parent()` function automatically determines the correct inheritancetree, so it can be used when overriding a block defined in an importedtemplate:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10</pre></div></td><td class="code"><div class="highlight"><pre>{% extends "base.html" %}
{% use "blocks.html" %}
{% block sidebar %}
{{ parent() }}
{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
</pre></div></td></tr></table>
In this example, `parent()` will correctly call the `sidebar` block fromthe `blocks.html` template.
Tip
In Twig 1.2, renaming allows you to simulate inheritance by calling the"parent" block:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7</pre></div></td><td class="code"><div class="highlight"><pre>{% extends "base.html" %}
{% use "blocks.html" with sidebar as parent_sidebar %}
{% block sidebar %}
{{ block('parent_sidebar') }}
{% endblock %}
</pre></div></td></tr></table>
Note
You can use as many `use` statements as you want in any given template.If two imported templates define the same block, the latest one wins.
- Twig
- Introduction
- Installation
- Twig for Template Designers
- Twig for Developers
- Extending Twig
- Twig Internals
- Deprecated Features
- Recipes
- Coding Standards
- Tags
- autoescape
- block
- do
- embed
- extends
- filter
- flush
- for
- from
- if
- import
- include
- macro
- sandbox
- set
- spaceless
- use
- verbatim
- Filters
- abs
- batch
- capitalize
- convert_encoding
- date
- date_modify
- default
- escape
- first
- format
- join
- json_encode
- keys
- last
- length
- lower
- merge
- nl2br
- number_format
- raw
- replace
- reverse
- round
- slice
- sort
- split
- striptags
- title
- trim
- upper
- url_encode
- Functions
- attribute
- block
- constant
- cycle
- date
- dump
- include
- max
- min
- parent
- random
- range
- source
- template_from_string
- Tests
- constant
- defined
- divisible by
- empty
- even
- iterable
- null
- odd
- same as