0

I need to add a <br/> after each character in a string which I'm passing into the template. While one way of doing it would involve doing it before it is passed into the template (i.e. in the Python code), I'm wondering if it is possible to do it in the actual template.

Or would I be better off creating a helper function and invoking it from the template, and if so, how would I import that function/module into the template.

The one thing I want to avoid is mixing HTML bits with the view code so adding the <br/>s in the view should only be the last resort.

1
  • 1
    If you were to find the need for some more complex template functionality, which is not available in Django by default, then you can create custom template tags. Commented Aug 21, 2011 at 12:56

2 Answers 2

5

Strings in Python are sequences, therefore the individual characters in them can be joined.

Sign up to request clarification or add additional context in comments.

Comments

0

How about you make such a function in the javascript, and after passing the string to the template, instead of using it directly, you can call that function on the string, and the function returns the string with every character is followed by a <br/>?

var nStr = "";
for (i=0;i<str.length;i++)
{
    nStr = nStr.concat(str.charAt(i),"<br/>");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.