<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michael Oteng</title>
    <description>The latest articles on DEV Community by Michael Oteng (@xetera1).</description>
    <link>https://dev.to/xetera1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4121886%2Fcedc935e-7231-4384-bfe7-adc8dfd1c4dc.png</url>
      <title>DEV Community: Michael Oteng</title>
      <link>https://dev.to/xetera1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xetera1"/>
    <language>en</language>
    <item>
      <title>Formatting Laravel Blade Templates: Why It Is Harder Than Formatting HTML</title>
      <dc:creator>Michael Oteng</dc:creator>
      <pubDate>Sat, 12 Sep 2026 14:34:11 +0000</pubDate>
      <link>https://dev.to/swiftvecto/formatting-laravel-blade-templates-why-it-is-harder-than-formatting-html-5h4h</link>
      <guid>https://dev.to/swiftvecto/formatting-laravel-blade-templates-why-it-is-harder-than-formatting-html-5h4h</guid>
      <description>&lt;p&gt;Laravel Blade templates can look deceptively simple.&lt;/p&gt;

&lt;p&gt;At first glance, a &lt;code&gt;.blade.php&lt;/code&gt; file is mostly HTML. That can make formatting one seem like the same problem as formatting an ordinary HTML document.&lt;/p&gt;

&lt;p&gt;It isn't.&lt;/p&gt;

&lt;p&gt;A Blade template can combine HTML with conditional directives, loops, components, slots, PHP expressions and Laravel-specific syntax, sometimes within the same few lines.&lt;/p&gt;

&lt;p&gt;Consider a deliberately badly formatted example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="user-card"&amp;gt;
@if($user)
@if($user-&amp;gt;active)
&amp;lt;h2&amp;gt;{{ $user-&amp;gt;name }}&amp;lt;/h2&amp;gt;
&amp;lt;p&amp;gt;{{ $user-&amp;gt;email }}&amp;lt;/p&amp;gt;
@if($user-&amp;gt;isAdmin())
&amp;lt;span class="badge"&amp;gt;Administrator&amp;lt;/span&amp;gt;
@elseif($user-&amp;gt;isEditor())
&amp;lt;span class="badge"&amp;gt;Editor&amp;lt;/span&amp;gt;
@else
&amp;lt;span class="badge"&amp;gt;User&amp;lt;/span&amp;gt;
@endif
@else
&amp;lt;p&amp;gt;This account is inactive.&amp;lt;/p&amp;gt;
@endif
@else
&amp;lt;p&amp;gt;No user is available.&amp;lt;/p&amp;gt;
@endif
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The HTML itself is not particularly complicated. The difficulty comes from understanding the structure created by the Blade directives around it.&lt;/p&gt;

&lt;p&gt;A formatter needs to recognise that &lt;code&gt;@if&lt;/code&gt;, &lt;code&gt;@elseif&lt;/code&gt;, &lt;code&gt;@else&lt;/code&gt; and &lt;code&gt;@endif&lt;/code&gt; are related control structures. It needs to understand how the HTML is nested inside those structures while also preserving Blade expressions such as &lt;code&gt;{{ $user-&amp;gt;name }}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Simply treating the file as HTML is therefore not enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a Blade-aware formatter needs to understand
&lt;/h2&gt;

&lt;p&gt;Formatting HTML is largely concerned with the hierarchy of elements.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;section&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The opening and closing tags provide most of the information needed to understand the structure.&lt;/p&gt;

&lt;p&gt;Blade introduces another structural layer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@if ($user)
    &amp;lt;div&amp;gt;
        @foreach ($user-&amp;gt;notifications as $notification)
            &amp;lt;p&amp;gt;{{ $notification-&amp;gt;message }}&amp;lt;/p&amp;gt;
        @endforeach
    &amp;lt;/div&amp;gt;
@endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are now at least three things a formatter needs to reason about:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The HTML structure.&lt;/li&gt;
&lt;li&gt;The Blade directive structure.&lt;/li&gt;
&lt;li&gt;The expressions embedded within the template.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Real templates can become considerably more complicated when components, slots, conditional attributes, sections and other Blade features are introduced.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;x-layout&amp;gt;
    &amp;lt;x-slot:title&amp;gt;
        {{ $pageTitle }}
    &amp;lt;/x-slot:title&amp;gt;

    @if ($user-&amp;gt;isAdmin())
        &amp;lt;x-admin.dashboard :user="$user" /&amp;gt;
    @else
        &amp;lt;x-user.dashboard :user="$user" /&amp;gt;
    @endif
&amp;lt;/x-layout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A formatter that only understands HTML does not have a complete picture of that document.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should our badly formatted example become?
&lt;/h2&gt;

&lt;p&gt;Running the original example through a Blade-aware formatter produces a much clearer structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="user-card"&amp;gt;
    @if ($user)
        @if ($user-&amp;gt;active)
            &amp;lt;h2&amp;gt;{{ $user-&amp;gt;name }}&amp;lt;/h2&amp;gt;
            &amp;lt;p&amp;gt;{{ $user-&amp;gt;email }}&amp;lt;/p&amp;gt;
            @if ($user-&amp;gt;isAdmin())
                &amp;lt;span class="badge"&amp;gt;Administrator&amp;lt;/span&amp;gt;
            @elseif ($user-&amp;gt;isEditor())
                &amp;lt;span class="badge"&amp;gt;Editor&amp;lt;/span&amp;gt;
            @else
                &amp;lt;span class="badge"&amp;gt;User&amp;lt;/span&amp;gt;
            @endif
        @else
            &amp;lt;p&amp;gt;This account is inactive.&amp;lt;/p&amp;gt;
        @endif
    @else
        &amp;lt;p&amp;gt;No user is available.&amp;lt;/p&amp;gt;
    @endif
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing particularly exciting happened to the application logic.&lt;/p&gt;

&lt;p&gt;That's the point.&lt;/p&gt;

&lt;p&gt;Formatting isn't supposed to redesign the template. It makes its existing structure easier for a human to see.&lt;/p&gt;

&lt;p&gt;The relationship between the outer user check, the account status check and the role check is now immediately apparent.&lt;/p&gt;

&lt;p&gt;There is also a smaller change that is easy to overlook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@if($user)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;became:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@if ($user)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consistent spacing and indentation might seem cosmetic, but across a large collection of views they reduce unnecessary variation and make templates easier to scan.&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting is not linting
&lt;/h2&gt;

&lt;p&gt;This distinction is important.&lt;/p&gt;

&lt;p&gt;A formatter can make code consistent without determining whether that code represents a good implementation.&lt;/p&gt;

&lt;p&gt;For example, this can be beautifully formatted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@if ($conditionOne)
    @if ($conditionTwo)
        @if ($conditionThree)
            @if ($conditionFour)
                ...
            @endif
        @endif
    @endif
@endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But formatting does not answer whether four levels of nested conditions belong in the template.&lt;/p&gt;

&lt;p&gt;Perhaps some of that logic should move elsewhere. Perhaps a Blade component would make the view clearer. Perhaps the underlying design needs reconsideration.&lt;/p&gt;

&lt;p&gt;Similarly, a formatter cannot necessarily tell you that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a route name does not exist;&lt;/li&gt;
&lt;li&gt;a variable was never passed to the view;&lt;/li&gt;
&lt;li&gt;a component is missing;&lt;/li&gt;
&lt;li&gt;an included view does not exist;&lt;/li&gt;
&lt;li&gt;the business logic is incorrect.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Formatting improves presentation. It should not be confused with validation, static analysis, testing or application design.&lt;/p&gt;

&lt;h2&gt;
  
  
  Blade formatting options
&lt;/h2&gt;

&lt;p&gt;There are several ways to approach Blade formatting.&lt;/p&gt;

&lt;p&gt;Editor extensions can format templates as part of the development workflow. Blade-aware Prettier tooling can provide automated formatting, and dedicated Blade formatter projects are available for developers who want formatting integrated into their local environment.&lt;/p&gt;

&lt;p&gt;Laravel Pint also supports Blade formatting.&lt;/p&gt;

&lt;p&gt;Blade formatting is not enabled by default in Pint, but it can be invoked with the &lt;code&gt;--blade&lt;/code&gt; option or configured using the &lt;code&gt;Pint/laravel_blade&lt;/code&gt; rule.&lt;/p&gt;

&lt;p&gt;For a Laravel project where Pint is already part of the development workflow, that can be a natural place to enforce formatting consistently.&lt;/p&gt;

&lt;p&gt;There are also situations where you may not want to configure anything locally.&lt;/p&gt;

&lt;p&gt;Perhaps you have been sent a Blade fragment.&lt;/p&gt;

&lt;p&gt;Perhaps you are reviewing an example outside the original project.&lt;/p&gt;

&lt;p&gt;Perhaps you are working on a machine without the project's development environment.&lt;/p&gt;

&lt;p&gt;Or perhaps you simply want to paste a template somewhere, format it and copy the result.&lt;/p&gt;

&lt;p&gt;That is where a browser-based formatter can be useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building a browser-based Blade formatter
&lt;/h2&gt;

&lt;p&gt;While developing SwiftVecto, I wanted the Laravel Blade Formatter to work without requiring the user to upload a Blade file or configure a Laravel project.&lt;/p&gt;

&lt;p&gt;The formatter therefore operates directly in the browser using Blade-aware formatting tooling.&lt;/p&gt;

&lt;p&gt;The basic workflow is intentionally simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Blade source
     ↓
Blade-aware formatter
     ↓
Formatted template
     ↓
Copy result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tool also analyses the source separately to provide information such as directive counts, echo expressions, components, slots, loops, conditionals, sections and approximate directive nesting depth.&lt;/p&gt;

&lt;p&gt;Those statistics aren't part of formatting itself. They are simply another way of getting a quick picture of the structure of a template.&lt;/p&gt;

&lt;p&gt;The important architectural distinction is that normal interactive formatting is performed in the browser rather than sending the Blade source to the server for processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating reusable formatting fixtures
&lt;/h2&gt;

&lt;p&gt;While testing formatting behaviour, another useful idea emerged.&lt;/p&gt;

&lt;p&gt;Instead of keeping test examples buried inside development notes, why not make them reusable?&lt;/p&gt;

&lt;p&gt;I created a public SwiftVecto repository for Blade formatting examples and fixtures.&lt;/p&gt;

&lt;p&gt;A simple fixture structure can contain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fixtures/
├── unformatted/
│   └── conditionals.blade.php
└── expected/
    └── conditionals.blade.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The unformatted version provides a reproducible input.&lt;/p&gt;

&lt;p&gt;The expected version provides a reference result.&lt;/p&gt;

&lt;p&gt;That makes it easier to experiment with different formatting workflows and, as the fixture collection grows, test more interesting Blade structures.&lt;/p&gt;

&lt;p&gt;The expected output should not necessarily be treated as the one canonical way every Blade template must look. Formatter versions, configuration and formatting rules can legitimately produce different results.&lt;/p&gt;

&lt;p&gt;The important part is having reproducible examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try the example yourself
&lt;/h2&gt;

&lt;p&gt;The example used in this article is available as part of the public Blade formatting fixtures:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/SwiftVecto/blade-formatting-examples" rel="noopener noreferrer"&gt;View the Blade formatting examples on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also paste the unformatted example directly into the browser-based formatter:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://swiftvecto.com/tool/laravel-blade-formatter" rel="noopener noreferrer"&gt;Try the SwiftVecto Laravel Blade Formatter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;No Laravel project is required to experiment with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Formatting will not make a Blade template well designed.&lt;/p&gt;

&lt;p&gt;It will not fix application logic, replace testing or tell you whether a view has become too complicated.&lt;/p&gt;

&lt;p&gt;What it can do is remove unnecessary visual inconsistency and make the structure that already exists considerably easier to reason about.&lt;/p&gt;

&lt;p&gt;And with Blade, that structure involves more than HTML.&lt;/p&gt;

&lt;p&gt;As the public fixture collection grows, I plan to add examples covering components, slots, loops, layouts, forms, convenience directives and some of the more awkward structures that can appear in real Blade templates.&lt;/p&gt;

&lt;p&gt;If you have an interesting Blade formatting edge case, I'd be interested to see it.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
