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

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

问题描述

我正在尝试将样式表添加到数组中,以便随着树枝模板扩展到第二层和第三层,聚合样式将得以延续.

本主题与跨继承模板组合资产资源相关p>

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

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

从我的操作中调用的第一个模板来自 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,它有自己的样式要在所有其他模板之前添加.

{% 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"/>{% endstylesheets %}</头>

但是,它在这一行中断

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

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

<代码>{%设置 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 }} %}

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

解决方案

目前这是不可能的,因为 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天全站免登陆