[英]RedCloth and Ruby string interpolation possible?
I have a view which renders a block of text through RedCloth and I would want to add some variables to that text, f.ex. 我有一个通过RedCloth渲染文本块的视图,我想向该文本添加一些变量,例如f.ex。 through ruby string interpolation. 通过红宝石字符串插值。
Is this in any way possible? 这有可能吗?
I am assuming you use redcloth, so your users/editors will enter text, but they will be given a few keywords they can enter and which will be filled in at render time. 我假设您使用红布,因此您的用户/编辑者将输入文本,但是将为他们提供一些可以输入的关键字,这些关键字将在渲染时填写。 For example something like author, date, ...
but of course I have no idea what your website is for. 例如类似author, date, ...
但我当然不知道您的网站是做什么用的。
The cleanest delayed string interpolation is to use the %
operator. 最干净的延迟字符串插值法是使用%
运算符。
Some nonsense example for demonstration purposes only : 一些废话的例子,仅用于演示目的:
some_text = "flap %{cookie} some more %{action} NOW!"
filled_in_text = some_text % {cookie: 'banana', action: 'eating' }
Redcloth.new(filled_in_text).to_html
So you will retrieve some_text
from the database (model), what the user/editor typed, and then interpolate using the %
and a hash of possible (supported values). 因此,您将从数据库(模型)中检索some_text
,用户/编辑者键入的内容,然后使用%
和可能的哈希值(支持的值)进行插值。 The result you just hand to redcloth. 结果就是您只需要穿红衣服。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.