1

Is it possible to write this as a one-liner?

{% if craft.entries.section('mypage').one() != null %}
  {{ craft.entries.section('mypage').one().title }} {# If title is provided - use, but don't translate #}
{% else %}
  {{ "My page"|t }} {# Default value should be translated #}
{% endif %}

This is my solution so far, however this will translate both the default and the provided title. Is it possible to group these filters somehow to not translate the title?

{{ craft.entries.section('mypage').one().title|default("My page")|t }}

2 Answers 2

2

You can put the t filter inside the default filter, this way it only applies to the default value:

{{ craft.entries.section('mypage').one().title|default("My page"|t) }}
0
1

Moritz's answer should be correct. For readability I'd like to propose something like that:

{% set mypage = craft.entries.section('mypage').one() %}
{% set title = mypage ? mypage.title : "default title"|t %}


{{ title }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.