Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Twig template include file in same bundle?

Tags:

twig

symfony

I'm relatively new to Symfony. I have a bundle with twig templates that are automatically loaded with annotations in a directory structure like:

src/bundle/Resources/views/Default/

One of my templates has a big chunk of code repeated a bunch of times (with a few minor changes each instance) that I think doing an include a few times formatted like this:

{% include 'form_include.html' with {'foo': 'bar'} %}

with different variables for each instance should work well. But the debugger is telling me that it's looking for the include file in

/app/Resources/

But the template is really specific to this bundle and I wouldn't want it kept elsewhere. I tried using the ../../src.... method to specify its location with no luck. Is there a way to do this?

like image 822
Dan Goodspeed Avatar asked Feb 29 '16 21:02

Dan Goodspeed


People also ask

How do you set a variable in Twig template?

This can be accomplished by creating an array of the entry years. Inside the loop that's building the array, a conditional checks if the year already exists in the array... if the year exists, a variable is set to TRUE which you can use in your conditional later down on the page. Save this answer.

How do I add a global variable in Twig?

If you are using Twig in another project, you can set your globals directly in the environment: $twig = new Twig_Environment($loader); $twig->addGlobal('myStuff', $someVariable); And then use {{ myStuff }} anywhere in your application.

How do you dump in Twig template?

In a Twig template, you can use the dump utility as a function or a tag: {% dump foo. bar %} is the way to go when the original template output shall not be modified: variables are not dumped inline, but in the web debug toolbar; on the contrary, {{ dump(foo.

What is macro in Twig file?

From the official Twig documentation: "Macros are comparable with functions in regular programming languages. They are useful to put often used HTML idioms into reusable elements to not repeat yourself."


2 Answers

You can provide a path using the bundle's name:

{% include 'YourBundleNameBundle:Default:form_include.html.twig' with {
    'foo': bar
} %}

Where each part is separated by : and:

  • YourBundleNameBundle corresponds to src/YourBundleNameBundle/Resources/views/
  • Default corresponds to the /Default directory in this folder
  • form_include.html.twig corresponds to form_include.html.twig in this folder

So, the 'YourBundleNameBundle:Default:form_include.html.twig' value will load the src/bundle/Resources/views/Default/form_include.html.twig file.

This syntax works for the different Twig functions: include, extends, etc.

It is useful for allowing templates inheritance.

like image 163
A.L Avatar answered Sep 28 '22 14:09

A.L


Have you considered a macro instead?

From: http://twig.sensiolabs.org/doc/tags/macro.html

Macros are comparable with functions in regular programming languages. They are useful to put often used HTML idioms into reusable elements to not repeat yourself.

Here is a small example of a macro that renders a form element:

{% macro input(name, value, type, size) %}
  <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
{% endmacro %}

Macros differs from native PHP functions in a few ways:

Default argument values are defined by using the default filter in the macro body; Arguments of a macro are always optional. If extra positional arguments are passed to a macro, they end up in the special varargs variable as a list of values. But as with PHP functions, macros don't have access to the current template variables.

You can pass the whole context as an argument by using the special _context variable.

Macros can be defined in any template, and need to be "imported" before being used (see the documentation for the import tag for more information):

{% import "forms.html" as forms %}

The above import call imports the "forms.html" file (which can contain only macros, or a template and some macros), and import the functions as items of the forms variable.

The macro can then be called at will:

<p>{{ forms.input('username') }}</p>
<p>{{ forms.input('password', null, 'password') }}</p>

If macros are defined and used in the same template, you can use the special _self variable to import them:

{% import _self as forms %}

<p>{{ forms.input('username') }}</p>

When you define a macro in the template where you are going to use it, you might be tempted to call the macro directly via _self.input() instead of importing it; even if seems to work, this is just a side-effect of the current implementation and it won't work anymore in Twig 2.x.

When you want to use a macro in another macro from the same file, you need to import it locally:

{% macro input(name, value, type, size) %}
  <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
{% endmacro %}

{% macro wrapped_input(name, value, type, size) %}
  {% import _self as forms %}

  <div class="field">
     {{ forms.input(name, value, type, size) }}
  </div>
{% endmacro %}
like image 41
ABM_Dan Avatar answered Sep 28 '22 15:09

ABM_Dan