twitter-bootstrap 如何在 symfony2 中使用 assetic 和 less 来使用 twitter bootstrap?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18969978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 18:33:12  来源:igfitidea点击:

How to use twitter bootstrap using assetic and less in symfony2?

twitter-bootstrapsymfonylessassetic

提问by zelazowy

I can't get twitter bootstrap working. I know there is many tutorials and so on, but none of it works for me...

我无法让 Twitter 引导程序正常工作。我知道有很多教程等等,但没有一个对我有用......

My composer.json is:

我的 composer.json 是:

"require":
    [...]
    "leafo/lessphp": "dev-master",
    "braincrafted/bootstrap-bundle": "dev-master",
    "twbs/bootstrap": "2.3.*"

My assetic configuration is:

我的资产配置是:

# Assetic Configuration
assetic:
    filters:
        lessphp:
            file: %kernel.root_dir%/../vendor/leafo/lessphp/lessc.inc.php
            apply_to: "\.less$"
        cssrewrite: ~
    assets:
        bootstrap_css:
            inputs:
                - %kernel.root_dir%/../vendor/twbs/bootstrap/less/bootstrap.less
                - %kernel.root_dir%/../vendor/twbs/bootstrap/less/responsive.less
            filters:
                - lessphp
                - cssrewrite
            output: css/bootstrap.css
        bootstrap_js:
            inputs:
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-transition.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-alert.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-button.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-carousel.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-collapse.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-dropdown.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-modal.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-tooltip.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-popover.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-scrollspy.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-tab.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-typeahead.js
                - %kernel.root_dir%/../vendor/twbs/bootstrap/js/bootstrap-affix.js
            output: js/bootstrap.js

And finally ::base.html.twig:

最后::base.html.twig

{% block stylesheets %}
    <link rel="stylesheet" href="{{ asset('css/bootstrap.css') }}">
{% endblock %}

{% block javascripts %}
    <script src="{{ asset('js/bootstrap.js') }}"></script>
{% endblock %}

I'm trying include js ans css with different ways, for example:

我正在尝试以不同的方式包含 js ans css,例如:

{% stylesheets '%kernel.root_dir%/../vendor/twbs/bootstrap/less/bootstrap.less' %}
    <link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

or

或者

{% stylesheets '@bootstrap_css' %}
    <link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

but it still not working and I'm getting 404 errors for bootstrap.css and bootstrap.js.

但它仍然无法正常工作,我收到了 bootstrap.css 和 bootstrap.js 的 404 错误。

Whats wrong with my config? I'm using wamp server on windows 7 64bit.

我的配置有什么问题?我在 Windows 7 64 位上使用 wamp 服务器。

The only way to get it working was to use cli command assetic:dumpbut I think it's not correct way to do this on dev environment.

让它工作的唯一方法是使用 cli 命令,assetic:dump但我认为这不是在开发环境中执行此操作的正确方法。

回答by zelazowy

Finally I've used simply js and css files from bootstrap, without any bundle in symfony because it's faster, easier and may have some advantages such as any update of bootstrap will not crash my site (see update bootstrap 2 to 3...).

最后,我只使用了 bootstrap 中的 js 和 css 文件,在 symfony 中没有任何包,因为它更快、更容易并且可能具有一些优点,例如任何 bootstrap 更新都不会使我的网站崩溃(请参阅更新 bootstrap 2 到 3...) .

So thanks to all for your answers but this is for me best solution.

所以感谢大家的回答,但这对我来说是最好的解决方案。

回答by Igor Pantovi?

Try adding this to your config_dev.yml:

尝试将其添加到您的 config_dev.yml 中:

assetic:
    use_controller: true

回答by sensi

So here is the final answer to your issue:

所以这是您问题的最终答案:

You have to run it manual because of performance impact. Otherwise if you use to much filters it takes to long to load the app and that can be really frustrating.

由于性能影响,您必须手动运行它。否则,如果您使用太多过滤器,则加载应用程序需要很长时间,这真的很令人沮丧。

In older versions of symfony it seems it was done automatic but have now removed.

在旧版本的 symfony 中,它似乎是自动完成的,但现在已删除。

See documentation: http://symfony.com/doc/current/cookbook/assetic/asset_management.html#dumping-asset-files-in-the-dev-environment

请参阅文档:http: //symfony.com/doc/current/cookbook/assetic/asset_management.html#dumping-asset-files-in-the-dev-environment

So everything is ok with your code and configuration.

因此,您的代码和配置一切正常。