简体   繁体   English

如何用变量+字符串连接在twig中显示img?

[英]How to display img inside twig with variable + string concatenate?

I have problem for displaying img, i saved images with name of book and now to access them need to concatenate .jpg extension. 我有显示img的问题,我保存了带有书名的图像,现在要访问它们需要连接.jpg扩展名。 So my problem is how to concatenate variable and string inside twig template ? 所以我的问题是如何在twig模板中连接变量和字符串?

<img src="{{ path({{ book.name ~ '.jpg' }}) }}"/>

here is full code: 这是完整的代码:

  {% for book in books %}
  <a href="{{ path('AppBundle_Book_detailsBook', {'bookId': book.id}) }}"
      class="col-2 white kartica">
      <img src="{{ path({{ book.name }} ~ '.jpg' ) }}"/>

      <h4>
          {{ book.name }}

      </h4>

  </a>
{% else %}
  No books.
{% endfor %}

为什么不尝试这些:

<img src="{{ path( book.name ~ '.jpg' ) }}"/>

Based on your comments, I think this is what you need: 根据您的意见,我认为这就是您所需要的:

<img src="{{ asset( '../app/Resources/images/covers/' ~ book.name ~ '.jpg') }}"/>

You might need to adjust the path slightly. 您可能需要稍微调整路径。 Assets start from the web/ directory. 资产从web/目录开始。 I think you should get the idea. 我想你应该明白这个想法。

By the way, thank you for making a detailed post! 顺便说一下,谢谢你做了详细的帖子!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM