1. Mikhail Korobov
  2. django-widget-tweaks

Commits

Mikhail Korobov  committed 1339c7b Merge

Merge pull request #4 from pahaz/master

New tags + support for template variables is added.

  • Participants
  • Parent commits eae7a4d, 5fde89a
  • Branches default

Comments (0)

Files changed (7)

File .hgignore

View file
 #my files
 db.sqlite
 *.log
-
+.idea

File AUTHORS.txt

View file
 * Anonymous
 * Skylar Saveland <skylar.saveland@gmail.com>
 * Trey Hunner (http://treyhunner.com)
+* Pavel Blinov <pahaz.blinov@gmail.com>

File CHANGES.rst

View file
 Changes
 =======
 
+1.1.2 (2012-06-03)
+------------------
+
+* add support template varible in ``render_field`` tag.
+* add ``field_type`` and ``widget_type`` filters.
+
 1.1.1 (2012-03-22)
 ------------------
 

File README.rst

View file
 
     <!-- add/change several attributes -->
     {% render_field form.text rows="20" cols="20" title="Hello, world!" %}
+    
+    <!-- add/change several attributes with use template variable -->
+    {% render_field form.text placeholder=form.text.label %}
 
     <!-- append to an attribute -->
     {% render_field form.title class+="css_class_1 css_class_2" %}
 
+field_type widget_type
+----------------------
 
+Tags for get field class name and field widget class name in lower case.
+
+Example::
+
+    <div class="field {{ field|field_type }} {{ field|widget_type }}
+                {{ field.html_name }}">
+        {{ field }}
+    </div>
+    
+    Out:
+    
+    <div class="field charfield textinput 
+                name">
+        <input id="id_name" type="text" name="name" maxlength="100" />
+    </div>
+
+ 
 Contributing
 ============
 

File setup.py

View file
     if cmd in sys.argv:
         from setuptools import setup
 
-version='1.1.1'
+version='1.1.2'
 
 setup(
     name = 'django-widget-tweaks',

File widget_tweaks/templatetags/widget_tweaks.py

View file
 def set_data(field, data):
     return set_attr(field, 'data-' + data)
 
+# render_field tag
 
 ATTRIBUTE_RE = re.compile(r"""
     (?P<attr>
     (?P<sign>
         \+?=
     )
+    (?P<value>
     ['"]? # start quote
-    (?P<value>
         [^"']*
+    ['"]? # end quote
     )
-    ['"]? # end quote
 """, re.VERBOSE | re.UNICODE)
 
 @register.tag
         if not match:
             raise TemplateSyntaxError(error_msg + ": %s" % pair)
         dct = match.groupdict()
-        attr, sign, value = dct['attr'], dct['sign'], dct['value']
+        attr, sign, value = dct['attr'], dct['sign'], parser.compile_filter(dct['value'])
         if sign == "=":
             set_attrs.append((attr, value))
         else:
     def render(self, context):
         bounded_field = self.field.resolve(context)
         for k, v in self.set_attrs:
-            bounded_field = set_attr(bounded_field, '%s:%s' % (k,v))
+            bounded_field = set_attr(bounded_field, '%s:%s' % (k,v.resolve(context)))
         for k, v in self.append_attrs:
-            bounded_field = append_attr(bounded_field, '%s:%s' % (k,v))
+            bounded_field = append_attr(bounded_field, '%s:%s' % (k,v.resolve(context)))
         return bounded_field
+
+"""
+    Use:
+        <div class="field {{ field|field_type }} {{ field|widget_type }} {{ field.html_name }}">
+            {{ field }}
+        </div>
+    Out:
+        <div class="field charfield textinput name">
+            <input id="id_name" type="text" name="name" maxlength="100" />
+        </div>
+"""
+
+@register.filter(name='field_type')
+def field_type(field):
+    if hasattr(field, 'field') and field.field:
+        return field.field.__class__.__name__.lower()
+    return ''
+
+@register.filter(name='widget_type')
+def widget_type(field):
+    if hasattr(field, 'field') and hasattr(field.field, 'widget') and field.field.widget:
+        return field.field.widget.__class__.__name__.lower()
+    return ''

File widget_tweaks/tests.py

View file
         res = render_form('{% render_field form.with_cls %}{% render_field form.with_cls class+="bar" %}{% render_field form.with_cls %}')
         self.assertEqual(res.count("class0"), 3)
         self.assertEqual(res.count("bar"), 1)
+        
+class RenderFieldTagUseTemplateVariableTest(TestCase):
+    def test_use_template_variable_in_parametrs(self):
+        res = render_form('{% render_field form.with_attrs egg+="pahaz" placeholder=form.with_attrs.label %}')
+        assertIn('egg="spam pahaz"', res)
+        assertIn('placeholder="With attrs"', res)
+        
+class RenderFieldFilter_field_type_widget_type_Test(TestCase):
+    def test_field_type_widget_type_rendering_simple(self):
+        res = render_form('<div class="{{ form.simple|field_type }} {{ form.simple|widget_type }} {{ form.simple.html_name }}">{{ form.simple }}</div>')
+        assertIn('class="charfield textinput simple"', res)