2

I'm new to Symfony2 and all this website development stuff. So I already wrote my code in /WebBundle/Resources/Views/Default/index.html.twig and some CSS code just to test it. The problem is, I don't know how to link the two. I tried looking it up online, but none of it solved my problem. In my index.html.twig file, I tried:

    {% block stylesheets %}
        <link href="{{ asset('bundles/WebBundle/css/web.css') }}" rel="stylesheet" type="text/css" />
    {% endblock %}

and

    {% block stylesheets %}
        <link href="{{ asset('css/web.css') }}" rel="stylesheet" type="text/css" />
    {% endblock %}
3
  • where you put your css file (web.css) ? Commented Jun 16, 2014 at 20:24
  • It's in /Applications/MAMP/htdocs/Symfony/src/Acme/WebBundle/Resources/public/css/web.css Or basically... /Symfony/src/Acme/WebBundle/Resources/public/css/web.css Commented Jun 16, 2014 at 20:26
  • Did you try to clear cache and re-install assets? If you didn't; open your console and type php app/console assets:install web --symlink ---Note: --symlink option is optional. If you want to hardcopy files to your web directory don't use it. Commented Jun 16, 2014 at 22:01

2 Answers 2

2

Use assetic :

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

In your config.yml tell symfony that your bundle will use assetic:

assetic:
    bundles:        [AcmeWebBundle]

Finally when passing to production you have to dump your css and/or your js buy executing :

php app/console assetic:dump

Further infos : http://symfony.com/doc/current/cookbook/assetic/asset_management.html

Sign up to request clarification or add additional context in comments.

Comments

1
{% block stylesheets %}
    {% stylesheets
        '@AcmeWebBundle/Resources/public/css/web.css'
        '@AcmeWebBundle/Resources/public/css/reset.css'
    %}
        <link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
    {% endstylesheets %}
{% endblock %}

Assuming your files web.css, reset.css are located in Acme\WebBundle\Resources\public\css

5 Comments

and don't forget to dump them when going to production php app/console assetic:dump
reset.css? What's that? I only made web.css :\
well then don't use that line.. it was only to show that it can be used for multiple files
Okay so I tried doing what you told me and I got this... CRITICAL - Uncaught PHP Exception Twig_Error_Syntax: "An exception has been thrown during the compilation of a template ("You must add AcmeWebBundle to the assetic.bundle config to use the {% stylesheets %} tag in AcmeWebBundle:Default:index.html.twig.") in "AcmeWebBundle:Default:index.html.twig"." at /Applications/MAMP/htdocs/Symfony/app/cache/dev/classes.php line 3124
I replaced "asset_url" with "bundles/AcmeWebBundle/css/web.css".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.