Is there a shorthand syntax for outputting conditional text?

Is there a shorthand syntax for outputting conditional text?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Is there a shorthand syntax for outputting conditional text?

If you are working with Twig templates, you have likely encountered the standard {% if %} block to toggle between states. The pattern {{ info.id ? 'edit' : 'create' }} is clean enough, but when that logic needs to happen at the very start of a tag like an <h1>, developers often feel constrained by the verbosity of the {% if %} syntax versus the brevity of PHP's ternary operator.

The specific scenario involves determining whether a resource exists to decide between "create" and "edit" labels. In standard Twig, this typically looks like:

<h1>{% if not info.id %}create{% else %}edit{% endif %}</h1>

This is functionally correct and robust, but it does feel heavy for a simple boolean check. The temptation is to ask if there is a more compact equivalent that doesn't require the explicit if/else keywords.

In strict Twig syntax, there is no dedicated ternary operator like PHP's condition ? true_value : false_value. However, you can achieve significant brevity by leveraging the fact that not is a filter and that Twig supports short-circuit evaluation in certain contexts, though it does not support the ?: operator natively.

One common workaround involves using the default filter, but that only handles missing keys or falsy values, not explicit boolean logic for string interpolation. If you strictly need to output text based on a condition, the {% if %} block is actually the idiomatic solution in Twig. However, you can make it slightly cleaner by removing the else block entirely if you are okay with defaulting to the "create" state:

<h1>{{ not info.id ? 'create' : 'edit' }}</h1>

Wait, that won't work directly because Twig does not have a ternary operator. The syntax {{ not info.id ? 'create' : 'edit' }} is invalid in standard Twig versions. To make it valid, you must use the {% if %} block, or rely on logical operators combined with the or filter logic if you are chaining conditions, but for a simple exclusive choice, the block remains necessary.

There is a subtle distinction between PHP and Twig here that often trips developers coming from the former language. In PHP, $info['id'] ? 'create' : 'edit' relies on type coercion where an empty string or null evaluates to false. In Twig, you must explicitly check the existence of the key using is defined or simply access the property if it is guaranteed to exist in your data structure. The example provided shows info.id, implying that id is a property on an object or array accessed via dot notation. If id is missing entirely, accessing info.id might throw an error depending on how the data is passed to the template, whereas PHP's loose typing handles it more gracefully.

If you are using Laravel and want to streamline this further, consider passing the necessary boolean flag directly into the view context rather than relying on template logic for such a simple decision. You can render the string in your controller or service layer:

$heading = !is_null($info->id) ? 'edit' : 'create';
return view('resources.show', ['heading' => $heading]);

This moves the cognitive load away from the template and keeps the logic where it belongs. However, if you must keep the logic inside the Twig template for readability or separation of concerns, the {% if %} block is the standard tool.

Sometimes developers try to force a solution using filters like |default combined with logical operators, but Twig lacks the ternary operator syntax found in JavaScript, PHP, or Python. The most "shorthand" way within Twig's own ecosystem remains:

<h1>{% if info.id %}edit{% else %}create{% endif %}</h1>

This is concise enough that adding more characters to invent a non-existent operator usually results in a syntax error rather than improved readability. If you are building a large application, consider creating a helper function or macro for this specific pattern if it appears frequently across your codebase. For instance, defining a macro that accepts the condition and the two strings allows you to write {{ conditional('edit', 'create', info.id) }}, effectively bringing back the ternary feel without breaking Twig's grammar rules.

Ultimately, the trade-off between PHP-style brevity and Twig's verbosity is worth accepting for the sake of language consistency and safety.

Our network