As Laravel ships with Blade, the Laravel manual gives templating examples in Blade.
For displaying basic variables in a Twig template, it’s pretty simple:
[code language=”html”]
{{ title }}
[/code]
However, it takes slightly more effort to display form validation errors.
If you’re implementing authentication in Laravel and you generate the relevant auth classes, you’ll be given a batch of Blade templates. Not much good if you prefer Twig.
Here’s a Blade code snippet from the Validation quickstart guide:
[code language=”html”]
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach</ul>
</div>
@endif [/code]
It doesn’t take too long to translate this into Twig syntax, but to save you some time (and to stop you wondering if it’ll even work in Twig), here’s the equivalent Twig code.
[code language=”html”]
{% if errors.any() %}
<div class="alert alert-danger">
<ul>
{% for error in errors.all() %}
<li>{{ error }}</li>
{% endfor %}</ul>
</div>
{% endif %} [/code]
And if you don’t want to cut and paste this everywhere that you need it, why not create a Twig template with the above code inside, and simply include the template when you need it?
Easy when you know how!