Commits

Dan Carroll committed cfc23d6

Move activity rendering to a template tag to reduce duplication

Comments (0)

Files changed (5)

mysite/activitysync/templates/activitysync/activities_tag.html

+{% if not use_date_headers %}<div id="activity-list"><ul>{% endif %}
+{% for activity in activities %}
+{% if use_date_headers %}    
+    {% ifchanged activity.pub_date.date %}
+        {% if not forloop.first %}</ul></div>{% endif %}
+        <h2 class="section-title">{{ activity.pub_date|date:"F j" }}</h2>
+        <div id="activity-list"><ul>
+    {% endifchanged %}
+{% endif %}
+    <li><a href="{{ activity.provider.link }}"><img src="{{ MEDIA_URL }}networks/{{ activity.provider.sourceid }}.png" alt="{{ activity.provider.name }}" /></a>{{ activity.provider.prefix }} <a href="{{ activity.link }}">{{ activity.title }}</a> <span class="time">{{ activity.pub_date|timesince|upper }} AGO</span></li>
+    
+{% endfor %}
+</ul></div>

mysite/activitysync/templatetags/__init__.py

Empty file added.

mysite/activitysync/templatetags/activitysync_extras.py

+from django import template
+from django.conf import settings
+from activitysync.models import Activity
+
+register = template.Library()
+
+@register.inclusion_tag('activitysync/activities_tag.html')
+def render_activities(activities):
+    return {
+        'activities': activities,
+        'use_date_headers': False,
+        'MEDIA_URL': settings.MEDIA_URL,
+    }
+
+@register.inclusion_tag('activitysync/activities_tag.html')
+def render_activities_with_date_headers(activities):
+    return {
+        'activities': activities,
+        'use_date_headers': True,
+        'MEDIA_URL': settings.MEDIA_URL,
+    }
+

mysite/templates/activity.html

 {% block title %} | Activity{% if page_title %} | {{ page_title }}{% endif %}{% endblock %}
 
 {% block content %}
-    {% for activity in object_list %}
-        {% ifchanged activity.pub_date.date %}
-            {% if not forloop.first %}</ul></div>{% endif %}
-            
-            <h2 class="section-title">{{ activity.pub_date|date:"F j" }}</h2>
-            <div id="activity-list"><ul>
-        {% endifchanged %}
-        
-        <li><a href="{{ activity.provider.link }}"><img src="{{ MEDIA_URL }}networks/{{ activity.provider.sourceid }}.png" alt="{{ activity.provider.name }}" /></a>{{ activity.provider.prefix }} <a href="{{ activity.link }}">{{ activity.title }}</a>
-        <span class="time">{{ activity.pub_date|timesince|upper }} AGO</span></li>
-    
-    {% endfor %}
-        </ul></div>
+    {% load activitysync_extras %}
+    {% render_activities_with_date_headers object_list %}
     <div class="indicator">
         {% if has_previous %}<a href="{{ previous }}">&#171; Newer</a>{% endif %}
         {% if has_previous and has_next %}|{% endif %}

mysite/templates/index.html

         </div>
         
         <h2 class="section-title"><a href="{% url main_activity %}">activity</a></h2>
-        <div id="activity-list">
-            <ul>
-            {% for activity in activities %}
-                <li><a href="{{ activity.provider.link }}"><img src="{{ MEDIA_URL }}networks/{{ activity.provider.sourceid }}.png" alt="{{ activity.provider.name }}" /></a>{{ activity.provider.prefix }} <a href="{{ activity.link }}">{{ activity.title }}</a>
-                    <span class="time">{{ activity.pub_date|timesince|upper }} AGO</span></li>
-            {% endfor %}
-            </ul>
+            {% load activitysync_extras %}
+            {% render_activities activities %}
             <div class="indicator"><a href="{% url main_activity %}">More activity &#187;</a></div>
-        </div>
 {% endblock %}