2013-05-06 97 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