VichUploaderBundle twig无法获取文件名属性值:“image”

时间:2012-10-10 15:03:29

标签: symfony symfony-2.1

我在使用VichUploaderBundle时遇到了问题。正确上传文件,但是当我写

<td><img src="<img src="{{ vich_uploader_asset(entity, 'image') }}" alt="{{ entity.nombre }}" />"</td>

显示上传的图片,我有这个错误:

An exception has been thrown during the rendering of a template ("Unable to get filename property value: "image"") in SuperlineaBundle:Instruccion:index.html.twig at line 21.

我试图在控制器上获得帮助,但它确实有效:

$helper = $this->container->get('vich_uploader.templating.helper.uploader_helper');
$path = $helper->asset($entities[0], 'image');
ladybug_dump( $path );

显示:string(25)“/ images / uploads / video.png”

任何帮助或线索?

这是我的实体:http://pastebin.com/d8X72zK4 这是我的配置:

vich_uploader:
    db_driver: orm
    twig: true
    gaufrette: false # set to true to enable gaufrette support
    storage: vich_uploader.storage.file_system
    mappings:
        uploads:
            uri_prefix: /images/uploads
            upload_destination: %kernel.root_dir%/../web/images/uploads
            namer: ~ # specify a file namer service id for this entity, null default
            directory_namer: ~ # specify a directory namer service id for this entity, null default
            delete_on_remove: true # determines whether to delete file upon removal of entity
            inject_on_load: true # determines whether to inject a File instance upon load
        pdfs:
            uri_prefix: /images/pdf
            upload_destination: %kernel.root_dir%/../web/images/pdf
            namer: ~ # specify a file namer service id for this entity, null default
            directory_namer: ~ # specify a directory namer service id for this entity, null default
            delete_on_remove: true # determines whether to delete file upon removal of entity
            inject_on_load: true # determines whether to inject a File instance upon load

这是我的树枝模板: ...

{% for entity in entities %}
        <tr>
            <td><a href="{{ path('instruccion_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>
            <td>{{ entity.nombre }}</td>
            <td>{{ entity.foto }}<img src="<img src="{{ vich_uploader_asset(entity, 'image') } }" alt="{{ entity.nombre }}" />"</td>
            <td>{{ entity.video }}</td>
            <td>
                <ul>
                    <li>
                        <a href="{{ path('instruccion_show', { 'id': entity.id }) }}">show</a>
                    </li>
                    <li>
                        <a href="{{ path('instruccion_edit', { 'id': entity.id }) }}">edit</a>
                    </li>
                </ul>
            </td>
        </tr>
    {% endfor %}

3 个答案:

答案 0 :(得分:6)

我也有这个问题。我是由我的实体加载方式引起的。我正在展示的实体是另一个实体的财产。

详细说明:

https://github.com/dustin10/VichUploaderBundle/issues/109

https://github.com/dustin10/VichUploaderBundle/issues/28

我希望这有助于某人!

谢谢,  GAV

答案 1 :(得分:1)

更正此行

<td>{{ entity.foto }}<img src="<img src="{{ vich_uploader_asset(entity, 'image') } }" alt="{{ entity.nombre }}" />"</td>

你有双img标签

答案 2 :(得分:1)

如果目标字段具有空值,则需要在调用vich_uploader_asset之前对其进行测试。试试这个:

<td>
{# foto is the uploader target field in your entity #}
{% if entity.foto %}
<img src="{{ vich_uploader_asset(entity, 'image') }}" alt="{{ entity.nombre }}" />
{% else %}
{# display nothing, or use a generic image #}
<img src='no-img.png' alt='{{ entity.nombre }}' />
{% endif %}
</td>