-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add support for forms in Blade templates #792
Conversation
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.
Thanks for this PR - it looks great! 🙌
I hope you don't mind, there's a couple of changes I'd like to make:
1. Form Params
Instead of requiring developers to need to loop through Simple Commerce's form parameters, I wonder if it might be easier for them to do something like this:
@php
$form = Statamic::tag('sc:cart:addItem')->params([
'redirect' => '/cart',
])->fetch();
@endphp
<form action="{{ $form['attrs']['action'] }}" method="{{ $form['attrs']['method'] }}">
{!! $formParams !!}
<input type="hidden" name="product" value="{{ $id }}" />
<input type="text" name="quantity" value="1" />
<button>Add to Cart</button>
</form>
In this case, $formParams
would be a string containing the hidden inputs etc needed for the form. It leaves the developer with slightly cleaner code.
Tests
I don't use Blade personally in Statamic projects so I'd like to ensure it's covered in case I touch something which breaks it.
If you have some time, would you be able to add in some tests to this PR? No worries if not, I can take a look.
Agreed, TBH I never loved the fact that you have to loop those with the original core PR, but that was really just a quick fix to expose the raw data so it was usable in Blade. How about best of both worlds? Keep the arrays of FYI, I’m working on a core PR that might make this even simpler in future. |
Yeah, I'm happy to have both and array and a string with the HTML for the form parameters. 👍 |
I've updated this with new I've also fixed what I think is a bug. The |
Thanks! I'll review this when I can. |
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.
This is all working great, thank you!
I've added some tests to ensure the item
isn't added as parameters to the <form>
tags and I've added a note to the documentation.
This will be included in the next release.
Released as part of v4.5.3. |
This PR adds support for the SC forms in Blade templates. It uses the same approach as statamic/cms#5892.
When the tags are called via
Statamic::tag(...)
the Antlers parser isn't set, so you can use that to check whether it's being called via Blade and return the raw form data instead, rather than the usual Antlers parsing. All the other variables that SC usually makes available inside the form tags is included in the returned array.I haven't tested every form, but since all the SC forms go through the same
createForm
method this should work for all of them.Usage Examples
Add to Cart Form
Remove from Cart Form
Cart Update Form