Ruby 在中间人中呈现单个文件
我正在编写一个助手,我需要获取一个作为字符串的渲染文件 我发现我需要的方法存在于中间人的库中: 如何从助手类调用此函数 我试过:Ruby 在中间人中呈现单个文件,ruby,middleman,Ruby,Middleman,我正在编写一个助手,我需要获取一个作为字符串的渲染文件 我发现我需要的方法存在于中间人的库中: 如何从助手类调用此函数 我试过: require "middleman-core/lib/middleman-core/core_extensions/rendering.rb" 但它似乎找不到文件,知道吗?我不确定3.0测试版是否已经为黄金时间做好了准备 也就是说,听起来你正在寻找的是部分方法。除非我遗漏了什么,否则中间人方法似乎是一个过于复杂的解决方案。对于我的一个站点,我希望将整个文本文件加
require "middleman-core/lib/middleman-core/core_extensions/rendering.rb"
但它似乎找不到文件,知道吗?我不确定3.0测试版是否已经为黄金时间做好了准备
也就是说,听起来你正在寻找的是部分方法。除非我遗漏了什么,否则中间人方法似乎是一个过于复杂的解决方案。对于我的一个站点,我希望将整个文本文件加载到模板中,因此我编写了以下帮助程序:
# Shortcut for loading raw text files. Obviously assumes that given file is in a valid format.
# @return [String] File contents
def load_textfile(filename)
File.read filename.to_s
end
同样,您应该澄清您是打算在模板中还是在Ruby代码中使用它。根据你的问题,我不清楚
以下是如何使用上述帮助器的示例:
config.rb
文件的helper
块中,如下所示:
helpers do
# Define helper functions here to make them available in templates
end
= load_textfile 'path'
p You can embed helper output in your page with interpolation, too: #{load_textfile 'path'}
你为什么不使用分部法呢?
= load_textfile 'path'
p You can embed helper output in your page with interpolation, too: #{load_textfile 'path'}