简体   繁体   English

Symfony:在PHP模板上包含CSS和JS

[英]Symfony: Include css and js on a php template

How to include .css and .js files in a php file (index.html.php for example) on Symfony? 如何在Symfony的php文件(例如index.html.php)中包括.css和.js文件? (I'm using Symfony2 if that matters), I read that I must include the src/link within an asset function like follow: (我在使用Symfony2,如果那很重要的话),我读到我必须在资产函数中包括src / link,如下所示:

<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. 既不是那个,也不是普通的php,与我合作。 Update: css files locate on \\Bundle\\Resources\\public\\css 更新:css文件位于\\Bundle\\Resources\\public\\css

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. 如果您的资产位于src/Your/Bundle/Resources/public/ ,则必须调用./app/console assets:install --symlink web/创建从web/bundle/yourbundlesrc/Your/Bundle/Resources/public/的符号链接。 src/Your/Bundle/Resources/public/以便可以从Web访问它们(或者,您可以执行./app/console assets:install web/以直接复制文件,而无需符号链接。

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: 使用Assetic,您不需要安装/复制文件,assetical将为您完成:

<?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). 请参阅http://symfony.com/doc/current/cookbook/assetic/asset_management.html (单击代码示例上的PHP选项卡)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM