2013-05-06 88 views
20

我存储IMG在Acme/DemoBundle/Resources/public/img/weather_icon/blizzard.gif 我想把这个IMG在我的模板,所以我做Symfony2的路径图像在树枝模板

<img src="{{ asset('bundles/acmedemo/img/weather_icons/blizzard.gif') }}" /> 

<img src="{{ asset('..img/weather_icons/blizzard.gif') }}" /> 

而这没有用。我做资产:安装和assetic:转储

解决 它的工作

<img src="{{ asset('img/weather_icons/Blizzard.gif') }}" alt="Symfony!" /> 
+0

第一语法是好的。 - 一些想法:检查'web/bundles/acmedemo/img/weather_icons/blizzard.gif'文件是否存在(很容易产生错字)。 – 2013-05-06 13:12:40

+0

@konadrian:只是为了阐明您的答案是没有Assetic的用法,您只需直接提供存储在应用程序中的文件。 使用Assetic比直接提供文件提供了许多优点。这些文件不需要存储在提供它们的位置,并且可以从各种来源(例如从一个包中提取)中获取。 – Dung 2015-12-27 15:49:18

回答

33

请试试这个

<img src="{{ asset('bundles/acmedemo/img/weather_icons/blizzard.gif') }}" /> 

你应该istall你的资产转移到web目录与命令

app/console assets:install web 
4

您可以使用一个包的图像:

{% image '@CoreBundle/Resources/public/images/your-image.jpg' output="images/your-image.jpg" %} 
<img src="{{ asset_url }}" width="100%" height="100%" alt="my alt of image" class="pull-left"> 
{% endimage %} 
0

资产解决方案:与资产相比,您将获得更好的资产表现。例如

目录结构:例如

C:\xampp\htdocs\yourproject\src\AppBundle\Resources\public\images\yourimage.png 

项目结构:

在yourhtml.html.twig通话
yourproject\src\AppBundle\Resources\public\images\yourimage.png 

{% image '@AppBundle/Resources/public/images/yourimage.png' %} 
        <img src="{{ asset_url }}" alt="Example" /> 
       {% endimage %} 

注:

ŧ他是为您提供安装assetic束可在项目的composer.json设置为这样:

"require": { 
"php": ">=5.3.3", 
"sensio/framework-extra-bundle": "~3.0", 
"symfony/assetic-bundle": "~2.6", }, 

谷歌上安装assetic束Symfony2的更多指令。

就是这样。

源:

http://symfony.com/doc/2.7/cookbook/assetic/asset_management.html#cookbook-assetic-cssrewrite