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
Feedback (v2-beta): Proposed template rendering, and theming #937
Comments
Thanks @engram-design it looks like a lot of thought has been put into different approaches for customising the template rendering and theming, it's certainly something that is difficult to get right and strike a good balance between customisation and convention. In my mind being able to define a global configuration options via a In saying that however, I feel that perhaps the idea of adding css/tailwindcss classes within a php config file may feel a little off for some developers, this goes for events as well. I wonder if global configuration could be handled through the use of an imported twig macro that wraps There are definitely some situations I can think of where you may want to honour you global configuration options for the most part, but then override one or two of the options for a specific form/form field. For most frontend developers looking to theme their forms I would say that this needs to be achievable via twig. Being able to disable the output of certain form elements is a welcome improvement as well. There has definitely been some situations where I feel that I have been tripping over the standard output to do what I want. The 'Twig tags' definitely get my vote for the preferred syntax. |
Added for the next release. To get this fix early, change your
Then run |
This looks excellent, well explained and thought through. |
Gearing up for a final v2 release, and all documentation on this can be now found here |
Thanks for this Josh.
I had read through your initial post with much trepidation as some of it I
struggled to follow as atwig newbie. Having read through the documentation (
https://verbb.io/craft-plugins/formie/docs/theming/theme-config) I feel
much more comfortable that this is approachable even for non experts.
Appreciate all your work.
Jubal
…On Thu, 7 July 2022, 9:57 am Josh Crawford, ***@***.***> wrote:
Gearing up for a final v2 release, and all documentation on this can be
now found here
<https://verbb.io/craft-plugins/formie/docs/theming/overview>
—
Reply to this email directly, view it on GitHub
<#937 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJLJKZ7DCLYJHMMNLAQUFWTVSYMNVANCNFSM5YXS4OFA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
@jubalj It's a bit to wrap your head around for sure, but appreciate the feedback on the docs! |
Lots of hard work here, well done and thanks! I'm excited about having a config file that can be applied to every form. |
We've been busy tinkering with a new way to define custom templates, for when you want to customise the HTML output from default Formie templates. The purpose of this issue is to discuss options on how best proceed, if the community is comfy with the proposed changes, and iterate.
Before we finalise development, we want YOUR feedback, so for all interested, read carefully and let us know what you think.
To summarise the current limitations with rendering forms via
craft.formie.renderForm()
when it comes to customising and theming..fui-input
on an input or adding your own classes and attributes.<div>
elements you might not need, like the wrapper.fui-input-container
around every<input>
element.<form id="...">
, or<form config="...">
) can really mess things up.While custom templates have been the answer (and will continue to be) if you want total, 100% control over how a form renders, we want an easier way to "theme" templates without having to resort to brand-new Twig files you need to maintain.
Proposal
What we're proposing is a revamped "Render Options" where you supply a configuration object for specific tags for the form and fields. This configuration object essentially is the configuration for a HTML tag, providing the tag name and attributes. This gives you the control of not only what HTML tag to use, but the attributes (including classes), and even whether or not to output them.
The first step is defining a set of "tag categories" that Formie uses for HTML elements from the
<form>
tag, all the way down to<input>
tags. This will be a consistent API that allows you to target Formie's HTML elements, and won't change as we develop Formie.To put it visually, the following HTML structure:
Would be now defined by the following Formie "tag categories":
Through some Twig magic (notice the
{% formtag %}
Twig tag), we'd render the HTML exactly the same, however the attributes and the HTML tag are now routed through PHP, which allows us to give you greater control.In addition, the above only shows categories up until the individual field. We'd have a similar structure for a field, so you can define HTML content for all aspects of a field.
The above mimics the same structure as the HTML currently, but again, using our other Twig tag
{% fieldtag %}
we can route this through the Field class to have some fun with it.As you can see, we're trying to setup a consistent API of "tag categories" that represents the HTML structure used by Formie - but without relying on the HTML elements - instead giving them names.
Let's move on to usage.
Usage
There's 3 different ways to use this new functionality. Let's look at some examples to add Tailwind classes to elements.
1. Events
Let's say we want to add a red border to the
<input>
element for a Single-Line Text field, and some padding to the field wrapper.Producing:
Here, we directly modify
$event->tag
's attributes to completely replace Formie's classes on that element. You could also change the HTML tag itself to something else, or append classes instead of replacing them.Producing:
This approach will be the same for editing form-level tags.
Producing:
2. Form Render Config
You can also pass in config at render time, when calling
craft.formie.renderForm()
.In this example we supply a config object to the form to use at render. You'll also notice the use of
tag
to override the HTML tag used andresetClass
for when you want to not just add classes to existing Formie classes. Instead, only the classes you provide will be used.3. Custom Templates
You can still override a template or template partial and make use of the "tag categories" functionality. For example, let's say we wanted to add some attributes to the
<input>
tag on a Single-Line Text field. We would create afields/single-line-text.html
file in our own project, and use the following:Or, another example might be adding attributes on the
<form>
element. You could accomplish this by still using our rendering handling (which will still work with events and form render config) withwith
:So even when using custom templates, you'll have the option to keep using
{% formtag %}
and{% fieldtag %}
to modify elements, or go on your own with just plain ol' HTML like you've always done.Removing Tags
Until now, we've just seen examples of manipulating attributes and the HTML tags by adding or replacing classes and attributes. What if we wanted to prevent an element from being output at all? Fortunately, that's easy!
Look at a typical field output:
There's a few reasons we add those extra
<div>
containers, but maybe you'd like to ditch them!Producing:
Other input types
Of course, not every input is the same. For option-based fields, using
<fieldset>
and<legend>
is pretty important. Fortunately, we can handle that, by changing the HTML tag and attribute, like we've been doing.(note you won't need to do this, Formie's Radio Buttons / Checkboxes / Dropdown fields will do this - it's just an example of what you yourself could do to a field).
This changed the
wrapper
andlabel
categories to use<fieldset>
and<legend>
for proper semantic and accessible markup.Summary
As you can see, we've moved from using HTML to a
tag
-heavy approach which gives us more flexibility with configuring HTML, at the cost of a potential learning curve for newcomers, expecting to edit raw HTML.Things to consider
1. No HTML in templates
With no HTML in Formie's templates (
templates/_special/form-templates
), this might make it difficult to "get started" with your BYO HTML. For example, this is what thetemplates/_special/form-templates/field.html
template would look like:That's pretty daunting for users wanting to edit the
field.html
template.But, maybe this just needs to be solved by providing example raw HTML templates (like the ones we have now) via the docs for people to use as a starter.
2. How to define config to both all field types or just specific ones
We'd want the option to configure rendering at the field type level (so for all Single-Line Text fields), or per field instance (a field with the handle
emailAddress
).3.
formie.php
config file configWould it be useful to have the option to define config in the
config/formie.php
config file, so it doesn't need to be included at eachcraft.formie.renderForm()
call? It could also be transferred to another project easily.4. Syntax for templates
Which do we prefer!
Option 1: Twig tags
Option 2: Twig functions
Let's discuss!
The text was updated successfully, but these errors were encountered: