Commits

Daniele Procida  committed 09afcc5

#7 Provide template in a file as well as an attribute on FilterSet

  • Participants
  • Parent commits f867586

Comments (0)

Files changed (2)

File django_easyfilters/filterset.py

 from django import template
+from django.template.loader import get_template
+
 from django.utils.safestring import mark_safe
 from django.utils.html import escape
 from django.utils.text import capfirst
 @python_2_unicode_compatible
 class FilterSet(object):
 
-    template = u"""
-<div class="filterline"><span class="filterlabel">{{ filterlabel }}:</span>
-{% for choice in choices %}
-  {% if choice.link_type == 'add' %}
-    <span class="addfilter"><a href="{{ choice.url }}" title="Add filter">{{ choice.label }}&nbsp;({{ choice.count }})</a></span>&nbsp;&nbsp;
-  {% else %}
-    {% if choice.link_type == 'remove' %}
-    <span class="removefilter"><a href="{{ choice.url }}" title="Remove filter">{{ choice.label }}&nbsp;&laquo;&nbsp;</a></span>
-    {% else %}
-      <span class="displayfilter">{{ choice.label }}</span>
-    {% endif %}
-  {% endif %}
-{% endfor %}
-</div>
-"""
+    # If the attribute "template" is provided (as a string), that will be
+    # preferred;  otherwise we use the specified template_file 
+    template = None
+    template_file = "django-easyfilters/default.html"
+    
     title_fields = None
 
     def __init__(self, queryset, params):
         return self.get_template(filter_.field).render(template.Context(ctx))
 
     def get_template(self, field_name):
-        return template.Template(self.template)
+        if self.template:
+            return template.Template(self.template)
+        else:
+            return get_template(self.template_file)
 
     def render(self):
         return mark_safe(u'\n'.join(self.render_filter(f) for f in self.filters))

File docs/filterset.rst

         * ``count``: for those that are ``add`` links, the number of items in
           the QuerySet that match that choice.
 
+   .. attribute:: template_file
+
+      The path to a file containing a Django template, used to render all the
+      filters. It is used by the default ``get_template`` method, see above. By
+      default, "django-easyfilters/default.html" is used. If the ``template``
+      attribute is provided, ``template_file`` will be ignored.
+
    .. attribute:: template
 
       A string containing a Django template, used to render all the filters.  It