Comment Counts in Twig

Views makes it easy to include a node's comment count as a field, but what about when you aren't using fields...when you display content, instead?

You could write a preprocess function, but why code when you don't have to? Use twig!

The comment class includes a method count(), but that method is not exposed to twig. Luckily, an alternative is:

{{ node.comment.comment_count }}

That's all there is to it!


Type:
Tip
Tags:
D9
D8
Drupal
twig