使用 Rails 和 ERB 从模板文件目录生成文件目录的最合适方法?

发布于 2024-10-18 09:59:44 字数 514 浏览 3 评论 0原文

我的目标是在我的 Rails (3) 应用程序中生成静态 html、javascript 和图像文件的目录,由 ERB 模板驱动。例如,作为开发人员,我可能想要生成/更新这些文件:

#{Rails.root}/public/products/baseball.html
#{Rails.root}/public/products/football.js

..来自以下模板文件:

#{Rails.root}/product_templates/baseball.html.erb
#{Rails.root}/product_templates/football.js.erb

理想情况下,模板可以访问我的应用程序的 Rails 环境(包括 URL 帮助程序、视图帮助程序、部分文件等)。

实现这一目标的最新、最好的方法是什么?

我尝试了自定义 Rails 生成器,但发现我需要编写自定义逻辑来跳过非 ERB 文件、替换文件名等。必须有更好的方法。

My goal is to generate a directory of static html, javascript, and image files within my Rails (3) app, driven by ERB templates. For example, as a developer I might want to generate/update these files:

#{Rails.root}/public/products/baseball.html
#{Rails.root}/public/products/football.js

..from the following template files:

#{Rails.root}/product_templates/baseball.html.erb
#{Rails.root}/product_templates/football.js.erb

Ideally the templates would have access to my app's Rails environment (including URL helpers, view helpers, partials, etc.).

What's the latest and greatest way to accomplish this?

I experimented with a custom Rails generator, but found that I needed to write custom logic for skipping non-ERB files, substituting file names, etc. There must be a better way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘人皮目录ツ 2024-10-25 09:59:44

我不确定你到底想做什么,这可能有助于提供更好的答案,但这里有一些有用的信息:

你可以直接调用 erb,关于这方面的一些信息在这里,可能已经在做了:

http://www.ruby-doc.org/stdlib/libdoc /erb/rdoc/classes/ERB.html

对于模板文件列表,简单的 Dir.glob 应该能够帮助轻松找到特定文件并循环遍历它们:

http://ruby-doc.org/core/classes/Dir.html#M000629

我不会的棘手部分我不知道如何向您提供有关访问助手和 Rails 提供的其他内容的建议。您编写的帮助程序只是模块,因此您可以将它们混合在一起,使用内置的 Rails 帮助程序也可以实现类似的功能。


这很有趣且相关,但并没有直接回答您的问题,因为它使用 Liquid 模板引擎而不是 ERB,但除此之外,它还执行您正在讨论的一些静态站点生成:

https://github.com/mojombo/jekyll

I'm not sure what you are trying to do exactly, that may help provide better answers, but here is some useful information:

You can call into erb directly, some information on that is here, which have probably already been doing:

http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html

For the list of template files an easy Dir.glob should be able to help find the specific files easily and loop through them:

http://ruby-doc.org/core/classes/Dir.html#M000629

The tricky part I wouldn't know how to advise you on is getting access to the helpers and other things Rails provides. The helpers that you write are just modules, so you could mix those in, something similar might be possible with the built-in rails helpers.


This is interesting and related but doesn't directly answer your question, since its uses the Liquid templating engine instead of ERB, but otherwise, it does some of the static site generation you are talking about:

https://github.com/mojombo/jekyll

软糖 2024-10-25 09:59:44

这就是我完成类似事情的方法。它接受源目录和目标目录,清除目标目录,然后处理源目录,或者 ERB 处理文件并将它们放置在目标目录中,或者只是复制它们(对于 on-ERB 文件)。需要对其进行修改以递归地处理目录。

我从 rake 任务中调用它,如下所示:

DirectoryGenerator.new.generate(Rails.root.join('src'), Rails.root.join('public', 'dest'))


class DirectoryGenerator
  include Rails.application.routes.url_helpers
  include ActionView::Helpers::TagHelper
  default_url_options[:host] = 'www.example.com'

  def generate(source, destination)
    FileUtils.rmtree(destination)
    FileUtils.mkdir_p(destination)

    Dir.glob(File.join(source, '*')).each do |path|
      pathname = Pathname.new(path)
      if pathname.extname == '.erb'
        File.open(destination.join(pathname.basename.sub(/\.erb$/, '')), 'w') do |file|
          file.puts(ERB.new(File.read(path)).result(binding))
        end
      else
        FileUtils.cp(pathname, File.join(destination, pathname.basename))
      end
    end
  end
end

This is how I accomplished something similar. It accepts source and destination directories, wipes out the destination, then processes the source directory, either ERB-processing files and placing them in the destination or simply copying them (in the case of on-ERB files). It would need to be modified to handle recursively processing a directory.

I invoke it from a rake task like so:

DirectoryGenerator.new.generate(Rails.root.join('src'), Rails.root.join('public', 'dest'))


class DirectoryGenerator
  include Rails.application.routes.url_helpers
  include ActionView::Helpers::TagHelper
  default_url_options[:host] = 'www.example.com'

  def generate(source, destination)
    FileUtils.rmtree(destination)
    FileUtils.mkdir_p(destination)

    Dir.glob(File.join(source, '*')).each do |path|
      pathname = Pathname.new(path)
      if pathname.extname == '.erb'
        File.open(destination.join(pathname.basename.sub(/\.erb$/, '')), 'w') do |file|
          file.puts(ERB.new(File.read(path)).result(binding))
        end
      else
        FileUtils.cp(pathname, File.join(destination, pathname.basename))
      end
    end
  end
end
溺渁∝ 2024-10-25 09:59:44

您研究过 Rails 模板吗?

例如 http://m.onkey.org/rails-templates ..

不确定你是什么正在准确地达到..您是否试图通过提供一些参数来生成客户端站点..这就是最终目标?

Have you looked into Rails templates?

http://m.onkey.org/rails-templates for instance..

Not sure what you are getting at exactly.. are you trying to generate client sites by providing a few parameters.. that the end goal?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文