2

I'm using Twig as a Template Engine. I want to render my CSS files though Twig, with the advantage of macros. The response I receive looks fine. But somehow if I include it, the HTML page doesn't recognise it as a css file. When I include the real css files, the site works just fine. The comparison of the received source code is exactly the same...

Although, Chrome displays the files differently. It displays the rendered file on one line. It is probably being interpreted as a HTML file.

The real css file is being displayed like the source code, on multiple lines.

You include both files with the extension of .css. How can I fix this?

2 Answers 2

2

You say that you get the correct contents, but it's not interpreted as css. This could be because of an incorrect contenty-type header.

Css files should be sent as text/css, in Sf the default is text/html. In the action that renders your css make sure to set the correct header.

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

3 Comments

Yes, that sounds legit. Do you know how to do this in Twig? Google didn't know the answer.
Ok, it worked by setting header("Content-Type: text/css"); Isn't there a more elegant way?
You can set headers in your controller on the Response object.
2

Perhaps you should take a look at LESS its more effizient instead of parsing your CSS files. With LESS you have a lot of possibilities to make blocks and reuse functions with variables.

Perhaps its an alternative for your problem.

Edit:
Template Suffix
Run CSS file through Twig when using {% stylesheets %} tag in Twig with Symfony2

4 Comments

Thanks, but I'd like to keep that plan B. I'm already using Twig for all of my files, that's why I wanted to do the same with my CSS files.
I have added 2 links with solutions.
The engine suffix didn't solve my problem. I think Twig isn't type-specified. The second link hasn't a solution yet.
Take a look at the solution. Thanks for your help though.

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.