Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a {% render_like %} template tag

{% render_like like %} renders the given like and prints it in the
template. The template that it uses depends upon the liked instance, and
is either:

* phileo/<app>/<model>.html
* phileo/<app>/like.html
* phileo/_like.html

This allows for custom like templates on a per model and per application
basis.
  • Loading branch information...
commit 21a83aaefb97ce51774c3ed058f2e3cd4b46be42 1 parent 94d9ff2
Tim Heap timheap authored
5 phileo/templates/phileo/_like.html
View
@@ -0,0 +1,5 @@
+{% if instance.get_absolute_url %}
+ <a class="phileo phileo-{{ like.type }}" href="{{ instance.get_absolute_url }}">{{ instance }}</a>
+{% else %}
+ <span class="phileo phileo-{{ like.type }}" href="{{ instance.get_absolute_url }}">{{ instance }}</span>
+{% endif %}
43 phileo/templatetags/phileo_tags.py
View
@@ -2,6 +2,7 @@
from django.conf import settings
from django.core.urlresolvers import reverse
+from django.template.loader import render_to_string
from django.contrib.contenttypes.models import ContentType
from phileo.models import Like
@@ -42,6 +43,48 @@ def likes(parser, token):
return LikesNode(user, model_list, varname)
+class LikeRenderer(template.Node):
+
+ def __init__(self, varname):
+ self.varname = template.Variable(varname)
+
+ def render(self, context):
+ like = self.varname.resolve(context)
+
+ instance = like.receiver
+ content_type = like.receiver_content_type
+ app_name = content_type.app_label
+ model_name = content_type.model.lower()
+
+ like_context = {
+ 'instance': instance,
+ 'like': like,
+ }
+
+ return render_to_string([
+ 'phileo/%s/%s.html' % (app_name, model_name),
+ 'phileo/%s/like.html' % (app_name),
+ 'phileo/_like.html',
+ ], like_context, context)
+
+@register.tag
+def render_like(parser, token):
+ """
+ {% likes user as like_list %}
+ <ul>
+ {% for like in like_list %}
+ {% render_like like %}
+ {% endfor %}
+ </ul>
+ """
+
+ tokens = token.split_contents()
+ var = tokens[1]
+
+ return LikeRenderer(var)
+
+
+
@register.filter
def likes_count(obj):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.