Php 在symfony2细枝中显示图像列表
我已经在下面的目录中上传了一些图片 BundleVoteBundle/web/uploads/images/namited/ 图像名称是唯一的。 现在我想用这个控制器显示所有数据和相应的图像Php 在symfony2细枝中显示图像列表,php,symfony,twig,Php,Symfony,Twig,我已经在下面的目录中上传了一些图片 BundleVoteBundle/web/uploads/images/namited/ 图像名称是唯一的。 现在我想用这个控制器显示所有数据和相应的图像 public function indexAction() { $nominee= $this->getDoctrine()->getRepository('BundleVoteBundle:Nomination')->findAll(); if(!$nominee)
public function indexAction()
{
$nominee= $this->getDoctrine()->getRepository('BundleVoteBundle:Nomination')->findAll();
if(!$nominee){
throw $this->createNotFoundException('There is no nominee in your list');
} else {
return $this->render('BundleVoteBundle:Nomination:index.html.twig',array('nominees'=>$nominee));
}
}
现在我的问题是如何使用for循环在细枝中显示这些图像。
在twig文件中,我只能使用其图像id显示一个图像
{% for nominee in nominees %}
<p>{{nominee.name|upper}}</p>
{{nominee.path}}
<img src="{{asset('uploads/images/voter/52bbc07a8136e.jpeg')}}"/>
{% endfor %}
{被提名人%中被提名人的百分比}
{{被提名人姓名|上}
{{被提名人路径}
{%endfor%}
在提名
实体中有一个返回上传图像路径的方法会很舒服:
然后在twig中,您应该能够以这种方式使用它:
{% for nominee in nominees %}
<p>{{nominee.name|upper}}</p>
// should return web/uploads/voter/52bbc07a8136e.jpeg #}
<img src="{{ asset(nominee.photoUploadDir) ~ '/' ~ nominee.path }}" />
{% endfor %}
{被提名人%中被提名人的百分比}
{{被提名人姓名|上}
//应返回web/uploads/voter/52bbc07a8136e.jpeg}
{%endfor%}
its可以显示单个照片。但是我想要照片列表。哦,老兄,在你的问题中加上这个。你如何获得图像id?{{namegate.path}}将显示图像idif{{namegate.path}}是你图像的路径,那么为什么不将其设置为图像源,比如。我不确定,但请尝试一下。根据您的定义方式,给我以下输出:。可能是被提名人。路径在此不起作用。您应该检查数据库,查看是否所有提名都已设置path
。请告诉我们问题出在哪里。
{% for nominee in nominees %}
<p>{{nominee.name|upper}}</p>
// should return web/uploads/voter/52bbc07a8136e.jpeg #}
<img src="{{ asset(nominee.photoUploadDir) ~ '/' ~ nominee.path }}" />
{% endfor %}