Ivan
Ivan

Reputation: 2579

Twig, get source

How I can get template with extend:

//first.html.twig
<div>
{% block test %}
{% endblock %}
</div>

And second.html.twig:

{% extend "first.html.twig" %}
{% block test %}
    {% if test = 'foo' %}
        {{ test }}
    {% endif %}
{% endblock %}

In php file:

<?php
    $load = ???('second.html.twig');
    $source = $load->getSource();
    //$source:
    //<div>
    //    {% if test = 'foo' %}
    //        {{ test }}
    //    {% endif %}
    //</div>

Upvotes: 2

Views: 946

Answers (2)

Anthony
Anthony

Reputation: 2132

As seen in the documentation you can set a layout to a template. Example:

From documentation

// {% extends layout %}

// deprecated as of Twig 1.28
$layout = $twig->loadTemplate('some_layout_template.twig');

// as of Twig 1.28
$layout = $twig->load('some_layout_template.twig');

$twig->display('template.twig', array('layout' => $layout));

Upvotes: 2

KevinTheGreat
KevinTheGreat

Reputation: 634

If I understand your question right you want to place the data from twig file #1 into #2 and then display #2 with its own content as well as that of #1.

I don't know if you are getting any errors. but the best way to do this is use the {% include %} option info on this can be found here include

Upvotes: 0

Related Questions