Skip to content

Blade vs Twig Reference

If you've previously developed themes with the legacy Laminas/Twig system, this page is your translation guide. Everything you could do in Twig has a Blade equivalent.


Syntax Comparison

Printing Variables

{{ variable }}
{{ variable | raw }}
{{ variable | escape }}
{{ $variable }}       {# escaped --}}
{!! $variable !!}     {# raw / unescaped --}}
{{ e($variable) }}    {# explicit escape --}}

Security

Always use {{ }} for user-generated content (auto-escapes HTML). Only use {!! !!} for trusted HTML from the database or known-safe helper functions.


Conditionals

{% if condition %}
    ...
{% elseif other %}
    ...
{% else %}
    ...
{% endif %}
@if($condition)
    ...
@elseif($other)
    ...
@else
    ...
@endif

Loops

{% for product in products %}
    {{ product.name }}
{% endfor %}

{% for item in items %}
    {{ loop.index }}  {# 1-based index --}}
{% endfor %}
@foreach($products as $product)
    {{ $product['name'] }}
@endforeach

@foreach($items as $index => $item)
    {{ $loop->index }}   {{-- 0-based --}}
    {{ $loop->iteration }} {{-- 1-based --}}
    {{ $loop->first }}   {{-- true on first --}}
    {{ $loop->last }}    {{-- true on last --}}
@endforeach

Template Inheritance

{% extends 'layout.twig' %}

{% block content %}
    <p>Content here</p>
{% endblock %}

{% block headtitle %}
    <title>My Page</title>
{% endblock %}
@extends('layout.layout')

@section('content')
    <p>Content here</p>
@endsection

@section('headtitle')
    <title>My Page</title>
@endsection

Including Partials

{% include 'partial/header.twig' %}
{% include 'partial/header.twig' with {'title': 'My Title'} %}
@include('partial.header')
@include('partial.header', ['title' => 'My Title'])

Setting Variables

{% set myVar = 'hello' %}
{% set price = product.normalPrice %}
@php $myVar = 'hello'; @endphp
@php $price = $product['normalPrice']; @endphp

Object vs Array Access

In Twig, both objects and arrays use dot notation:

{{ product.name }}
{{ page.settings.siteTitle }}

In Blade, arrays use ['key'] and objects use ->property:

{{-- Product from a helper like featureProducts() is an array --}}
{{ $product['name'] }}

{{-- $page->settings is an object --}}
{{ $page->settings->siteTitle }}


Filters → PHP Functions

Twig filters become PHP function calls in Blade:

Twig Filter Blade Equivalent
\| upper {{ strtoupper($var) }}
\| lower {{ strtolower($var) }}
\| trim {{ trim($var) }}
\| length {{ twig_length($var) }} or {{ count($var) }}
\| date('Y-m-d') {{ twig_date($var, 'Y-m-d') }}
\| default('fallback') {{ twig_default($var, 'fallback') }} or {{ $var ?? 'fallback' }}
\| first {{ twig_first($var) }}
\| raw {!! $var !!}

Comment Syntax

{# This is a Twig comment #}
{{-- This is a Blade comment --}}

Auth Checks

{% if page.user %}
    Logged in as {{ page.user.email }}
{% endif %}
@auth('customer')
    Logged in as {{ auth('customer')->user()->email }}
@else
    Not logged in
@endauth

Or use $page->user:

@if($page->user)
    Logged in as {{ $page->user->email }}
@endif


Helper Function Migration

Twig Helper Blade Equivalent
featureProducts() featureProducts() ✅ same
latestProducts() latestProducts() ✅ same
seasonsBestProduct(n) seasonsBestProduct(n) ✅ same
discountedProducts() discountedProducts() ✅ same
visitedProducts(identity()) getVisitedProducts()
MostviewedProducts() getVisitedProducts()
hybridauthinfo(provider, serverUrl(true)) hybridAuth(route('user.login', [], false), 'provider')
tagPrice(value) tagPrice($value) ✅ same
tagShortText(text, len) tagShortText($text, $len) ✅ same
tagDescription(text, len) tagShortText($text, $len) (unified)
getAlt(image) getAlt($image, $fallback)
serverUrl() serverUrl() ✅ same
getPath(themes.image) getPath($themes->image)
bulkBuyer() bulkBuyer() ✅ same
identity() identity() ✅ same
getCart('cartProductQuantity') getCart('cartProductQuantity') ✅ same
url('user/login') route('user.login', [], false) or customUrl('login')
dumpVar() {{ dump($variable) }} or @dump($variable)