Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find that the content is truncated?

I'm trying to build a blog app and the problem is when I use tag 'truncatewords_html' in my template to truncate posts longer than specified number of words, I need to link to complete post by some title like 'read more...' after truncation. So I should know that the post was truncated or not.

P.S.: Is this a pythonic way to solve the problem?

{% ifequal post.body|length post.body|truncatewords_html:max_words|length %}
  {{ post.body|safe }}
{% else %}
  {{ post.body|truncatewords_html:max_words|safe }}<a href="{{ post.url}}">read more</a>
{% endifequal %}
like image 931
sadegh Avatar asked Nov 26 '09 09:11

sadegh


1 Answers

This is pretty convoluted but django has some weird corners. Basically I figure if the string length is the same if you truncate at x and x+1 words then the string has not been truncated...

{% ifnotequal post.body|truncatewords_html:30|length post.body|truncatewords_html:31|length %}
   <a href="#">read more...</a>
{% endifnotequal %}
like image 93
mtvee Avatar answered Sep 25 '22 16:09

mtvee