How to add custom content in child template in Symfony2? - symfony

I have a index.html.twig page as my homepage with extend
{% extends "::base.html.twig" %}
pointing to app/Resources/Views base template and my that base template extends a
{% extends "UAMBootstrapBundle::stickyfooter.html.twig" %}
How I can add some custom content to get displayed on my homepage template index.html.twig? Whenever I add contents the error is that I can't any body to it if it has extends.

You get that error if forget to use the {% block %}{% endblock %}
So you need to create a block in you base.html.twig which you can 'fill' in your index.html.twig
Example in base.html.twig
{% block content %}{% endblock %}
In your index.html.twig
{% block %}My content{% endblock %}

Related

Including header in base.twig for all pages

It is something pretty simple to be done outside of twig but here I am not even sure it is possible. This is the case:
Have base.html.twig. header.html.twig, home.html.twig.
#home.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<h1>MY HTML</h1>
{% endblock %}
#base.html.twig
....some html here
{% block header%}{% endblock %}
{% block body %}{% endblock %}
....some more html here
#header.html.twig
{% extends 'base.html.twig' %}
{% block header %}
some here things that have to shown on every page through base.html.twig
{% endblock %}
I think it is pretty straight forward scenario but still my header doesn't show anywhere. As I understood from the documentation it is not working because this is how blocks work. It renders the page i am calling from my controller (home.html.twig) and the extended by it (base.html.twig). But wont call the header as well. So! How should I call the header on every page ?
To add the header on all pages just put include see example
{% block header %}
{% include 'header.html.twig' %}
{% endblock %}

Adding page specific js using timber twig

I have a twig file that extends base
{% extends "base.twig" %}
{% block content %}
<div> all articles </div>
{% for blog in blog_articles %}
<div><i> {{blog.post_title }}</i></div>
{% endfor %}
{% endblock %}
base.twig has an include for my global js and in my article.twig above i want to be able to add template specific JS that will appear below the global js that is in an include in base.twig
Can this be done?
To answer my own question i did this
in the base.twig
{% include 'globaljs.twig' %}
{% block javascript %}{% endblock %}
Then in the article.twig where i call the javascript block i just add my page scripts. simple

Rendering a block in extended -> included template

Problem
In navbar.html.twig I have a block that looks like this:
//navbar.html.twig
{% block back_link %}{% endblock %}
This navbar is included by my base.html.twig.
//base.html.twig
{% include navbar.html.twig %}
And then my page template extends the base.
//page.html.twig
{% extends base.html.twig %}
...
{% block back_link %} Things i want in the navbar. {% endblock %}
But the things I want in the navbar don't show up in the navbar, because it's included by the base, so there's no parent/child relationship there.
Question
What's a good (or any) way to let me override a block in an included template in an extended template?
if you understand your idea right, you want to have some reusable content for a navbar, separated to navbar.html.twig that will possible to use in several templates?
So you can do it with "use" http://twig.sensiolabs.org/doc/tags/use.html
In navbar.html.twig
//navbar.html.twig
{% block back_link %}{% endblock %}
In base.html.twig.
//base.html.twig
{% use '::navbar.html.twig' %}
{{ block('back_link') }}
In page.html.twig
{% extends '::base.html.twig' %}
{% block back_link %} Things i want in the navbar. {% endblock %}

extends in symfony from multiple children

I have a base.html.twig with the basic html.
In the 'base' I have a {% block body %}{% endblock %} and a {% block javascript %}{% endblock %}
I call a 'form.html.twig' file from the Controller and the template extends the base.html.twig.
The 'form' contains the <form> and </form> tags and can contain a random numbers of sub templates/form elements:
'{% block body %}
{% for template in templates %}
{% embed template %}{% endembed %}
{% endfor %}
{% endblock %}'
The 'template' is perhaps a customer.html.twig, confirm.html.twig, vehicle.html.twig etc. etc. and they all have {% block body %} and {% block javascript %} - now for the question:
The content in the template files aren't moved to the blocks body/javascript. How do I get twig/symfony to move the content in the template files to the respective areas in the 'base.html.twig'?
Thank you for your time.
You want to use the use twig tag instead of the embed tag like this
{% use template %}
See more info here:
http://twig.sensiolabs.org/doc/tags/use.html

Sonata Block - add javascript block

Is it possible to add a javascript block in a Sonata Block View like this ?
{% extends sonata_block.templates.block_base %}
{% block block %}
<!-- html -->
{% endblock %}
{% block javascripts %}
// javascript code
{% endblock %}
I couldn't make it works in this way as javascript block doesn't exist in template block base but present in my layout.

Resources