Include css and js files from vendor library via assets in Twig

允我心安 提交于 2019-12-10 18:57:08

问题


I want to include css and js files from a library in my vendor directory into Twig.

I downloaded morrisjs via composer into my vendor directory of symfony. Now I want to include the main css und js files into my Twig Template. But as far as I know the asset function only works with files that are located in Bundles.

The files I want to include are located in the following paths:

  • project\vendor\morrisjs\morris.js\morris.js
  • project\vendor\morrisjs\morris.js\morris.css

I thought about some theoretical code that would look like that:

{% block stylesheets %}
    <link href="{{ asset('vendor/morrisjs/morris.js/morris.css') }}" />
{% endblock %}

Is there any possibility to include these files directly from vendor and how when not?


回答1:


If morrisjs is a frontend javascript library then install it via npm or bower.

Packages installed via composer should have all their public assets in Resources/public so you can publish them using:

$ php bin/console assets:install target [--symlink]

Then in a twig template use just:

<link href="{{ asset('bundles/acmedemo/css/contact.css') }}" rel="stylesheet" />



回答2:


After installing new packages, run:

php app/console assets:install web

'web' being your server's document root(public_html or whatever it is). You also need to dump your assets:

//for dev
php app/console assetic:dump
//for prod
php app/console assetic:dump --env=prod --no-debug

In your servers public root, you will have directory /bundles/ where all your files will be present, and you can easily include them in Twig with {{ asset('bundles/morri..) }}

Read up on combining assets, something you will need at some point.



来源:https://stackoverflow.com/questions/35697584/include-css-and-js-files-from-vendor-library-via-assets-in-twig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!