Skip to content

Instantly share code, notes, and snippets.

@lantins
Created October 31, 2014 13:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lantins/af9752f536ce087c9ef2 to your computer and use it in GitHub Desktop.
Save lantins/af9752f536ce087c9ef2 to your computer and use it in GitHub Desktop.
Twig Macro Example --- Bootstrap
{% macro error_block(errors) %}
{% if errors %}
<span class="help-block">
<ul>
{% for err in errors %}
<li>{{ err }}</li>
{% endfor %}
</ul>
</span>
{% endif %}
{% endmacro %}
{% macro input_text(name, label, placeholder, value, errors) %}
{% import _self as form %}
<div class="form-group {% if errors %}has-error{% endif %}">
<label class="control-label col-sm-3" for="input_{{ name }}">{{ label }}</label>
<div class="col-sm-8">
<input class="form-control" type="text" id="input_{{ name }}" name="{{ name }}" placeholder="{{ placeholder }}" value="{{ value }}">
{{ form.error_block(errors) }}
</div>
</div>
{% endmacro %}
{% macro input_select(name, label, placeholder, option_array, value, errors, options = { multiple: false, include_blank: true }) %}
{% import _self as form %}
<div class="form-group {% if errors %}has-error{% endif %}">
<label class="control-label col-sm-3" for="input_{{ name }}">{{ label }}</label>
<div class="col-sm-8">
<select id="input_{{ name }}" name="{{ name }}" class="selectpicker form-control disable-text-shadow" {% if options.multiple %}multiple{% endif %}>
{% if options.include_blank %}
<option disabled selected></option>
{% endif %}
{% for title in option_array %}
<option {% if value == title %}selected{% endif %}>{{ title }}</option>
{% endfor %}
</select>
{{ form.error_block(errors) }}
</div>
</div>
{% endmacro %}
{% macro input_textarea(name, label, placeholder, value, errors) %}
{% import _self as form %}
<div class="form-group {% if errors %}has-error{% endif %}">
<label class="control-label col-sm-3" for="input_{{ name }}">{{ label }}</label>
<div class="col-sm-8">
<textarea rows="6" class="form-control" id="input_{{ name }}" name="{{ name }}" placeholder="{{ placeholder }}">{{ value }}</textarea>
{{ form.error_block(errors) }}
</div>
</div>
{% endmacro %}
@rutame
Copy link

rutame commented Jan 31, 2015

Hello! It looks very useful. How I pass the value for the option in input_select?

@theskyfloor
Copy link

Do you have any other Bootstrap twig macros completed? I've been looking for a library like this to use as a base for my Bootstrap projects.

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