cms icon indicating copy to clipboard operation
cms copied to clipboard

[5.x] Statamic Tag Blade Compiler

Open JohnathonKoster opened this issue 4 months ago • 0 comments

This PR adds a new tag compiler for Blade. Its primary goal is to make leveraging existing tags simpler when writing Blade templates. For example, in Antlers, you can use the collection tag like so:

{{ collection:blog }}
  {{ title }}
{{ /collection:blog }}

{{ collection:blog as="posts" }}

  {{ posts }}
    {{ title }}
  {{ /posts }}

{{ /collection:blog }}

with the changes in this PR, the following would now be possible in Blade:

<s:collection:blog>
  {{ $title }}
</s:collection:blog>

<s:collection:blog as="posts">
  @foreach ($posts as $post)
    {{ $post->title }}
  @endforeach
</s:collection:blog>

The internal compiler will compile the custom <s: tags to PHP behind the scenes.

This PR also adds a new @recursive_children directive, which is only intended to be used within the <s:nav tag:

<ul>
<s:nav:main>
<li>
    {{ $title }} - {{ $depth }}
    
    @if (count($children) > 0)
    <ul class="the-wrapper">
        @recursive_children
    </ul>
    @endif
</li>
</s:nav:main>
</ul>

-- or with an alias --

<ul>
  <s:nav:main as="the_items">
    @foreach ($the_items as $item)
      <li>{{ $item['title'] }} - {{ $item['depth'] }}</li>

      @if (isset($item['children']) && count($item['children']))
        <ul class="wrapper">
          @recursive_children($item['children'])
        </ul>
      @endif
    @endforeach
  </s:nav:main>
</ul>

Important: While they shares the same syntax as components, its important to think of these as "tags" and not traditional Blade components! For example, "slot" content shares the same scope as the outer template:

<?php $myVar = 0; ?>

<s:collection:blog>
  @php($myVar++)
</s:collection:blog>

Notes:

  1. Most tags will be compatible with this syntax with no additional work. Docs will be coming for handling the edge cases/etc.
  2. Some tags may behave differently than they do in Antlers. This is largely due to how Antlers handles null/empty arrays. When used in Blade, tags that have different behaviors have been adapted to fit much more naturally in Blade. Docs for these scenarios is in progress.

JohnathonKoster avatar Oct 17 '24 03:10 JohnathonKoster