Does anyone know how to render a partial (or at least include another file) within a asset file in rails? I have a templates/ folder setup (which contains static HTML files) and there bits and pieces within these files that I would like to abstract and share between other template files.
有谁知道如何在rails中的资产文件中呈现部分(或至少包括另一个文件)?我有一个模板/文件夹设置(其中包含静态HTML文件)以及这些文件中的部分内容,我想在其他模板文件之间进行抽象和共享。
The render
method doesn't work in any asset pipeline files. I'm sure I can include it somehow by finding the correct module, but I'm not sure if this would work since the render module itself may rely on other modules, methods, variables.
render方法在任何资产管道文件中都不起作用。我确信我可以通过找到正确的模块以某种方式包含它,但我不确定这是否可行,因为渲染模块本身可能依赖于其他模块,方法,变量。
Any ideas on how to do this? Or if its possible?
关于如何做到这一点的任何想法?或者如果可能的话?
1 个解决方案
#1
3
I've come up with my own solution.
我想出了自己的解决方案。
You create a helper file in the lib folder and call it assets_helper.rb
. The code within the asset_helper is as follows:
您在lib文件夹中创建一个帮助文件,并将其命名为assets_helper.rb。 asset_helper中的代码如下:
def partial(name, args = {}, dir = 'app/assets/templates')
require 'ostruct'
namespace = OpenStruct.new(args)
name += '.html'
[name + '.erb', name].each do |n|
n = File.join(dir, n)
next unless File.exist?(n)
tpl = ERB.new(File.new(n).read)
compiled = tpl.result(namespace.instance_eval { binding })
return compiled
end
end
Then you include that file at the top of each asset file that you wish to use it in. Inside of the file you can use the partial command like so.
然后将该文件包含在您希望使用它的每个资产文件的顶部。在文件内部,您可以使用部分命令。
<% require './lib/asset_helper.rb' %>
<!--- some HTML template --->
<%= partial('page/_partial', :var1 => true, :var2 => false) %>
<---- some other page --->
This works. But the only issue is that the Rails 3.1 will cache anything that's in the assets folder. So if you edit a partial you will need to update the root file that it's inside of so that the cache gets cleared for that file.
这很有效。但唯一的问题是Rails 3.1将缓存assets文件夹中的任何内容。因此,如果您编辑部分内容,则需要更新其内部的根文件,以便为该文件清除缓存。
#1
3
I've come up with my own solution.
我想出了自己的解决方案。
You create a helper file in the lib folder and call it assets_helper.rb
. The code within the asset_helper is as follows:
您在lib文件夹中创建一个帮助文件,并将其命名为assets_helper.rb。 asset_helper中的代码如下:
def partial(name, args = {}, dir = 'app/assets/templates')
require 'ostruct'
namespace = OpenStruct.new(args)
name += '.html'
[name + '.erb', name].each do |n|
n = File.join(dir, n)
next unless File.exist?(n)
tpl = ERB.new(File.new(n).read)
compiled = tpl.result(namespace.instance_eval { binding })
return compiled
end
end
Then you include that file at the top of each asset file that you wish to use it in. Inside of the file you can use the partial command like so.
然后将该文件包含在您希望使用它的每个资产文件的顶部。在文件内部,您可以使用部分命令。
<% require './lib/asset_helper.rb' %>
<!--- some HTML template --->
<%= partial('page/_partial', :var1 => true, :var2 => false) %>
<---- some other page --->
This works. But the only issue is that the Rails 3.1 will cache anything that's in the assets folder. So if you edit a partial you will need to update the root file that it's inside of so that the cache gets cleared for that file.
这很有效。但唯一的问题是Rails 3.1将缓存assets文件夹中的任何内容。因此,如果您编辑部分内容,则需要更新其内部的根文件,以便为该文件清除缓存。