Twig, Render duplicated if use includes in the template

2

I'm trying Twig (currently I'm noob in twig it's my first time). This is what I am doing:

main.tpl

<!DOCTYPE html>
<html>

{% include 'header.tpl' %}

    <body>

    {% include 'logo.tpl' %}

        content

    {% include 'footer.tpl' %}

    </body>
</html>

index.php

<?php
    require_once PLUGIN_PATH.'Twig/Autoloader.php';
    Twig_Autoloader::register();

    $loader = new Twig_Loader_Filesystem(VIEWS_PATH);

    $options = [
        'autoescape' => false,
        'strict_variables' => false
    ];

    $twig = new Twig_Environment($loader, $options);
    $template = $twig->loadTemplate('main.tpl);
    echo $template->render(['title' => 'myTemplate']);
?>

header.tpl

<head>
    <!-- head content -->
</head>

logo.tpl

<img src="logo.png">

Up to this point everything is fine (apparently), but in the folder of my development there is no logo.png, so I know that I will throw an error in the console, but to my surprise the error throws it twice.

If I try to reproduce another error (such as putting another image without source) it shows me the error twice as well. It's weird because the layout's html does not duplicate it but for some strange reason the error does.

Is this normal? Or what am I doing wrong?

    
asked by José Loguercio 14.08.2016 в 07:42
source

1 answer

1

To do what you say in twig , it is not done that way. I'll give you some examples:

base.html.twig

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    {% block css %}   
    {% endblock %}
</head> 
<body>
    <div class="container">
        {% block content %}
        {% endblock %}
    </div>
    {% block js %}
    {% endblock %}
</body>
</html>

content.html.twig

{% extends "base.html.twig" %}
{% block content %}
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header" style="font-size: 30px;">TEST</h1>
        </div>
    </div>
    <div class="clearfix"></div>
    <br/>
{% endblock %}
{% block js %}
    <script src="/javascripts/application.js" type="text/javascript" charset="utf-8" async defer></script>
{% endblock %}
{% block css %}
    <link rel="stylesheet" type="text/css" href="/css/style.css">
{% endblock %}

The file that shows the content, must have at the beginning of all {% extends "nombre_archivo.html.twig" %} to indicate that it depends on that file.

Then this: {% block content %} indicates where part of the base file will be placed

I hope it serves you

    
answered by 06.10.2016 в 08:48