Skip to content

Instantly share code, notes, and snippets.

@johnkary
Created January 5, 2012 16:37
Show Gist options
  • Save johnkary/1566022 to your computer and use it in GitHub Desktop.
Save johnkary/1566022 to your computer and use it in GitHub Desktop.
{# ./src/Acme/DemoBundle/Resources/view/Survey/_question.html.twig #}
{#
Template for rendering a radio button
#}
<div class="question">
{{ form_errors(widget) }}
{{ form_label(widget, label, {'attr': {'class':'question_text'} }) }}
{{ form_widget(widget) }}
</div>
{# ./src/Acme/DemoBundle/Resources/view/Survey/_question_radio.html.twig #}
{#
Used as a "form_theme"
This customizes the choice_widget when expanded by wrapping it in a span tag,
and rendering as "label" "field" instead of "field" "label"
#}
{% block choice_widget %}
{% spaceless %}
{% if expanded %}
<div {{ block('widget_container_attributes') }}>
{% for child in form %}
<span class="radio_choice">
{{ form_label(child) }}
{{ form_widget(child) }}
</span>
{% endfor %}
</div>
{% else %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{% if empty_value is not none %}
<option value="">{{ empty_value|trans }}</option>
{% endif %}
{% if preferred_choices|length > 0 %}
{% set options = preferred_choices %}
{{ block('widget_choice_options') }}
{% if choices|length > 0 %}
<option disabled="disabled">{{ separator }}</option>
{% endif %}
{% endif %}
{% set options = choices %}
{{ block('widget_choice_options') }}
</select>
{% endif %}
{% endspaceless %}
{% endblock choice_widget %}
{# ./src/Acme/DemoBundle/Resources/view/Survey/_render_macros.html.twig #}
{#
Renders a radio button input with its question label text. Each widget is
rendered with a special form_template explicitly defined by the file included
with the form_theme code block.
@param object widget Usually the form.widget_name object for the widget
you want to render.
@param string label The question text to use as the widget's label.
#}
{% macro question_radio(widget, label) %}
{% form_theme widget 'AcmeDemoBundle:Survey:_question_radio.html.twig' %}
{% include 'AcmeDemoBundle:Survey:_question.html.twig' %}
{% endmacro %}
{# ./src/Acme/DemoBundle/Resources/view/Survey/page_1.html.twig #}
{% extends "AcmeDemoBundle:Survey:survey.html.twig" %}
{% import "AcmeDemoBundle:Survey:_render_macros.html.twig" as render %}
{% block example_form %}
<form action="{{ post_url }}" id="example_form" method="post" {{ form_enctype(form) }}>
{{ form_errors(form) }}
<p>Please answer the following questions.</p>
{{ render.question_radio(form.question_1, 'Have you taken the BLOCWORK exam?') }}
{{ render.question_radio(form.question_2, 'Have you received your degree yet?') }}
<button type="submit">Submit Answers</button>
{{ form_rest(form) }}
</form>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment