Skip to main content
added 51 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140

Testing for the pre-populated variables is probably the only way to do this. But as you're asking for a single (global) var to test against, I believe you're looking for a way to have clean, dry code in your shared code block without using your posted snippet again and again.

How about doing something like this:

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

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

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

Another thing you could do is to set a new var model, which holds either the Entry Model or the Category Model.

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

<h1>{{ model.title }}</h1>

Testing for the pre-populated variables is probably the only way to do this. But as you're asking for a single (global) var to test against, I believe you're looking for a way to have clean, dry code in your shared code block without using your posted snippet again and again.

How about doing something like this:

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

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

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

Another thing you could do is to set a new var model, which holds either the Entry Model or the Category Model.

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

<h1>{{ model.title }}</h1>

Testing for the pre-populated variables is probably the only way to do this. But as you're asking for a single (global) var to test against, I believe you're looking for a way to have clean, dry code in your shared code block without using your posted snippet again and again.

How about doing something like this:

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

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

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

Another thing you could do is to set a new var model, which holds either the Entry Model or the Category Model.

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

<h1>{{ model.title }}</h1>
added 240 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140

ToTesting for the pre-populated variables is probably the only way to do this. But as you're asking for a single (global) var to test against, I believe you're looking for a way to have clean, dry code in your shared code block you can dowithout using your posted snippet again and again.

How about doing something like this:

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

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

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

Another thing you could do is to set a new var model, which holds either the Entry Model or the Category Model.

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

<h1>{{ model.title }}</h1>

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

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

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

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

Another thing you could do is to set a new var model, which holds either the Entry Model or the Category Model.

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

<h1>{{ model.title }}</h1>

Testing for the pre-populated variables is probably the only way to do this. But as you're asking for a single (global) var to test against, I believe you're looking for a way to have clean, dry code in your shared code block without using your posted snippet again and again.

How about doing something like this:

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

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

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

Another thing you could do is to set a new var model, which holds either the Entry Model or the Category Model.

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

<h1>{{ model.title }}</h1>
added 16 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140

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

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

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

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

Another thing you could do is to set a new var model, which holds either the Entry Model or the Category Model.

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

<h1>{{ model.title }}</h1>

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 very short ternary syntax in your print expressions.

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

Another thing you could do is to set a new var model, which holds either the Entry Model or the Category Model.

{% 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>

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

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

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

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

Another thing you could do is to set a new var model, which holds either the Entry Model or the Category Model.

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

<h1>{{ model.title }}</h1>
added 16 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
added 129 characters in body
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading
Source Link
carlcs
  • 36.3k
  • 5
  • 63
  • 140
Loading