Symfony2 Assetic + Twig 模板 JavaScript 继承

发布于 2024-12-25 10:27:41 字数 2161 浏览 0 评论 0 原文

我的问题:

我有 3 个模板:

  • main.html.twig (主布局文件)
  • layout.html.twig (捆绑包特定的布局覆盖,其中包含一些捆绑包特定的 JS 标签)
  • create.html.twig(页面特定的模板文件,还包含一些页面特定的 JS 标签)

我在我的基本布局中定义一个名为“javascript”的块(main.html.twig ),然后覆盖它(但是在 layout.html.twig 中调用 {{parent() }} 。这工作正常,并且主模板文件中的 JS 标记仍然包含在 然后,我在 create.html.twig文件

中执行相同的操作,按如下方式重写该块:

{% block javascripts %}
    {{ parent() }}
    {% javascripts '@BundleName/Resources/public/js/application.album.uploader.js'
                   '@BundleName/Resources/public/js/jquery.uploadify.js'
                   '@BundleName/Resources/public/js/swfuploadify.js' filter='?yui_js' %}
         <script src='{{ asset_url }}' type='text/javascript'></script>
    {% endjavascripts %}
{% endblock %}

此时,而不是仅仅重写父级中的 javascript 块(layout.html.twig) 并包括其上方模板中定义的所有脚本,它执行以下操作:

  • 转储
  • 然后它还会转储脚本和其他人一起出去(因为我 我不知道

是什么导致脚本被转储到 create.html.twig 模板的中间,而且我也很困惑为什么它们被转储到屏幕两次(一次在创建的中间,然后一次在底部以及来自 main.html.twiglayout.html.twig 的所有其余脚本代码>.

有人有什么想法吗?如果有任何不清楚的地方或者我是否可以提供更多信息,请告诉我。

编辑:

文件内容如下...

main.html.twig: https://gist.github.com/7f29353eaca0947528ce< /a>

布局.html.twig: https://gist.github.com/734947e9118b7765715e

create.html.twig: https://gist.github.com/c60c8d5c61e00ff86912

编辑2:

今天早上我再次查看了这个问题,看起来它对样式表做了同样的事情。我尝试在 layout.html.twig 中定义一个名为 pagescripts 的新块,然后在我的 create.html.twig 中使用该块,但是这有相同的结果,它似乎只是在我使用的地方转储脚本和样式表

{% block pagescripts %} 
   (scripts here) 
{% endblock}

My problem:

I have 3 templates:

  • main.html.twig (main layout file)
  • layout.html.twig (a bundle specific layout override which contains some bundle specific JS tags)
  • create.html.twig (a page specific template file which also contains some page specific JS tags)

I am defining a block called 'javascript' in my base layout (main.html.twig), then overriding it (but calling {{ parent() }} in layout.html.twig. This works fine, and the JS tags from the main template file are still included above those in the layout.html.twig template.

I then do the same in the create.html.twig file, overriding the block as follows:

{% block javascripts %}
    {{ parent() }}
    {% javascripts '@BundleName/Resources/public/js/application.album.uploader.js'
                   '@BundleName/Resources/public/js/jquery.uploadify.js'
                   '@BundleName/Resources/public/js/swfuploadify.js' filter='?yui_js' %}
         <script src='{{ asset_url }}' type='text/javascript'></script>
    {% endjavascripts %}
{% endblock %}

At this point, instead of just overriding the javascript block in the parent (layout.html.twig) and including all the scripts defined in the templates above it, it does the following:

  • Dumps the <script> tags in the middle of the output (which causes an error, because in my main.html.twig file I am only including the jQuery library at the end of the HTML markup
  • Then it also dumps the scripts out along with the rest of the others (as I would expect it to)

I am not sure what is causing the scripts to be dumped in the middle of the create.html.twig template, and I'm also confused as to why they're being dumped to the screen twice (once in the middle of the create and then once at the bottom along with all the rest of my scripts from main.html.twig and layout.html.twig.

Has anyone got any ideas? Let me know if anything is unclear or if I can provide some more information.

EDIT:

File contents are below...

main.html.twig: https://gist.github.com/7f29353eaca0947528ce

layout.html.twig: https://gist.github.com/734947e9118b7765715e

create.html.twig: https://gist.github.com/c60c8d5c61e00ff86912

EDIT 2:

I've been having another look at the issue this morning and it looks as though its doing the same thing for stylesheets. I tried to define a new block called pagescripts in my layout.html.twig and then use the block in my create.html.twig but this had the same outcome, it just seems to dump the scripts and stylesheets wherever I use the

{% block pagescripts %} 
   (scripts here) 
{% endblock}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼疼热情 2025-01-01 10:27:41

我发现了这个问题。在 create.html.twig 中,我在 {% block content %} 内部定义了 {% block javascripts %} 内容,所以我假设 Twig 正在渲染 content 块内的 javascripts 块的输出。

{% block javascripts %} 内容移到 {% block content %} 块之外解决了该问题。

I found the issue. In create.html.twig I was defining my {% block javascripts %} content inside inside my {% block content %}, so I assume Twig was rendering the output of the javascripts block inside the content block.

Moving the {% block javascripts %} content outside of the {% block content %} block fixed the issue.

挖鼻大婶 2025-01-01 10:27:41

这是 main.html.twig 的示例:

<body>
    {% block stylesheets %}
    {% endblock %}

    {% block jsscript %}
    {% endblock %}

    {% block content %}
    {% endblock %}
</body>

进入您的 create.html.twig

{% extends '::base.html.twig' %}

{% block jsscript %}
    my javascript files...
{% endblock %}

{% block content %}
<h1>Create</h1>

<form action="{{ path('entity_create') }}" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}
    <p>
        <button type="submit">Create</button>
    </p>
</form>

 {% endblock %}

如果仍然有问题,您可以添加文档准备功能:

$(document).ready(function() {
   // put all your jQuery goodness in here.
 });

Here is an example of main.html.twig:

<body>
    {% block stylesheets %}
    {% endblock %}

    {% block jsscript %}
    {% endblock %}

    {% block content %}
    {% endblock %}
</body>

into your create.html.twig

{% extends '::base.html.twig' %}

{% block jsscript %}
    my javascript files...
{% endblock %}

{% block content %}
<h1>Create</h1>

<form action="{{ path('entity_create') }}" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}
    <p>
        <button type="submit">Create</button>
    </p>
</form>

 {% endblock %}

If you still have issue, you can add a document ready function :

$(document).ready(function() {
   // put all your jQuery goodness in here.
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文