Include css and js files from vendor library via a

2019-07-04 04:25发布

问题:

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.