允许用户自定义稍后用嵌入表达式解释的字符串
最初,我们在 Rails 上构建了一项服务,该服务使用带有嵌入表达式 (#{}) 的静态字符串来表示对象的日期时间等值。这些字符串用于与其他用户建立通信。
我们希望为用户提供一种使用嵌入表达式自定义默认文本的方法。
它们目前分散在我们的模型、控制器和视图中,但我们希望将它们全部放在一个地方(就像本地化一样,但每个用户都不同)。
我们最初的想法是使用 MongoDB(通过 MongoMapper)创建一个存储字符串的自定义文档。然后我们就可以不断添加新的字符串,而无需进行迁移。
我们会将字符串存储为如下形式:
"Testing out a string at #{object.datetime}."
稍后用户可以使用表单,我们必须编辑这些字符串以供其使用(与其他用户隔离)。
然后,当我们想要使用该字符串时,我们可以用一个值替换#{},或者以某种方式告诉嵌入的表达式评估。
问题:在定义带有嵌入表达式的字符串后,如何计算它?有没有类似 to_sym 的东西来创建符号?
有更好的方法吗?
谢谢!
Originally we built out a service on Rails that used static strings with embedded expressions (#{}) for values like the datetime of an object. These strings were used to build communications to other users.
We want to provide a way for our users to customize default text with embedded expressions.
They are currently scattered throughout our models, controllers and views, but we want to put them all in one place (like localization, but it is different per user).
Our initial thinking is to use MongoDB (via MongoMapper) to create a customization document that stores the strings. Then we can continuously add new strings without doing a migration.
We would store the string as something like this:
"Testing out a string at #{object.datetime}."
Later a user can use a form we have to edit these strings for their use (isolated from other users).
Then when we want to use the string we would either replace the #{} with a value or somehow tell the embedded expression evaluate then.
Question: how do I later calculate a string with embedded expressions after it is defined? Is there something like to_sym for creating symbols?
Is there a better way to do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要使用或编写自己的模板引擎。通过模板引擎,我的意思很简单:
您可能还喜欢用于此目的的液体或小胡子。您可以立即拒绝任何涉及 eval 的解决方案:它们对于不受信任的字符串并不安全。
就存储配置值而言,mongo 似乎很好。
You'll want to use or write your own templating engine. And by templating engine, I mean something as simple as:
You might also like liquid or mustache for this purpose. You can reject out of hand any solutions involving eval: they aren't safe with untrusted strings.
As far as storing the configuration values, mongo seems fine.
显然,facets 允许您使用 String.interpolate 来做到这一点。我在观看此视频时遇到了它< /a> 今天早上。
Apparently facets allows you to do this, using
String.interpolate
. I came across it while watching this video this morning.