[Answer]-Templatetag to post a list of cmsplugin-blog entries on home page

1๐Ÿ‘

I achieved this by injecting a list into the context with templatetag and included other html for rendering the list.

the tag:

 # blog post widget
 20 @register.tag
 21 def get_latest_entries(parser, token):
 22   bits = token.contents.split()
 23   if len(bits) != 4:
 24     raise TemplateSyntaxError, "(cmsplugin_blog) get_latest_entries tag takes exactly 3 arguments)"
 25   if bits[2] != 'as':
 26     raise TemplateSyntaxError, "second argument to the get_latest_links tag must be 'as'"
 27   return LatestEntriesNode(bits[1], bits[3])
 28 
 29 class LatestEntriesNode(Node):
 30   def __init__(self, num, varname):
 31     self.num, self.varname = num, varname
 32 
 33   def render(self, context):
 34     request = context["request"]
 35     language = get_language_from_request(request)
 36     kw = get_translation_filter_language(Entry, language)
 37     context[self.varname] = Entry.published.filter(**kw)[:self.num]
 38     return ''

Placing the tag where the list should appear:

  14 {% block entries_widget %}
  15   {% get_latest_entries 3 as blog_entries %}
  16   {% if blog_entries %}
  17     {% include "cmsplugin_blog/widget_latest_entry_include.html" %}
  18   {% else %}
  19     <p>{% trans "No entries" %}</p>
  20   {% endif %}
  21 {% endblock %}
๐Ÿ‘คThuy Dang

Leave a comment