使用 AsseticBundle 包含 Twig 视图中的资源

发布于 2024-11-01 17:54:58 字数 708 浏览 0 评论 0原文

在 Symfony 1.4 中,我通常仅包含所需的资源:

apps/myApp/config/view.yml (每个页面中使用的常规资源)

stylesheets:   [main.css]
javascripts:    [lib/jquery.js, lib/jquery.qtip.js, backend/main.js]

apps/myApp/modules/someModule/ templates/someTemplateSuccess.php (仅用于此视图、部分等的资产)

<?php use_stylesheet('backend/datagrid.css') ?>
<?php use_javascript('backend/datagrid.js') ?>

,然后最终在 apps/myApp/templates/layout.php 中链接这些内容:

<?php include_stylesheets() ?>
<?php include_javascripts() ?>

那么,如何在 Twig 视图中使用 AsseticBundle 来做到这一点呢?

我真的很困惑...谢谢!

In Symfony 1.4 I use to include just the needed assets with:

apps/myApp/config/view.yml (general assets to be used in every page)

stylesheets:   [main.css]
javascripts:    [lib/jquery.js, lib/jquery.qtip.js, backend/main.js]

apps/myApp/modules/someModule/templates/someTemplateSuccess.php (assets just for this view, partial, etc)

<?php use_stylesheet('backend/datagrid.css') ?>
<?php use_javascript('backend/datagrid.js') ?>

and then finally linking those in apps/myApp/templates/layout.php:

<?php include_stylesheets() ?>
<?php include_javascripts() ?>

So, how to do this using the AsseticBundle in Twig views?

I'm really confused... thanks!

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

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

发布评论

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

评论(1

埋情葬爱 2024-11-08 17:54:58

好的,我到了这里:

https://github.com/kriswallsmith/symfony-sandbox/commit /f1fc1d0cf2fe69660f94f33719a4508d6e9e25ae

,它有效!

它像这样:

src/MySite/MyBundle/Resources/css/datagrid.css

将其包含在视图中:

< strong>src/MySite/MyBundle/Resources/views/MyViews/myview.html.twig

{% block stylesheets %}
    {% stylesheets '@MySiteMyBundleBundle/Resources/css/datagrid.css' %}
        <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
    {% endstylesheets %}
{% endblock %}

最后,让我们打印它:

app/Resources/views/base.html.twig

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>{% block title %}Lol!{% endblock %}</title>
        {% block stylesheets %}{% endblock %}
        <link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        {% block body %}{% endblock %}
        {% block javascripts %}{% endblock %}
    </body>
</html>

太棒了!

更新:

我仍然不知道为什么,但是:

{% stylesheets '@MySiteMyBundleBundle/Resources/css/*.css' output='css/all.css' %}
    <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}

只能将 debug 设置为 false,因此最好的方法是配置它:

app/config/config.yml

# Assetic Configuration
assetic:
    debug:          false
    use_controller: true
    write_to: %kernel.root_dir%/../web
    filters:
        cssrewrite: ~

Ok, I got here:

https://github.com/kriswallsmith/symfony-sandbox/commit/f1fc1d0cf2fe69660f94f33719a4508d6e9e25ae

and it WORKS!

it goes like this:

src/MySite/MyBundle/Resources/css/datagrid.css

to include it in the view:

src/MySite/MyBundle/Resources/views/MyViews/myview.html.twig

{% block stylesheets %}
    {% stylesheets '@MySiteMyBundleBundle/Resources/css/datagrid.css' %}
        <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
    {% endstylesheets %}
{% endblock %}

and finally, lets print it:

app/Resources/views/base.html.twig

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>{% block title %}Lol!{% endblock %}</title>
        {% block stylesheets %}{% endblock %}
        <link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        {% block body %}{% endblock %}
        {% block javascripts %}{% endblock %}
    </body>
</html>

Great!

UPDATE:

I still don't know why but:

{% stylesheets '@MySiteMyBundleBundle/Resources/css/*.css' output='css/all.css' %}
    <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}

Only works setting debug to false, so the best way to do this is configuring it:

app/config/config.yml

# Assetic Configuration
assetic:
    debug:          false
    use_controller: true
    write_to: %kernel.root_dir%/../web
    filters:
        cssrewrite: ~
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文