Django templates share text on fullstop

Using django templates, I would like to break a block of text on the first full transition. Then I would like to give the first sentence the highlight class, and then the rest of the text is the normal class. how would i do that? for example (I know this doesn't work)

text = Aliquam pretium vestibulum nibh, vel molestie velit varius nec Curabitur non neque sed elit tincidunt. Dignissim eget vel du molestie magna auctor faucibus. Curabitur id nisl nec ipsum molestie egestas in at dolor. Morbi et risus ac quam sagittis accumsan. Morbi vitae elementum metus. Vestibulum malesuada ornare elit, et interdum nisi imperdiet sed.

<p class="highlight">{{text|split:"."[0]}}</p>
<p class="normal">{{text|split:"."[1]}}</p>

which would give

<p class="highlight">Aliquam pretium vestibulum nibh, vel molestie velit varius nec Curabitur non neque sed elit tincidunt.</p>

<p class="normal">Dignissim eget vel du molestie magna auctor faucibus. Curabitur id nisl nec ipsum molestie egestas in at dolor. Morbi et risus ac quam sagittis accumsan. Morbi vitae elementum metus. Vestibulum malesuada ornare elit, et interdum nisi imperdiet sed.</p>

I am using Django 1.1

+3
source share
1 answer

Define a custom filter , say mysplit.

@register.filter
def mysplit(value, sep = "."):
    parts = value.split(sep)
    return (parts[0], sep.join(parts[1:]))

Then use the filter in the template:

{% with text|mysplit:"." as parts %}
    <p class="highlight">{{ parts.0 }}</p>
    <p class="normal">{{ parts.1 }}</p>
{% endwith %}

This has been tested with Django 1.2.1.

+10
source

Source: https://habr.com/ru/post/1763817/


All Articles