Skip to content

Instantly share code, notes, and snippets.

@iamkirkbater
Created April 5, 2016 15:00
Show Gist options
  • Save iamkirkbater/970c354aa73302448f647676b83e52f7 to your computer and use it in GitHub Desktop.
Save iamkirkbater/970c354aa73302448f647676b83e52f7 to your computer and use it in GitHub Desktop.
Twig Macros for forms. Uses an array of attributes instead of 400 separate parameters for separate values. Also abstracts HTML5 text types for ease of reading in your template files.
{% macro input(name, value, type, attributes) %}
<input name="{{ name }}" type="{{ type }}" value="{{ value }}"{% for attr, value in attributes %} {{ attr }}="{{ value }}"{% endfor %}{% if not attributes.id is defined %} id="{{ name }}"{% endif %}/>
{% endmacro %}
{% macro text(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "text", attributes) }}
{% endmacro %}
{% macro password(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "password", attributes) }}
{% endmacro %}
{% macro email(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "email", attributes) }}
{% endmacro %}
{% macro color(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "color", attributes) }}
{% endmacro %}
{% macro button(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "button", attributes) }}
{% endmacro %}
{% macro date(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "date", attributes) }}
{% endmacro %}
{% macro file(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "file", attributes) }}
{% endmacro %}
{% macro hidden(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "hidden", attributes) }}
{% endmacro %}
{% macro month(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "month", attributes) }}
{% endmacro %}
{% macro number(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "number", attributes) }}
{% endmacro %}
{% macro range(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "range", attributes) }}
{% endmacro %}
{% macro search(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "search", attributes) }}
{% endmacro %}
{% macro tel(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "tel", attributes) }}
{% endmacro %}
{% macro time(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "time", attributes) }}
{% endmacro %}
{% macro url(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "url", attributes) }}
{% endmacro %}
{% macro week(name, value, attributes) %}
{% from _self import input %}
{{ input(name, value, "week", attributes) }}
{% endmacro %}
{% macro textarea(name, value, attributes) %}
<textarea name="{{ name }}"{% for attr, value in attributes %} {{ attr }}="{{ value }}"{% endfor %}{% if not attributes.id is defined %} id="{{ name }}"{% endif %}>{{ value|e }}</textarea>
{% endmacro %}
{% macro label(id, content, attributes) %}
<label for="{{ id }}"{% for attr, value in attributes %} {{ attr }}="{{ value }}"{% endfor %}>{{ content|e }}</label>
{% endmacro %}
{% macro checkbox(name, value, id, attributes) %}
<input type="checkbox" name="{{ name }}" id="{{ id|default(name) }}" class="{{ class }}" value="1"{% if value %} checked="checked"{% endif %}{% for attr, value in attributes %} {{ attr }}="{{ value }}"{% endfor %} />
{% endmacro %}
{% macro select(name, values, value, attributes) %}
<select name="{{ name }}"{% for attr, value in attributes %} {{ attr }}="{{ value }}"{% endfor %}{% if not attributes.id is defined %} id="{{ name }}"{% endif %}>
{% for key, name in values %}
<option value="{{ key }}"{% if value == key %} selected="selected"{% endif %}>{{ name }}</option>
{% endfor %}
</select>
{% endmacro %}
{% macro radio(name, values, value, attributes) %}
{% for key, label in values %}
<label{% if value == key %} class="selected"{% endif %} ><input type="radio" name="{{ name }}" value="{{ key }}"{% if value == key %} checked="checked"{% endif %}{% for attr, value in attributes %} {{ attr }}="{{ value }}"{% endfor %}{% if not attributes.id is defined %} id="{{ name }}"{% endif %}> {{ label }}</label>
{% endfor %}
{% endmacro %}
@iamkirkbater
Copy link
Author

Usage:

{% import "macros.forms.twig" as form %}

<div class="form-item">
  {{ form.label('myForm[firstName]', "First Name", {"class": "myLabelClass"}) }}
  {{ form.text('myForm[firstName]', data.firstName, {"required": true, "class": "some classes here"}) }}
</div>

Basically, I wanted to be able to add the HTML 5 required attribute to a text area, without having to add another parameter to the exceedingly long amount of parameters already there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment