0

How to include .css and .js files in a php file (index.html.php for example) on Symfony? (I'm using Symfony2 if that matters), I read that I must include the src/link within an asset function like follow:

<link href="<?php echo $view['assets']->getUrl('MyStyle.css') ?>" rel="stylesheet" type="text/css" />

<link href="<?php echo $view['assets']->getUrl('buttons.css') ?>" rel="stylesheet" type="text/css" />

Neither that nor the ordinary of php, worked with me. Update: css files locate on \Bundle\Resources\public\css

3
  • what does the generated HTML look like? Commented Aug 22, 2011 at 11:11
  • it did not 'clearly' load the .css files, my .classes doesn't affect on my divs. Commented Aug 22, 2011 at 11:15
  • can you access the url of your css? (i.e. copy paste the link in your href). Another way would be to debug your page with Firebug or so and see which class do affect your element. Thus, you would see what happens (and if your element inherit properties from any other class or DOM). Commented Aug 22, 2011 at 11:27

1 Answer 1

1

You have two options:

without using assetic:

If your assets are in src/Your/Bundle/Resources/public/, you have to call ./app/console assets:install --symlink web/ to create a symlink from web/bundle/yourbundle to src/Your/Bundle/Resources/public/ so that they are accessible from web (alternatively you could do ./app/console assets:install web/ to copy the files directly, without symlink.

Then you could do this:

<link href="<?php echo $view['assets']->getUrl('/bundle/yourbundle/css/MyStyle.css') ?>" rel="stylesheet" type="text/css" />

using assetic:

With assetic you don't need to install/copy the files, assetic will do it for you:

<?php foreach ($view['assetic']->stylesheets(
    array('@YourBundle/css/MyStyle.css')) as $url): ?>
<script type="text/javascript" src="<?php echo $view->escape($url) ?>"></script>
<?php endforeach; ?>

See http://symfony.com/doc/current/cookbook/assetic/asset_management.html (click on the PHP tab on the code samples).

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

1 Comment

"With assetic you don't need to install/copy the files, assetic will do it for you" You do if you're using background images in the 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.