【问题标题】:How to render variable from placeholders in a string如何从字符串中的占位符呈现变量
【发布时间】:2013-10-02 08:54:09
【问题描述】:

我正在创建一些用户可以单击以填充文本区域的消息建议。显示时,通用字符串必须与用户的详细信息一起呈现。

这是我想要做的,但是这个例子使用了 html 编码的消息,这些消息不是从数据库中获取的:

<ul>
    <li><a><%= "#{@user.name} is the best" %></a></li>
    <li><a><%= "#{@user.name} is the worst" %></a></li>
    <li><a><%= "I think #{@user.name} is the best" %></a></li>
    <li><a><%= "I think #{@user.name} is the worst" %></a></li>
</ul>

我希望能够将带有“占位符”的通用字符串存储在数据库中,并且只计算视图中的值。

这就是我尝试在数据库中(在种子文件中)创建字符串的方式

Suggestion.create(message: '#{@user.name} is the best')
Suggestion.create(message: '<%= #{@user.name} %> is the best')
Suggestion.create(message: '<%= @user.name %> is the best')

在视图中我有一个迭代

<%= suggestion.message %>

我正在尝试在渲染之前将 ruby​​ 代码添加到视图中。可能是个愚蠢的想法。

这是在 html 源代码中显示的内容

&lt;%= @user.name %&gt; is the best
&lt;%= #{@user.name} %&gt; is the best
#{@user.name} is the best

这里有一些类似的东西,但它附加了无法使用的消息,因为变量在每条消息中的不同位置:

<ul>
    <% @suggestions.each do |message| %>
        <li><a><%= "#{@user.name} message" %></a></li>
    <% end %>
</ul>

【问题讨论】:

  • 您的视图可能使用了.html 扩展而不是.html.erb
  • 它使用的是 .erb 扩展名
  • &lt;a&gt; 标签在哪里?在代码中的字符串中,还是在视图中?
  • 当您使用浏览器点击该页面时,您的日志文件会显示什么?
  • 您需要向我们展示更多代码。你可以将你的项目提交到 github 以便我们审核吗?

标签: ruby-on-rails ruby liquid


【解决方案1】:

您正在尝试将一组模板存储在数据库中,然后将这些模板呈现到您的视图中。

你应该使用液体

http://liquidmarkup.org/

示例 sn-p:

<ul id="products">
  {% for product in products %}
    <li>
      <h2>{{ product.title }}</h2>
      Only {{ product.price | format_as_money }}

      <p>{{ product.description | prettyprint | truncate: 200  }}</p>

    </li>
  {% endfor %}
</ul>

要渲染的代码

Liquid::Template.parse(template).render 'products' => Product.find(:all)

如何使用它:

class Suggestion < AR::Base
  validate :message, presence: true

  def render_with(user)
    Liquid::Template.parse(message).render user: user
  end
end


Suggestion.create(message: "{{user.name}} is the best")
Suggestion.create(message: "{{user.name}} is the worst")
Suggestion.create(message: "{{user.name}} is the awesome")

<ul>
  <% Suggestion.all.each do |suggestion| %>
    <li><%= suggestion.render_with(@user) %>
  <% end %>
</ul>

【讨论】:

  • 太棒了。谢谢!非常感谢我在示例中如何使用液体的示例。我不得不添加: def to_liquid {'name'=>self.name} end 到用户模型。
【解决方案2】:

不确定这是否是您想要的,但是当@user 可能是nil 时,这里有一些可行的解决方案:

"#{@user.try(:name)} is the best in the biz"
"%s is the best in the biz" % @user.try(:name)
"#{name} is the best in the biz" % { name: @user.try(:name) }

try 如果在 nil 上调用,将返回 nil。

如果 html 输出仍然被转义,请尝试以下方法之一:

raw(expression)
expression.html_safe

【讨论】:

    【解决方案3】:

    如果您想为每个用户显示此消息,那么您应该将其设为方法调用:

    class Suggestion < AR::Base
      belongs_to :user
    
      def default_message
        "#{user.name} is the best"
      end
    end
    
    @user       = User.new(name: "Bob")
    @suggestion = Suggestion.create(user: @user)
    @suggestion.default_message #=> "Bob is the best"
    

    【讨论】:

    • 它在种子文件中不起作用,因为当我希望它存储 rails 代码时,它会尝试插入字符串(并给出 @user 为 nil 的错误)
    • 我不希望它存储每个用户的计算值。这个想法是存储消息的通用版本,以便在显示时可以计算出来。
    • 我不希望它存储每个用户的计算值。
    • 它没有。它存储用户 ID。
    • 不存储消息。它们是在您调用 default_message 时生成的。
    猜你喜欢
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2013-04-10
    • 2014-05-02
    相关资源
    最近更新 更多