Skip to content

Commit

Permalink
[TwigBundle] converted form filters to functions
Browse files Browse the repository at this point in the history
|render_enctype -> form_enctype()
|render         -> form_field()
|render_hidden  -> form_hidden()
|render_errors  -> form_errors()
|render_label   -> form_label()
|render_data    -> form_data()
  • Loading branch information
fabpot committed Jan 3, 2011
1 parent e20a246 commit 5c6b594
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
35 changes: 21 additions & 14 deletions src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php
Expand Up @@ -64,18 +64,15 @@ public function getTokenParsers()
);
}

/**
* {@inheritdoc}
*/
public function getFilters()
public function getFunctions()
{
return array(
'render_enctype' => new \Twig_Filter_Method($this, 'renderEnctype', array('is_safe' => array('html'))),
'render' => new \Twig_Filter_Method($this, 'render', array('is_safe' => array('html'))),
'render_hidden' => new \Twig_Filter_Method($this, 'renderHidden', array('is_safe' => array('html'))),
'render_errors' => new \Twig_Filter_Method($this, 'renderErrors', array('is_safe' => array('html'))),
'render_label' => new \Twig_Filter_Method($this, 'renderLabel', array('is_safe' => array('html'))),
'render_data' => new \Twig_Filter_Method($this, 'renderData', array('is_safe' => array('html'))),
'form_enctype' => new \Twig_Function_Method($this, 'renderEnctype', array('is_safe' => array('html'))),
'form_field' => new \Twig_Function_Method($this, 'renderField', array('is_safe' => array('html'))),
'form_hidden' => new \Twig_Function_Method($this, 'renderHidden', array('is_safe' => array('html'))),
'form_errors' => new \Twig_Function_Method($this, 'renderErrors', array('is_safe' => array('html'))),
'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))),
'form_data' => new \Twig_Function_Method($this, 'renderData', array('is_safe' => array('html'))),
);
}

Expand All @@ -84,7 +81,7 @@ public function getFilters()
*
* Example usage in Twig templates:
*
* <form action="..." method="post" {{ form|render_enctype }}>
* <form action="..." method="post" {{ render_enctype(form) }}>
*
* @param Form $form The form for which to render the encoding type
*/
Expand All @@ -98,17 +95,17 @@ public function renderEnctype(Form $form)
*
* Example usage in Twig:
*
* {{ field|render }}
* {{ form_field(field) }}
*
* You can pass additional variables during the call:
*
* {{ field|render(['param': 'value']) }}
* {{ form_field(field, {'param': 'value'}) }}
*
* @param FieldInterface $field The field to render
* @param array $params Additional variables passed to the template
* @param string $resources
*/
public function render(FieldInterface $field, array $attributes = array(), array $parameters = array(), $resources = null)
public function renderField(FieldInterface $field, array $attributes = array(), array $parameters = array(), $resources = null)
{
if (null === $this->templates) {
$this->templates = $this->resolveResources($this->resources);
Expand Down Expand Up @@ -193,6 +190,16 @@ public function renderLabel(FieldInterface $field, $label = null, array $paramet
));
}

/**
* Renders the widget data of the given field
*
* @param FieldInterface $field The field to render the data for
*/
public function renderData(FieldInterface $field)
{
return $field->getData();
}

protected function getWidget(FieldInterface $field, array $resources = array())
{
$cacheable = true;
Expand Down
26 changes: 13 additions & 13 deletions src/Symfony/Bundle/TwigBundle/Resources/views/form.twig
Expand Up @@ -2,20 +2,20 @@
{% spaceless %}
<div>
{# TODO: would be nice to rename this variable to "field" #}
{{ child|render_label }}
{{ child|render_errors }}
{{ child|render }}
{{ form_label(child) }}
{{ form_errors(child) }}
{{ form_field(child) }}
</div>
{% endspaceless %}
{% endblock field_row %}

{% block field_group %}
{% spaceless %}
{{ field|render_errors }}
{{ form_errors(field) }}
{% for child in field.visibleFields %}
{{ block('field_row') }}
{% endfor %}
{{ field|render_hidden }}
{{ form_hidden(field) }}
{% endspaceless %}
{% endblock field_group %}

Expand All @@ -34,7 +34,7 @@
{% block hidden %}
{% spaceless %}
{% for child in field.allHiddenFields %}
{{ child|render }}
{{ form_field(child) }}
{% endfor %}
{% endspaceless %}
{% endblock hidden %}
Expand Down Expand Up @@ -106,7 +106,7 @@
{% spaceless %}
{% if field.isExpanded %}
{% for choice, child in field %}
{{ child|render }}
{{ form_field(child) }}
<label for="{{ child.id }}">{{ field.label(choice) }}</label>
{% endfor %}
{% else %}
Expand Down Expand Up @@ -137,8 +137,8 @@

{% block date_time_field %}
{% spaceless %}
{{ field.date|render }}
{{ field.time|render }}
{{ form_field(field.date) }}
{{ form_field(field.time) }}
{% endspaceless %}
{% endblock date_time_field %}

Expand All @@ -147,7 +147,7 @@
{% if field.isfield %}
{{ block('text_field') }}
{% else %}
{{ field.pattern|replace({ '{{ year }}': field.year|render, '{{ month }}': field.month|render, '{{ day }}': field.day|render })|raw }}
{{ field.pattern|replace({ '{{ year }}': form_field(field.year), '{{ month }}': form_field(field.month), '{{ day }}': form_field(field.day) })|raw }}
{% endif %}
{% endspaceless %}
{% endblock date_field %}
Expand All @@ -156,7 +156,7 @@
{% spaceless %}
{# TODO the next line should be set attr.size = 1, but that's not supported yet by Twig #}
{% if field.isfield %}{% set attr = { 'size': 1 } %}{% endif %}
{{ field.hour|render(attr) }}:{{ field.minute|render(attr) }}{% if field.isWithSeconds %}:{{ field.second|render(attr) }}{% endif %}
{{ form_field(field.hour, attr) }}:{{ form_field(field.minute, attr) }}{% if field.isWithSeconds %}:{{ form_field(field.second, attr) }}{% endif %}
{% endspaceless %}
{% endblock time_field %}

Expand Down Expand Up @@ -184,8 +184,8 @@
{% set group = field %}
{% set field = group.file %}
<input type="file" {{ block('field_attributes') }} />
{{ group.token|render }}
{{ group.original_name|render }}
{{ form_field(group.token) }}
{{ form_field(group.original_name) }}
{% endspaceless %}
{% endblock file_field %}

0 comments on commit 5c6b594

Please sign in to comment.