2

I have some pages in my Symfony project that needs some specific css, in general I have a general css file for font and such, but on every page I have a table that needs different formatting.

I know I can use classes for this, but it is more convenient that I just use a different css file for these pages.

Now I am using this in my twig template file:

{%block stylesheets %}

{%endblock%}

But is there a way to include a css file? I have this in my main template file:

<head>
        <meta charset="utf-8">
        <title>Sign in &middot; Project</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">

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

        {%block stylesheets %}
        {%endblock%}
    </head>

And if I do it like this it won't work:

{%block stylesheets %}
     <link href="{{asset('bundles/loginlogin/css/specificPage.css')}}" rel="stylesheet">
{%endblock%}
2
  • Are u extending the main template? Commented Nov 3, 2014 at 11:42
  • yes i am, the head section in my post is of the main template, I extend it like this {%extends "LoginLoginBundle:Default:gameTemplate.html.twig" %} Commented Nov 3, 2014 at 11:44

1 Answer 1

6

Your main template should look like this

    {% block stylesheets %}

                {% stylesheets
                        'bundles/loginlogin/css/socproGame.css'
                %}
                    <link rel="stylesheet" href="{{ asset_url }}" />
                {% endstylesheets %}

   {% endblock %}

And your twig which extends the main:

    {% block stylesheets %}
        {{ parent() }}

        {% stylesheets
            'bundles/loginlogin/css/specificPage.css'
                %}
            <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}

    {% endblock %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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