In order to layout faster and to support the project in an easier way, we have made a pug mixin for form elements. It looks like a big sausage code:
However, in reality it is a very simple mixin to use. With it, we can do the following types of form elements:
input(type=”text”)
input(type=”password”)
input(type=”email”)
input(type=”checkbox”)
input(type=”radio”)
select
textarea
One example of a fetch:
Let’s first see how mixin works.
Note that the mixin has one parameter - config. This is the object that will contain our options. It the beginning, we declare the defaults. These values will be further used in mixin. If the value is not specified, we replace it with an empty string:
Next, we edit the identifier of our field. If it is not specified, it will be equal to the title parameter.
The essence of this operation is to bring the field id into a valid form. Let’s look at these transformations using an example (before-after):
After that, mixin is divided into 2 main parts depending on the type of field. If it is input (type = “text”), input (type = “password”), input (type = “email”), select, textarea we will use the structure dl, dt, dd:
We use this structure because it is the most semantic. You can read more about it in an article on
To add modifier classes in a mixin, there is a class_array parameter (an array of classes). This is how a class is added to mixin:
Here is an example of a fetching along with the definition of an array of classes.
This is necessary in order to create several types of fields and then boldly reuse them. For example, you have 3 types of fields on your site (for example, input (type = “text”)). You just create 3 such arrays and use them:
Next, let’s consider the following code snippet:
The separation is by field type, as well as by the value parameter. Pay attention to the expression ‘block’, it allows you to insert any elements after the field. Example link:
As a result, we get the following html:
The link was inserted after input. For input (type = “file”), the following structure is used:
Everything is simple, may vary depending on the layout. For select the following structure is used:
In other words, you need to pass an array of options for the select in the mixin fetch. Here is the example of the fetching:
The structure for checkboxes and radio buttons is
This structure is most easily styled. Here, the separation goes by the parameter ‘checked’. This is where the mixin is completed.
Fetch examples
input(type=”text”)
pug:
html:
input(type=”password”)
pug:
html:
input(type=”email”)
pug:
html:
textarea
pug:
html:
input(type=”checkbox”)
pug:
html:
input(type=”radio”)
pug:
html:
select
pug:
html:
All options
type - field type. Possible values are ‘text’, ’textarea’, ’email’, ’password’, ’checkbox’, ’radio’, ’select’.Mandatory field
title - field name. Usually is located in dt, except checkbox and radio. Mandatory field
id - field identifier.
class_array - an array of modifier classes.
add_class - additional class for the field.
value - the value attribute for the field.
placeholder - placeholder attribute for the field. For selects, it is added to the data-placeholder.
options - an array of options. Only for selects.
name - the name of the field. Only for checkbox and radio.
checked - the checked attribute. Only for checkbox and radio.