如何创建新的绑定并为其分配实例变量以使其在ERB中可用? [英] How to create new binding and assign instance variables to it for availability in ERB?

查看:56
本文介绍了如何创建新的绑定并为其分配实例变量以使其在ERB中可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ruby项目(无轨)中实现HTML模板。为此,我将使用ERB,但是我对绑定的东西有些担心。

I'm implementing HTML templating in a Ruby project (non-rails). To do this I'll be using ERB, but I have some concerns about the binding stuff.

首先,这是我到目前为止所使用的方法:

First out, this is the method I got so far:

def self.template(template, data)
  template = File.read("#{ENV.root}/app/templates/#{template}.html.erb")

  template_binding = binding.clone

  data.each do |k, v|
    template_binding.local_variable_set(k, v)
  end

  ERB.new(template).result(template_binding)
end

我会这样做

Email.template('email/hello', {
  name: 'Bill',
  age:  41
}

当前解决方案有两个问题。

There are two issues with the current solution though.

首先,我m克隆当前绑定。我想创建一个新绑定。我尝试使用 Class.new.binding 创建一个新绑定,但是由于 binding 是一个私有方法,无法以这种方式获得。
我想要一个新方法的原因是我想避免实例变量泄漏到ERB文件或从ERB文件泄漏的风险(克隆

First, I'm cloning the current binding. I want to create a new one. I tried Class.new.binding to create a new, but since binding is a private method it can't be obtained that way. The reason I want a new one is that I want to avoid the risk of instance variables leaking into or out from the ERB file (cloning only takes care of the latter case).

第二,我希望传递给ERB文件的变量作为实例变量公开。我尝试了 template_binding.instance_variable_set ,通过了在哈希键 k 中,该键抱怨它不是有效的实例变量名称,并且 @#{k} ,它没有提出投诉,但也没有在ERB代码中使用。
我要使用实例变量的原因是,依赖于此代码的人们很熟悉这种约定。

Second, I want the variables passed to the ERB file to be exposed as instance variables. Here I tried with template_binding.instance_variable_set, passing the plain hash key k which complained that it wasn't a valid instance variable name and "@#{k}", which did not complain but also didn't get available in the ERB code. The reason I want to use instance variables is that it's a convention that the people relying on this code is familiar with.

我在此处检查了一些主题堆栈溢出,例如使用带有哈希值的ERB模板,但是提供的答案不能解决我正在讨论的问题。

I have checked some topics here at Stack Overflow such as Render an ERB template with values from a hash, but the answers provided does not address the problems I'm discussing.

因此,简而言之,就像标题:如何创建新的绑定和将实例变量分配给它以在ERB中使用?

So in short, like the title: How to create new binding and assign instance variables to it for availability in ERB?

推荐答案

1)无需克隆,便会创建新的绑定

1) No need to clone, new binding is created for you each time.

我已经在irb中对此进行了测试:

I have tested this in irb:

class A; def bind; binding; end; end
a = A.new
bind_1 = a.bind
bind_2 = a.bind

bind_1.local_variable_set(:x, 2)
=> 2
bind_1.local_variables
=> [:x]
bind_2.local_variables
=> []

2)打开对象Eigenclass并添加 attr_accessor

2) Open the objects Eigenclass and add attr_accessor to it

class << template_binding  # this opens Eigenclass for object template_binding
  attr_accessor :x
end

So在ruby中,您可以打开任何类并为其添加方法。
Eigenclass表示单个对象的类-每个对象都可以具有自定义类定义。来自C#,直到现在我都无法想象会使用这种情况。 :)

So in ruby you can just open any class and add methods for it. Eigenclass means class of a single object - each object can have custom class definition. Coming from C# I couldn't imagine a situation where this would be used, until now. :)

对每个哈希执行此操作

data.each do |k, v|
  class << template_binding; attr_accessor k.to_sym; end
  template_binding.k = v
end

这篇关于如何创建新的绑定并为其分配实例变量以使其在ERB中可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆