解决方案:
使用@语法...
{% image '@VendorMyShopBundleBundle/Resources/public/images/example.jpg'
output='/images/example.jpg' %}
<img src="{{ asset_url }}" alt="Example"/>
{% endimage %}
请注意供应商/YourBundle/资源/公共您的网络服务器通常无法访问。
因此,assets:install 命令会将资产复制到网络/捆绑包/vendoryourbundle
如果您使用的是开发环境,则 {{ asset('path/to/asset.jpg') }} 函数将调整资产的 url:
http://hostname/app_dev.php
from
/path/to/asset.jpg
to
/app_dev.php/to/asset.jpg
[EDIT]
如果您想对资产有更多控制权,可以考虑使用资产收藏.
您可以按如下方式配置它们:
# app/Resources/config/config.yml
assetic:
[...]
assets:
css_bootstrap:
inputs:
- %kernel.root_dir%/../src/Vendor/YourBundle/Resources/public/twitter-bootstrap/less/bootstrap.less
- [...]
filters:
- lessphp
- [...]
output: css/bootstrap.css
my_image:
inputs:
- %kernel.root_dir%/../path/to/image.png
filters:
- optipng
output: images/image-with-new-name.png
然后在模板中使用它们,如下所示:
{% stylesheets '@css_bootstrap' %}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}">
{% endstylesheets %}
我现在不确定 assetic/assets/packagename/inputs 配置数组是否也支持 @VendorYourBundle 语法,然后使用包继承。
添加:
在使用这些包之前,您必须使用控制台命令:
php app/console assetic:dump