views:

40

answers:

3

Hello, I'm exploring Django and got this particular problem.

How do I prepend <span class="label">Note:</span> inside {{article.content_html|safe}}?

The content of {{article.content_html|safe}} are paragraph blocks, and I just wanna add <span class="label">Note:</span> in the very first paragraph.

Thanks!

A: 

There's no easy way. You can easily prepend to all articles.

<span class="label">Note:</span>
{{article.content_html|safe}}

If that doesn't help you consider changing the structure of article.content_html so you can manipulate with blocks from django templates, so it should look something like this

{{article.content_header}}
<span class="label">Note:</span>
{{article.content_html}}

If that solution is not feasible to you and you absolutely need to parse and modify the content of article.content_html, write your own custom filter that does that. You can find documentation about writing custom filters here http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters.

Ivan
Thanks Ivan! ;)
voidnothings
+1  A: 

Sounds like you want to write a custom tag that uses BeautifulSoup to parse the HTML and inject the fragment.

Ignacio Vazquez-Abrams
thanks :) will explore it.
voidnothings
A: 

An alternate approach could be to do this with javascript. In jQuery, it would look something like:

var first_p_text = $("p:first").text()
$("p:first").html("<span class="label">Note:</span>" + first_p_text)

Note though that if there are other elements inside your first p, $("p:first").text() will grab the text from those as well - see http://api.jquery.com/text/

Of course, this relies on decent javascript support in the client.

Chris Lawlor