我有这个:
ShopBundle
Controller
Resources
public
images
marker.png
views
Default
index.html.twig
在我的index.html。小枝,我想要这个
<img src="{{ asset("images/marker.png") }}"/>
我希望使用我的bundle的人只要使用他们自己的marker.png就可以创建一个继承我的bundle并按照文件结构放置他们的图像:
MyShopBundle
Resources
public
images
marker.png
是否有任何简单的方法来做到这一点在Symfony2 ?需求似乎如此简单,以至于我不敢相信我还没有找到答案。
如何从bundle resources目录中包含一个图像资源到你的bundle模板中?我已经做了一个
./apps/hfr/console assets:install web
,但我的模板没有打印正确的url (/images/marker.png而不是/bundles/ShopBundle/Resources/public/images/png)是否有可能重写我想要的方式或我迷路了?
解决方案:
使用@语法…
{% image '@VendorMyShopBundleBundle/Resources/public/images/example.jpg'
output='/images/example.jpg' %}
<img src="{{ asset_url }}" alt="Example"/>
{% endimage %}
请注意Vendor/YourBundle/Resources/public将不能被你的web服务器正常访问。
因此,assets:install命令会将这些资源复制到web/bundles/vendoryourbundle如果你使用的是开发环境,{{asset('path/to/asset.jpg')}}函数会调整你的资源的url:
http://hostname/app_dev.php
从/path/to/asset.jpg
/app_dev.php/to/asset.jpg
[编辑]如果你想对资产有更多的控制,可以考虑使用资产集合。
配置方法如下:
# 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 %}
我不确定现在是否asset/assets/packagename/inputs配置数组支持@VendorYourBundle语法,然后使用bundle继承。
添加:
在您可以使用这些包之前,您必须使用控制台命令:
php app/console assetic:dump