Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
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 5a41e83677692bbe4d0c0cebd605ccf7db942cd4 1 parent 1193aaa
@timheap authored
View
5 phileo/templates/phileo/like.html
@@ -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 %}
View
52 phileo/templatetags/phileo_tags.py
@@ -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.auth.models import User
from django.contrib.contenttypes.models import ContentType
@@ -52,6 +53,52 @@ 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.inclusion_tag("phileo/js.html")
+def phileo_js():
+ return {"STATIC_URL": settings.STATIC_URL}
+
+
@register.filter
def likes_count(obj):
"""
@@ -69,11 +116,6 @@ def likes_count(obj):
).count()
-@register.inclusion_tag("phileo/js.html")
-def phileo_js():
- return {"STATIC_URL": settings.STATIC_URL}
-
-
@register.inclusion_tag("phileo/widget.html")
def likes_widget(user, obj, widget_id=None, like_type="like", toggle_class="phileo-liked"):
ct = ContentType.objects.get_for_model(obj)
Please sign in to comment.
Something went wrong with that request. Please try again.