Преглед изворни кода

[TwigBundle] converted form filters to functions

|render_enctype -> form_enctype()
|render         -> form_field()
|render_hidden  -> form_hidden()
|render_errors  -> form_errors()
|render_label   -> form_label()
|render_data    -> form_data()
Fabien Potencier пре 14 година
родитељ
комит
5c6b594dae

+ 21 - 14
src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php

@@ -64,18 +64,15 @@ class FormExtension extends \Twig_Extension
         );
     }
 
-    /**
-     * {@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'))),
         );
     }
 
@@ -84,7 +81,7 @@ class FormExtension extends \Twig_Extension
      *
      * 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
      */
@@ -98,17 +95,17 @@ class FormExtension extends \Twig_Extension
      *
      * 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);
@@ -193,6 +190,16 @@ class FormExtension extends \Twig_Extension
         ));
     }
 
+    /**
+     * 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;

+ 13 - 13
src/Symfony/Bundle/TwigBundle/Resources/views/form.twig

@@ -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 %}
 
@@ -34,7 +34,7 @@
 {% block hidden %}
 {% spaceless %}
     {% for child in field.allHiddenFields %}
-        {{ child|render }}
+        {{ form_field(child) }}
     {% endfor %}
 {% endspaceless %}
 {% endblock hidden %}
@@ -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 %}
@@ -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 %}
 
@@ -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 %}
@@ -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 %}
 
@@ -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 %}