Mo J. Mughrabi
Mo J. Mughrabi

Reputation: 6997

django templatetag

am looking for a django templatetag that will count words and substring a whole paragraph without chopping off words. Is there a built in function? I tried looking into the built-in function list at Django template documentation but couldn't find anything.

Please advice?

Upvotes: 1

Views: 768

Answers (3)

Peter
Peter

Reputation: 6669

I think you need a filter. So far, check the django template documentation for slice filter https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#slice . Also check out the trucatechars and the trucatewords filters there too https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#truncatechars, https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#truncatewords. Another one is the https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#truncatewords-html which is ok in html. All these filters are unfortuantely in the development version of django

Upvotes: 0

errx
errx

Reputation: 1791

Here is my implementation of this. It actually chops a sentence not a paragraph but anyway you should get an idea.

{% splitarticle some_data word_count %}
    {{ pre_part }}
    {% if post_part %}
       {{ post_part }}
     {% endif %}

And it will return two variables

And the code. You should put in < your_app >/templatetags/

from django import template
from django.utils.encoding import force_unicode

def split_by_sentence(text, word_count):
    words = force_unicode(text).strip().split(' ')
    word_count = int(word_count)
    if len(words)>word_count:
        cnt = word_count
        for word in words[word_count:]:
            cnt+=1
            if '.' in word or '?' in word or '!' in word:
                break
        if cnt>=len(words):
            cnt = word_count

        pre = ' '.join(words[:cnt])
        post = ' '.join(words[cnt:])
        return pre, post    
    else:
        return text, None

register = template.Library()
@register.tag
def splitarticle(parser, token):
    try:
        tag, data, word_count = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError('splitarticle parsing error')
    return SplitArticleNode(data, word_count)

class SplitArticleNode(template.Node):
    def __init__(self, data, word_count):
        self.data = template.Variable(data)
        self.word_count = word_count
    def render(self, context):
        data = self.data.resolve(context)
        context['pre_part'], context['post_part'] = split_by_sentence(data, self.word_count)
        return ''

Upvotes: 1

Dominic Santos
Dominic Santos

Reputation: 1960

As far as I am aware there is not built-in tag to do this. Depending on the nature of the words you want (they aren't inside, or part of a for-loop - although even then you could just do this recursively) you could just do what you want in the views.py and pass the output as a variable to the template?

So you'd do your substringing and word-count in the views and pass the answer to the template as a variable/list?

Upvotes: 1

Related Questions