多个模板中的 Twig Assetic 样式表 [英] Twig Assetic Stylesheets Among Several Templates

查看:27
本文介绍了多个模板中的 Twig Assetic 样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将样式表添加到数组中,以便当树枝模板扩展到第二和第三层时,聚合样式将继续进行.

本主题与跨继承模板组合Assetic资源>

config.yml 中,我创建了一个全局数组 mystyles,这样我们就可以在渲染过程冒泡"时将其添加到必要的样式列表中.

树枝:调试:%kernel.debug%严格变量:%kernel.debug%全局变量:我的风格:[]

从我的操作中调用的第一个模板来自 CommunicatorBundle/Resources/views/Admin/Workspace.html.twig,我为此页面设置了名为 admin.workspace 的特定样式.css.

{% 扩展 "DJCommunicatorBundle::base.html.twig" %}{% set mystyles = ["@DJCommunicatorBundle/Resources/public/css/admin.workspace.css"]|merge(mystyles) %}

它扩展了CommunicatorBundle/Resources/views/base.html.twig,它有自己的需求data-table.css.

{% 扩展 "DJSharedBundle::base.html.twig" %}{% set mystyles = ["@DJCommunicatorBundle/Resources/public/css/data-table.css"]|merge(mystyles) %}

最后,我们加载最外面的模板,SharedBundle/Resources/views/base.html.twig,它有自己的样式可以添加到所有其他模板之前.

{% set mystyles = ['@DJSharedBundle/Resources/public/css/global.css', '@DJSharedBundle/Resources/public/css/grid.990.9-col.css']|merge(mystyles) %}{% 样式表 {{ mystyles }} %}<link rel="stylesheet" href="{{ asset_url }}" type="text/css"/>{% 结束样式表 %}

但是,它在这一行中断

{% 样式表 {{ mystyles }} %}

尽管这种类型的测试以正确的顺序打印我期望的数组

{{ mystyles|join(', ') }}

似乎 {% 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"/>{% 结束样式表 %}

即使那样,我也尝试将字符串设置为如此长的值并打印它,但这也不起作用:

{%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"%}{% 样式表 {{ str }} %}

我觉得全局应该是一个可行的解决方案,尽管目前还行不通.希望我很接近.什么可以解决这个问题?

解决方案

这现在是不可能的,因为 Assetic 静态 分析您的模板以查找您在那里定义的任何资产.支持这些动态案例已在路线图上.

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?

解决方案

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.

这篇关于多个模板中的 Twig Assetic 样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆