ideas
ideas copied to clipboard
Can we use wrapping tags in Blade?
Bug description
It doesn't seem like it's possible in Blade to use tags where content is added between an opening and closing tag. For example, the cache tag:
Antlers:
{{ cache for="5 minutes" }}
<!-- probably lots of stuff happening -->
{{ /cache }}
There are a few tags that work this way and it would be nice to have examples in the docs of how to use them in the Blade.
I'm pretty sure this doesn't work:
Blade:
@php(Statamic::tag('cache')->for('5 minutes'))
<!-- probably lots of stuff happening -->
@php(Statamic::tag('/cache'))
How to reproduce
N/A
Logs
No response
Versions
Statamic 3.3.11 Pro Laravel 9.12.2 PHP 8.1.1
Installation
Existing Laravel app
Antlers Parser
No response
Additional details
No response
Probably need an endTag('cache') method, but likely also need to pass the contents in via a slot or something.
At the moment, no.
But potentially we could have a Statamic::startTag('cache') and Statamic::endTag('cache'), where the start tag could do ob_start and ob_get_clean to capture the contents.
Or. Other ideas. đŸ˜„
I imagine it's possible to build somehow, but at the moment there's no built in way to do it.
I'm going to move this to the ideas repo.
Blade directives seem perfect for this sort of thing but I guess they aren't entirely compatible with antler tags since that approach was abandoned.
Perhaps there could be a single blade directive for these "wrapping" tags?
@tag('cache', ['for' => '5 minutes'])
{{--content--}}
@endtag
If you want to cache chunks of your blade templates, you can use this package: https://github.com/ryangjchandler/blade-cache-directive
@jasonvarga Thanks. Looks good.
I just used the cache tag as an example. My issue was aimed more at the usage of these sorts of tags in general, though after further inspection, it looks like there aren't that many of them. The upcoming no_cache tag looks like an important one though.
I've just ran into this. You can't use form tags like {{ form:create }} (or similar ones provided by addons) in Blade because of no support for wrapping tags.
@duncanmcclean
For SC could you implement the same parser check that the built-in forms have:
https://github.com/statamic/cms/blob/f1f653dad2de0b31fbb1ff99710398ad80cd3fb8/src/Forms/Tags.php#L104-L109
It wouldn't allow you to do wrapping, but Blade users could then do something like:
$form = Statamic::tag('sc:checkout')->params([
'redirect' => '/thanks',
])->fetch();
To get all the bits necessary to build the form themselves.
Before this PR was merged I did something similar to this when building a Simple Commerce site with Blade.
Thanks @jacksleight!
Unfortunately, I think it only really works if they're "wrapping tags" because otherwise you can't add your own HTML in the middle for inputs/etc.
@duncanmcclean
I don't think I explained it well. When you call Statamic::tag('form:contact') (for example), it doesn't return a complete <form> tag anymore, it just returns an array of tag attributes, hidden inputs, and a few other things.
With those values you can then write the <form>...</form> tag yourself, along with all the inputs inside.
It's not a general "wrapping tags in Blade" solution, but it makes using forms (that implement the parser check) possible.
Ah okay, gotcha! I'll give it a go later. Thanks!