码迷,mamicode.com
首页 > Web开发 > 详细

rails 字符串 转化为 html

时间:2016-11-19 13:29:08      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:asi   分段   san   mat   cal   log   blink   format   graph   

simple_format              http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/simple_format
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html
my_text = "Here is some basic text...\n...with a line break."

simple_format(my_text)
# => "<p>Here is some basic text...\n<br />...with a line break.</p>"

simple_format(my_text, {}, wrapper_tag: "div")
# => "<div>Here is some basic text...\n<br />...with a line break.</div>"

more_text = "We want to put a paragraph...\n\n...right there."

simple_format(more_text)
# => "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>"

simple_format("Look ma! A class!", class: description)
# => "<p class=‘description‘>Look ma! A class!</p>"

simple_format("<blink>Unblinkable.</blink>")
# => "<p>Unblinkable.</p>"

simple_format("<blink>Blinkable!</blink> It‘s true.", {}, sanitize: false)
# => "<p><blink>Blinkable!</span> It‘s true.</p>"

一个\n被认为是换行其之后是<br/>  两个\n被认为是分段其之前是</p>之后<p>

If you want to escape all content, you should invoke the h method before calling the text helper.
simple_format h(<a href="http://example.com/">Example</a>)
# => "<p>&lt;a href=\"http://example.com/\"&gt;Example&lt;/a&gt;</p>"

 

rails 字符串 转化为 html

标签:asi   分段   san   mat   cal   log   blink   format   graph   

原文地址:http://www.cnblogs.com/znsongshu/p/6080217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!