多个模板中的 Twig Assetic 样式表
我正在尝试将样式表添加到数组中,以便当树枝模板延伸到第二层和第三层时,聚合的样式将继续下去。
相关
本主题与跨继承模板组合 Assetic 资源来自 config .yml,我创建了一个全局数组 mystyles
,以便我们可以在渲染过程中“冒泡”时将其添加到必要的样式列表中。
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
globals:
mystyles: []
我的操作调用的第一个模板来自 CommunicatorBundle/Resources/views/Admin/Workspace.html.twig,我已为此页面设置了名为 admin.workspace.css< 的特定样式/代码>。
{% extends "DJCommunicatorBundle::base.html.twig" %}
{% set mystyles = ["@DJCommunicatorBundle/Resources/public/css/admin.workspace.css"]|merge(mystyles) %}
它扩展了 CommunicatorBundle/Resources/views/base.html.twig,它有自己的要求 data-table.css
。
{% extends "DJSharedBundle::base.html.twig" %}
{% set mystyles = ["@DJCommunicatorBundle/Resources/public/css/data-table.css" ]|merge(mystyles) %}
最后,我们加载最外面的模板,SharedBundle/Resources/views/base.html.twig,它有自己的样式要在所有其他模板之前添加。
<head>
{% set mystyles = ['@DJSharedBundle/Resources/public/css/global.css', '@DJSharedBundle/Resources/public/css/grid.990.9-col.css']|merge(mystyles) %}
{% stylesheets {{ mystyles }} %}
<link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %}
</head>
它在这一行中断了
{% stylesheets {{ mystyles }} %}
然而,尽管这种类型的测试以正确的顺序打印我期望的数组,但
{{ mystyles|join(', ') }}
似乎 {% stylesheets %}
标记想要像下面的代码片段一样工作(可以理解,这不是一个数组对象,而是一个以空格分隔的分隔字符串列表)。
{% stylesheets
'@DJSharedBundle/Resources/public/css/global.css'
'@DJSharedBundle/Resources/public/css/grid.990.9-col.css'
'@DJCommunicatorBundle/Resources/public/css/data-table.css'
'@DJCommunicatorBundle/Resources/public/css/admin.workspace.css'
%}
<link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %}
即便如此,我尝试将字符串设置为这么长的值并打印它,但这也不起作用:
{%
set str = "@DJSharedBundle/Resources/public/css/global.css
@DJSharedBundle/Resources/public/css/grid.990.9-col.css
@DJCommunicatorBundle/Resources/public/css/data-table.css
@DJCommunicatorBundle/Resources/public/css/admin.workspace.css"
%}
{% stylesheets {{ str }} %}
我觉得全局应该是一个可行的解决方案,尽管目前不起作用。希望我很接近。什么可以解决这个问题?
I'm trying to add stylesheets to an array so that as twig templates extend through a second and third levels the aggregated styles will carry through.
This topic is related to Combining Assetic Resources across inherited templates
From config.yml, I made a global array mystyles
so that we can add to the list of necessary styles as we "bubble up" through the rendering process.
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
globals:
mystyles: []
The first template called from my action is from CommunicatorBundle/Resources/views/Admin/Workspace.html.twig and I've set the specific style for this page called admin.workspace.css
.
{% extends "DJCommunicatorBundle::base.html.twig" %}
{% set mystyles = ["@DJCommunicatorBundle/Resources/public/css/admin.workspace.css"]|merge(mystyles) %}
It extends CommunicatorBundle/Resources/views/base.html.twig which has it's own requirement data-table.css
.
{% extends "DJSharedBundle::base.html.twig" %}
{% set mystyles = ["@DJCommunicatorBundle/Resources/public/css/data-table.css" ]|merge(mystyles) %}
Finally, we load the outermost template, SharedBundle/Resources/views/base.html.twig, which has it's own styles to add before all others.
<head>
{% set mystyles = ['@DJSharedBundle/Resources/public/css/global.css', '@DJSharedBundle/Resources/public/css/grid.990.9-col.css']|merge(mystyles) %}
{% stylesheets {{ mystyles }} %}
<link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %}
</head>
However, it breaks at this line
{% stylesheets {{ mystyles }} %}
inspite of this type of test that prints the array that I expect in the proper order
{{ mystyles|join(', ') }}
It seems that the {% stylesheets %}
tag wants something like the following snippit to work (which is understandably not an array object, but a whitespace separated list of delimited strings).
{% stylesheets
'@DJSharedBundle/Resources/public/css/global.css'
'@DJSharedBundle/Resources/public/css/grid.990.9-col.css'
'@DJCommunicatorBundle/Resources/public/css/data-table.css'
'@DJCommunicatorBundle/Resources/public/css/admin.workspace.css'
%}
<link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %}
Even then, I tried setting a string to such a long value and printing it, but this doesn't work either:
{%
set str = "@DJSharedBundle/Resources/public/css/global.css
@DJSharedBundle/Resources/public/css/grid.990.9-col.css
@DJCommunicatorBundle/Resources/public/css/data-table.css
@DJCommunicatorBundle/Resources/public/css/admin.workspace.css"
%}
{% stylesheets {{ str }} %}
I feel like the global should be a viable solution, though not currently working. Hopefully I'm close. What might fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前这是不可能的,因为 Assetic 静态分析您的模板以查找您在其中定义的任何资产。支持这些动态案例已列入路线图。
This is not possible right now because Assetic statically analyzes your templates to find any assets you've defined there. Supporting these dynamic cases is on the roadmap.