我正在尝试在奏鸣曲管理列表中呈现我的产品图像。
在我的索引页面中,我正在渲染我的产品:
{% for entity in entity %}
<span><img alt="airline" width="500" height="400" src="{{ entity.image.path}}" ></span>
{% endfor %}
我试图做同样的事情,但它不起作用。
所以我决定安装LiipImagineBundle,这就是我所做的。
ProductAdmin.php
->add('image.id', null, array('template' => 'ApplicationSonataBackcommerceBundle::list_image.html.twig'))
list_image.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field%}
<div>
{% for entity in entity %}
<img src="{{ entity.image.path | imagine_filter('productThumb') }}" />
{% endfor %}
</div>
{% endblock %}
config.yml
liip_imagine:
resolvers:
default:
web_path: ~
filter_sets:
cache: ~
productThumb:
quality: 90
filters:
thumbnail: { size: [47, 42], mode: outbound }
刷新页面时出现此错误:
在渲染模板期间抛出了异常 (“通知:未定义的索引:实体”)in 第4行的ApplicationSonataBackcommerceBundle :: list_image.html.twig。
修改
这是完整的添加方法:
/**
* @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
*
* @return \Sonata\AdminBundle\Datagrid\ListMapper
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('user.id')
->add('image.id', null, array('template' => 'ApplicationSonataBackcommerceBundle::list_image.html.twig'))
->add('airport', null, array('editable' => true))
->add('airport1', null, array('editable' => true))
->add('departuredate')
->add('arrivaldate')
->add('price')
->add('telephone', null, array('editable' => true))
->add('_action', 'actions', array(
'actions' => array(
'show' => array(),
'edit' => array(),
'delete' => array(),
)
));
return $listMapper;
}
这是仅生成两张图片的html:
<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="7">
<div>
<img src="http://127.0.0.1/symfony/web/app_dev.php/media/cache/resolve/productThumb/symfony/web/airline/Emirates.png" />
</div>
</td>
<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="9">
<div>
<img src="http://127.0.0.1/symfony/web/app_dev.php/media/cache/resolve/productThumb/symfony/web/airline/Singapore_Airlines.svg" />
</div>
</td>
答案 0 :(得分:1)
这是因为您更改了cache_prefix
。
在original doc中,提取:
For example with this bundle the following is possible:
<img src="{{ '/relative/path/to/image.jpg' | imagine_filter('thumbnail') }}" />
默认缓存前缀为:cache_prefix: media/cache
因为它是在www.yoursite/media/cache/
您已被/symfony/web/media/cache/cache/airline
因此,如果您通过{{ entity.image.path | imagine_filter('productThumb') }}
发送,则该路径不是所需的亲戚......
您现在发送/symfony/web/media/cache/cache/airline/your_image.jpg