Skip to content

Commit

Permalink
Add a {% render_like %} template tag
Browse files Browse the repository at this point in the history
{% 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
askeyt committed Nov 28, 2011
1 parent 1193aaa commit 5a41e83
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
5 changes: 5 additions & 0 deletions 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 %}
52 changes: 47 additions & 5 deletions phileo/templatetags/phileo_tags.py
Expand Up @@ -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

Expand Down Expand Up @@ -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):
"""
Expand All @@ -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)
Expand Down

0 comments on commit 5a41e83

Please sign in to comment.