Reuse variable / logic within template

I have a block of meta tags in my layout (layout.antlers.html) and have a fallback set whenever a value cannot be retrieved from the content.

{{ yield:meta }}
  <link rel="canonical" href="{{ permalink }}" />

  {# Removed other meta tags for clarity... #}
  <meta name="description" content="{{ if excerpt }}{{ excerpt | widont }}{{ else }}{{ content | strip_tags | safe_truncate:160:... }}{{ /if }}">
  <meta property="og:description" content="{{ if excerpt }}{{ excerpt | widont }}{{ else }}{{ content | strip_tags | safe_truncate:160:... }}{{ /if }}" />
  <meta name="twitter:description" content="{{ if excerpt }}{{ excerpt | widont }}{{ else }}{{ content | strip_tags | safe_truncate:160:... }}{{ /if }}">
{{ /yield:meta }}

When the content has an excerpt it uses it as the description, but it falls back to the truncated content when its missing. It works like expected, but I don't like that the logic is copied three times. Is there a way to do this once and reuse the outcome multiple times?

I tried to set it through frontmatter, but that doesn't seem to work ¯\_(ツ)_/¯

---
description: '{{ if excerpt }}{{ excerpt | widont }}{{ else }}{{ content | strip_tags | safe_truncate:160:... }}{{ /if }}'
---

<meta name="description" content="{{ view:description }}">
>>>>>>> Unanswered <<<<<<<
1 Reply
2 Followers