Skip to main content
2 of 6
added 129 characters in body
carlcs
  • 36.3k
  • 5
  • 63
  • 140

To have cleaner, dry code in your shared code block you can do something like this:

{% set entry = entry is defined ?: false %}
{% set category = category is defined ?: false %}

Then your two variables entry and category will always be defined, which allows you to use short ternary syntax in your print expressions.

<div class="myDefaultClass{{ entry ? ' myEntryClass' }}"></div>

You could even set a new var model, which holds the Entry Model or the Category Model if one is set.

{% set model = entry is defined ?: category is defined ?: false %}
{% set entry = entry is defined ?: false %}
{% set category = category is defined ?: false %}

<h1>{{ model.title }}</h1>
carlcs
  • 36.3k
  • 5
  • 63
  • 140