Line Breaks in Django and Ruby on Rails
Let's assume you are developing a blog application with Django or Ruby on Rails. You have created your Post model and everything is working fine, except the paragraphs you have written during creating your new post. How do you solve this problem? It is very simple to do so in both Ruby on Rails and Django.
Solving this problem in Django is simpler than Ruby on Rails because there is a built-in template tag in Django named "linebreaks". If you are showing your entry like {{ post.entry }} all you need to do is adding linebreaks tag like this:
{{ post.entry|linebreaks }}
And for Rails you should write a helper for this. Just create a modelname_helper.rb in /railsapp/app/helpers folder and add the following lines:
def linebreak(str)
str.gsub("\n", "<br />")
end
After that this line <%= @post.entry %> becomes the following:
<%= linebreak @post.entry %>
And it is working! :)

Comments
Leave a Response