简体   繁体   中英

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. So my problem is how to concatenate variable and string inside twig template ?

<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. I think you should get the idea.

By the way, thank you for making a detailed post!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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