0

I am using Drupal 9 with twig files

I have a page where in one section I need to show thumbnails of the latest posts. So I created a new view as a block, and placed this block under Content in admin/structure/block/list/

When I inspect with debug on I get these file suggestions -

 FILE NAME SUGGESTIONS:
   * block--views-block--latest-news-block-1.html.twig
   * block--views-block--latest-news-block-1.html.twig
   * block--views-block.html.twig
   * block--views.html.twig
   x block.html.twig

So I created a file name block--views-block--latest-news-block-1.html.twig with the code from block.html.twig.

Inside that file I have -

    {% block content %}
      {{ content }}
    {% endblock %}

Which shows all the content in the block. I need to customize the content in the block though, some things need to be removed, I need to truncate the article text, need to add buttons.

I think I'd be able to do what I want if I could target specific parts of the content. I want to do something like this -

<div class="card">
     <h2>{{ content.title }}</h2>
     <p>{{ content.body }}</p>
</div>

Is there a way to target specific parts of the content like that? 

2

Normally it works that way but Views templates may be different.

  1. Enable Twig debugging.
  2. Add Devel and Kint: composer require drupal/devel kint-php/kint
  3. Enable Devel: drush -y en devel
  4. Inside the template put {{ kint(content) }}, flush caches, visit the page and you should see what else can be found inside content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.