Line Breaks in Django and Ruby on Rails
2009-11-10 11:10:19 | 0 Comment
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", "
") end After that this line <%= @post.entry %> becomes the following: <%= linebreak @post.entry %> And it is working! :)
") end After that this line <%= @post.entry %> becomes the following: <%= linebreak @post.entry %> And it is working! :)
Check my latest project compector.com where you can post references about your former employers and see what others have said about the future employers. Sign up now and post a reference and share it on twitter or facebook.


Comments
Leave a Response