Skip to content

Instantly share code, notes, and snippets.

@jayan-blutui
Created May 5, 2021 03:06
Show Gist options
  • Save jayan-blutui/228a410ebc3d0779011f019d0620ef1e to your computer and use it in GitHub Desktop.
Save jayan-blutui/228a410ebc3d0779011f019d0620ef1e to your computer and use it in GitHub Desktop.
Blutui form macro
{% macro input(data) %}
<input type="{{ data.type }}" name="{{ data.name }}" value="{{ data.value }}" class="form-control"/>
{% endmacro %}
{% macro textarea(data) %}
<textarea name="{{ data.name }}" class="form-control">{{ data.value }}</textarea>
{% endmacro %}
{% macro checkbox(data) %}
{% for item in data.options %}
<label class="form-check">
<input type="checkbox" name="{{ data.name }}[]" value="{{ item.value }}" />
<span class="form-check-label">{{ item.value }}</span>
</label>
{% endfor %}
{% endmacro %}
{% macro radio(data) %}
{% for item in data.options %}
<label class="form-check">
<input type="radio" name="{{ data.name }}" value="{{ item.value }}" />
<span class="form-check-label">{{ item.label }}</span>
</label>
{% endfor %}
{% endmacro %}
{% macro select(data) %}
<select name="{{ data.name }}" value="{{ data.value }}" class="form-control">
{% for item in data.options %}
<option value="{{ item.value }}">{{ item.value }}</option>
{% endfor %}
</select>
{% endmacro %}
{% macro errors(message) %}
<small class="form-text text-danger">{{ message }}</small>
{% endmacro %}
{% macro field(data) %}
<div class="form-group">
<label for="{{ data.name }}">{{ data.label }}</label>
{% if data.type == 'checkbox' %}
{{ _self.checkbox(data) }}
{% elseif data.type == 'radio' %}
{{ _self.radio(data) }}
{% elseif data.type == 'select' %}
{{ _self.select(data) }}
{% elseif data.type == 'textarea' %}
{{ _self.textarea(data) }}
{% else %}
{{ _self.input(data) }}
{% endif %}
{% if data.helpText %}
<small class="form-text text-muted">{{ data.helpText }}</small>
{% endif %}
{{ _self.errors(data.errors) }}
</div>
{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment