Closed beck24 closed 2 years ago
Weird. Can you provide the rest of the template?
Sure, kind of, I've been trying to work around it but I'm still getting the error when I do this:
{{ if {partial:exists src="forms/{form.handle}"} }}
{{ partial src="forms/{form.handle}"}}
{{ else }}
{{ form:create in="{ form.handle }" }}
{{ fields }}
<div class="p-2">
<label>
{{ display }}
{{ if validate | contains:required }}
<sup class="text-red">*</sup>
{{ /if }}
</label>
<div class="p-1">{{ field }}</div>
{{ if error }}
<p class="text-gray-500">{{ error }}</p>
{{ /if }}
</div>
{{ /fields }}
<input type="submit" value="Submit">
{{ /form:create }}
{{ /if }}
What I'm trying to do is build a view that handles generic forms created by a user in the CMS. However there are a couple of specific forms that are going to need some specific template handling, so I wanted to make it so I could just create the template based on the form handle and drop it in.
Instead I'm having to do something like:
{{ if form.handle === 'specific_form_1' }}
{{ partial:forms/specific_form_1 }}
{{ elseif form.handle === 'specific_form_2' }}
{{ partial:forms/specific_form_2 }}
{{ else }}
// generic template
{{ /if }}
But as you can see, new specific forms would need both a new template and be added to this if/else statement in this template.
The only thing I can think of is that in other views rendered before this I have already used the {{ partial:path/to/view }}
shorthand syntax. Could using the shorthand previously be affecting how this is interpreted? It shouldn't :)
Also possibly relevant {{ partial:if_exists src="path/to/view" }}
appears to be working just fine, it just doesn't have an else
that I need
Digging into the Statamic code a bit here.
if (! $partial = $this->params->get('src')) {
return $this->wildcard('exists');
}
This is why it's trying to render a view called 'exists' - because $partial
is null, but it's clearly being passed. I've even taken out the variable to pass an explicit string that I know resolves to a valid view.
Digging further this is because this is called from within a partial, it's a nested partial.
eg.
// template
{{ partial src="partial1" }}
// partial 1
{{ if {partial:exists src="maybe/exists/partial2"} }}
// now we're hitting the exception
{{ /if }}
Now the Partial tag for the exists check gets the following parameters:
[ 'partial1' => 'maybe/exists/partial2' ]
Instead of the expected
['src' => 'maybe/exists/partial2']
Nice detective work!
Bug description
Using a conditional for whether the partial exists as per the documentation here:
https://statamic.dev/tags/partial-exists
I get this exception, it appears it's not treating it as it's own tag, but attempting to use the shorthand {partial:path/to/view} and the view 'exists' doesn't exist. I'm using it exactly as documented though, so I can't see how it's user error (though if it is please do let me know how/where).
How to reproduce
This is the relevant part of my template
Logs
Versions
Statamic 3.2.28 Pro Laravel 8.77.1 PHP 7.4.26 arckinteractive/statamic-bard-custom-class dev-master arckinteractive/statamic-bard-font-size 1.0.0 arckinteractive/statamic-bard-font-weight 1.0.1 doublethreedigital/duplicator 2.0.1 goldnead/statamic-collapse-fieldtype 1.0.4 optimoapps/statamic-bard-text-align 1.0.2 xndbogdan/statamic-bard-text-color 2.2
Installation
Existing Laravel app
Additional details
No response