admin管理员组文章数量:1429895
I am using a Webpack Encore in my Symfony 4 project, and when including the Twig helper for JavaScript in my base.html.twig
:
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
The JavaScript doesn't load, however, when including the exact same Twig helper in one of my templates (index.html.twig
), the JavaScript loads in.
So my question is, why does the above Twig helper work in one of my templates, but not in my base template?
I am using a Webpack Encore in my Symfony 4 project, and when including the Twig helper for JavaScript in my base.html.twig
:
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
The JavaScript doesn't load, however, when including the exact same Twig helper in one of my templates (index.html.twig
), the JavaScript loads in.
So my question is, why does the above Twig helper work in one of my templates, but not in my base template?
Share Improve this question asked May 9, 2019 at 20:43 AlexAlex 1,1093 gold badges19 silver badges38 bronze badges2 Answers
Reset to default 5What preciel said is true, using a block inside a template that extends another template will overwrite the default code inside that said block.
However there is another solution than just moving the code outside the block in the base template and that's calling the parent()
function
main.twig
{% extends 'base.twig' %}
{% block foo %}
{{ parent() }}
Bar
{% endblock %}
base.twig
{% block foo %}
Foo
{% endblock %}
output
Foo
Bar
demo
It's failing because it's inside your javascript block.
Your template extends base.html.twig
, so whatever you place within your javascript block in any template will be inside the javascript block.
But if you do the same within base.html.twig
it will just be ignored.
Just move {{ encore_entry_script_tags('app') }}
out of your javascript block.
base.html.twig
{{ encore_entry_script_tags('app') }}
{% block javascripts %}
{# nothing here, your templates will overwrite it when you extends base.html.twig #}
{% endblock %}
Remember this: If it's inside your layout, which is base.html.twig
, then don't place anything inside the {% block %}
tags. It will just be ignored.
本文标签: symfonyWebpack Encore not loading JavaScript in base templateStack Overflow
版权声明:本文标题:symfony - Webpack Encore not loading JavaScript in base template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745438920a2658357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论