澄清:帖子的创建者应该能够决定截断何时发生.
我在我的博客中使用以下辅助函数实现了类似[— MORE —]功能的wordpress:
# application_helper.rb def more_split(content) split = content.split("[---MORE---]") split.first end def remove_more_tag(content) content.sub(“[---MORE---]",'') end
在索引视图中,帖子正文将显示(但没有)[— MORE —]标记的所有内容.
# index.html.erb <%= raw more_split(post.rendered_body) %>
在节目视图中,除了[— MORE —]标签外,还会显示帖子正文中的所有内容.
# show.html.erb <%=raw remove_more_tag(@post.rendered_body) %>
这个解决方案目前对我没有任何问题.
由于我还是编程的初学者,我不断想知道是否有更优雅的方法来实现这一目标.
你会怎么做?
谢谢你的时间.
这是更新版本:
# index.html.erb <%=raw truncate(post.rendered_body,:length => 0,:separator => '[---MORE---]',:omission => link_to( "Continued...",post)) %>
……并在展示视图中:
# show.html.erb <%=raw (@post.rendered_body).gsub("[---MORE---]",'') %>
解决方法
我只使用截断,它具有您需要的所有选项.
truncate("And they found that many people were sleeping better.",:length => 25,:omission => '... (continued)') # => "And they f... (continued)"
更新
在看完评论并挖掘了一些文档后,似乎:separator完成了工作.
来自doc:
Pass a :separator to truncate text at a natural break.
对于referenece,请参阅docs
truncate(post.rendered_body,:separator => '[---MORE---]')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。