-
-
Notifications
You must be signed in to change notification settings - Fork 539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.x] Statamic Tag Blade Compiler #10967
[5.x] Statamic Tag Blade Compiler #10967
Conversation
Into edge-case compatibility with this one
Go on little guy. go play with your friends
This makes it so the Blade-version is not entirely reliant on the contents of the tag.
This makes the following type of Blade template "Just Work": ```blade <s:glide :src="$multiple_assets" width="600" as="assets"> @foreach ($assets as $asset) {{ $asset['url'] }} @Endforeach </s:glide> ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I've done something dumb, it seems that nested tags don't work.
<s:trans key="Hello" /> // works fine out here
<s:collection:articles>
<s:trans key="Hello" /> // does nothing in here
</s:collection:articles>
@JohnathonKoster This looks really neat. Would this in theory make it easier to integrate other templating engines as well or is this specifically tailored to Blade? There are addons for Twig and Latte and maybe this allows a few new tricks to make those integrations more seamless. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that nested partials don't work.
one.blade.php
just some text
two.blade.php
{{ $slot }}
<s:partial:one /> this partial renders
<s:partial:two>
this text renders
<s:partial:one /> this partial doesnt render
</s:partial:two>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That worked great, except now I'm noticing that slot content is escaped.
bar.blade.php
<b>text</b>
foo.blade.php
{{ $slot }}
<s:partial:bar />
<s:partial:foo>
<s:partial:bar />
</s:partial:foo>
outputs
<b>text</b>
<b>text</b>
When using Blade components, {{ $slot }}
doesn't escape. It looks like they are dealing with Htmlable
objects. (ComponentSlot
)
The concept itself could be applied to other templating languages, but this implementation is tailored to Blade (interacting with Blade's loop variables, attribute/parameter syntax & behavior, slot behavior, etc.). |
Wow this is an amazing improvement! Thanks! |
Much more thorough documentation/information can be found over here: statamic/docs#1473
Overview
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:
with the changes in this PR, the following would now be possible in Blade:
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:Helper Functions
This PR also introduces a small number of namespaced helper functions. These can be imported at the top of a Blade template by using
use function
. Each helper function aims to reduce friction/help in a very specific way.value
The first of these is the
value
helper function. It is intended to be used in conditions (and other similar scenarios). It resolvesValue
instances, and a few other things for you automatically so you don't have to remember to do it each time (or if something changes in the future where it now returnsValue
where it didn't before):The
value
helper function will handle the following scenarios for you:Statamic\Fields\Value
objects (calls->value()
)Statamic\Fields\Values
objects (calls->all()
)Statamic\Tags\FluentTag
objects (calls->fetch()
)Statamic\Modifiers\Modify
objects (calls->fetch()
)modify
The
modify
helper function is simply a shortcut to callingStatamic::modify
. Once imported, you can replace allStatamic::modify
calls withmodify
:void
When using tags in Antlers, you can "remove" a parameter conditionally by using the
void
keyword:The above is equivalent to:
We can use the
void()
helper function to accomplish the same thing in Blade:Collecting them all
Just a quick example on what it'd look like if you wanted to collect all of the new Blade helper functions in your template:
Notes