Skip to main content
  • Guides & Documentation

Knowledgebase

Featured articles, how-to guides and quick tips.

Image Styles in Twig - Quick & Easy Guide - Drupal

So you need to render an image with an image style in Twig.

Follow this quick and easy guide to do so in your node.html.twig. You should be able to apply this to any custom entity template though!

Just switch out the bolded text below to accomplish this:

{% set img_with_style = content.field_custom_image.0 %}
{% set img_with_style = img_with_style|merge({'#image_style': 'large'}) %}
{{ img_with_style }}

Best practise would usually be to do this via the Manage Display tab for your content (or other entity) type, or via fielding in Views.

But there are definitely situations where these usual methods won't quite fit the bill, particularly if you're dealing with an especially complex Drupal site, and are managing and displaying content in a highly-architectured way.

If you can't do this via the Drupal admin UI, then doing so via Twig works great for special cases. There's no simpler way to accomplish this, so have at it!