How to verify a view has results before rendering it in a template with Twig Tweak

The Twig Tweak module in Drupal offers an easy built-in solution that allows you to verify that a view has results prior to rendering it in a template.

Use drupal_view_result('view_name', 'view_display_machine_name')

Here is an example.


{% if drupal_view_result('author_blog_posts', 'block_1') is not empty %}
 
  <div class="author-blog-posts">
  
    {{ drupal_view('author_blog_posts', 'block_1') }}

  </div>

{% endif %}

Check the Twig Tweak cheat sheet for more options.