Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 550837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:19:50+00:00 2026-05-13T11:19:50+00:00

Using the standard Django templating system, is there any snippet/reusable template tag to have

  • 0

Using the standard Django templating system, is there any snippet/reusable template tag to have the first n words in a piece of text wrapped in a tag so I can style them?

What I’m ideally looking for:

{{item.description|wrap:"3 span big"}}

which outputs:

<span class="big">Lorem ipsum dolor</span> sit amet, consectetur adipiscing elit.

If this would be, for any reason, not feasible or really difficult to obtain, I can use JavaScript and do it client side, but I’d like more to be able to do it on page output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:19:50+00:00Added an answer on May 13, 2026 at 11:19 am

    It turns out that writing a filter is really easy (and works exactly the intended way).
    This could be made a lot safer, but does the job (and won’t break unless someone passes html as parameters):

    from django import template
    from django.template.defaultfilters import stringfilter
    from django.utils.safestring import mark_safe
    
    register = template.Library()
    
    @register.filter(name='wrap')
    @stringfilter
    def wrap(value, arg):
        params = arg.split()
        n = int(params[0])
        tag = params[1]
        tagclass = params[2]
        words = value.split()
        head = ' '.join( words[:n] )
        tail = ' '.join( words[n:] )
        return mark_safe('<%s class="%s">%s</%s> %s' % (tag, tagclass, head, tag, tail))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On a system I have developed It requires me to login using a standard
I have a Django application that logs users in and out using the standard
I am using the standard User model (django.contrib.auth) which comes with Django. I have
I have a standard django setup using postgres, but I also want to access
I have a flash message in Django that is built using a standard char
Using standard mysql functions is there a way to write a query that will
I am using standard windows forms controls under C#. I have two controls which
Currently I have a custom tool which generates vanilla-SQL (only using standard SQL), from
I have an application running under WebLogic that is using standard forms authentication. The
I have a form with most of its functionality implemented using standard TAction. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.