Tiko M
Tiko M

Reputation: 7

Symfony 2 assetic paths

i didnt understand how assethic paths works..

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

My css and images are in /bundles/dproc/ directory. The code i showed below works, but i cant understand why should i use asset function, i can do it like this and get the same result

{% block stylesheets %}
            <link href="/bundles/dproc/css/base.css" type="text/css" rel="stylesheet" />
        {% endblock %}

So what does asset function? Or how should my path look like with asset function?

Upvotes: 0

Views: 313

Answers (1)

Igor Pantović
Igor Pantović

Reputation: 9246

Because you probably shouldn't even use it like that. You should probably use Assetic with css like this:

{% block stylesheets %}
    {% stylesheets
    '/bundles/dproc/css/base.css' {# this will just get base.css #}
    '/bundles/dproc/css/mycustomdir/* {# this will find all css files in that dir #}
    %}
    <link href="{{ asset_url }}" rel="stylesheet">
    {% endstylesheets %}
{% endblock %}

By setting it up like this (with default config), in dev environment you will get exactly the same thing as if you just used <link .... > without assetic. But in production environment, your whole stylesheets assetic block will be merged into one file to reduce number of requests.

Later, you can set up some css and javascript minifiers and make them run in production environment. So, when you switch to production, you will have nice,economic css and javascript, while in dev invironment you will have exactly what you'd expect if you didn't use assetic at all.

Furthermore, if you use less or sass or something that needs to be precompiled, you could just include those files and tell assetic to recompile them automatically when they are changed.

Upvotes: 2

Related Questions