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.
Related
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 %}
I have this situation:
{% extends 'base.html.twig' %}
{% set isManager = (isAdmin or isAffiliateManager or app.user.isFinanceDepartment) %}
{% block content %}
[...](add some html if manager)
{% endblock %}
{% block css %}
(add some css if manager)
{% endblock %}
{% block js %}
(add some js if manager)
{% endblock %}
isAdmin and isAffiliateManager are declared in base.html.twig. I want to access isManager in all the blocks, without declaring it 3 times. Why isn't that possible?
Edit: simpler replication: https://twigfiddle.com/kcz6mn/2
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 %}
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
The structure of my Twig files looks like this:
- "skeleton_main"
- includes "skeleteon_header"
- render "block content"
- "skeleteon_header" should render "block breadcrumb"
- "partial"
- extends "skeleton_main"
- fills "block breadcrumb"
Now I can output "block breadcrumb" in "sekeleton_main" but it isn't passed to "skeleton_header". How can I access and render the block from within the included template? I tried using {% include '' with {} %} but without luck.
# skeleton_main
{% include 'header' %}
{% block content %}{% endblock %}
# header
{% block breadcrumb %}{% endblock %}
# partial
{% extends 'skeleton_main' %}
{% block breadcrumb %} Breadcrumb {% endblock %}
{% block content %} Content {% endblock %}
Maybe there's something wrong with this approach?
You are using include which does not permit overriding blocks.
Is there a reason to use include instead of extend ?
Another solution would be to use embed which does the same function as include, but permits overriding blocks at the same time:
http://twig.sensiolabs.org/doc/tags/embed.html
I think you have a wrong approch.
You should define header as a block, not as a separate template.
{# skeleton_main #}
{% block header %}
{% block breadcrumb %}{% endblock %}
{% endblock %}
{% block content %}{% endblock %}
{# partial #}
{% extends 'skeleton_main' %}
{% block breadcrumb %} Breadcrumb {% endblock %}
{% block content %} Content {% endblock %}