【问题标题】:Ruby Sinatra Embed erb Partial External HTML FileRuby Sinatra 嵌入 erb 部分外部 HTML 文件
【发布时间】:2019-02-11 22:43:22
【问题描述】:

我需要在我的网站上保存“采购合同”类型的报告。我正在使用Sinatra 使用erb 文件来传递内容。当人们注册各种项目时,我想通过电子邮件发送当前报告(版本会更改)。

我想我可以将它以某种格式存储在数据库或外部文件中,这样我就可以两者都做:

  • 将其导入erb 文件以在网络上展示
  • 在电子邮件中使用它,以便以文本格式阅读

所以基本上我需要尽可能基本的格式,但它必须翻译成HTML (erb) 和文本。

我对这个文件的格式有哪些选择?我怎样才能把它翻译成 HTML?我看过markdown,我发现gems 不是很漂亮,我发现它翻译成文本。看到它需要纯文本以及HTML,我有点不知道如何完成这项工作。

文件片段

Privacy Policy
Updated Feb 20, 2019

Website.com (“Website”) is a private business. In this Privacy Statement the terms “we” and “our” refer to Website. This Privacy Statement explains Website’s practices regarding personal information of our users and visitors to this website (the “Website”), as well as those who have transactions with us through telephone, Internet, faxes and other means of communications.

Website’s Commitment to Privacy
At Website, we are committed to respecting the privacy of our members and our Website visitors. For that reason we have taken, and will continue to take, measures to help protect the privacy of personal information held by us.

This Privacy Statement provides you with details regarding: (1) how and why we collect personal information; (2) what we do with that information; (3) the steps that we take to help ensure that access to that information is secure; (4) how you can access personal information pertaining to you; and (5) who you should contact if you have questions and concerns about our policies or practices.

【问题讨论】:

  • 有点不清楚你在问什么,但肯定有可能。看看sinatra partial
  • @iain 是的,我将重组问题。
  • 你见过docs for ERB吗?使用它,您应该能够从任何地方渲染模板。还可以阅读Sinatra README 的视图/模板部分。如果在那之后您仍然不清楚,请大声喊叫,我会在下面写更长的内容。
  • @iain 是的,我有。使用erb 已经有一段时间了。我有一个File.read(filepath) 输出,但它的格式不正确(privacy.txt)。我经常阅读 Sinatra README,但大部分内容仍然令人困惑。
  • 我已经把答案放在下面了,如果您有任何问题,请添加一些 cmets,我会尝试对其进行扩展。

标签: ruby sinatra embed erb


【解决方案1】:

解决方案:将文件另存为HTML,并使用此gem转换为text

https://github.com/soundasleep/html2text_ruby

如果HTML 足够简单,则可以正常工作。

Remaining:仍然存在将HTML 文件用作partial 的问题。

已解决

@text = markdown File.read('views/privacy.md')

因此将源文件保存为markdown 文件,该文件可以转换为HTML。当我需要email 版本时,我需要使用HTML2text gem 转换为HTML,然后转换为texthttps://rubygems.org/gems/html2text

【讨论】:

    【解决方案2】:

    据我了解,您有一部分文本(存储在数据库或文件中,在哪里并不重要)并且您想要:

    • 通过网页以 HTML 格式提供此服务
    • 通过电子邮件直接发送

    假设视图目录位于项目目录中的标准 Sinatra 项目布局,例如

    project-root/
      app.rb
      views/
    

    以及在app.rb 中传递文本的路线:

    get "/sometext" do
    
    end
    

    如果您将erb 模板放在views 目录中,并在路由的最后一行调用erb 模板渲染器,您应该得到HTML 格式的输出。例如

    project-root/
      app.rb
      views/
        sometext.erb # this is the erb template
    

    在 Sinatra 应用中

    # app.rb
    # I'm assuming you've some way to differentiate
    # bits of text, e.g.
    get "/sometext/:id" do |id|
      @text = DB.sometext.getid id # my fake database call
      erb :sometext # <- this will render it, make it the final statement of the block
      # make sure @text is in the template
      # else use locals, e.g.
      # erb :sometext, :locals => { text: @text }
    end
    

    现在,当用户访问http://example.org/sometext/485995 时,他们将收到 HTML。可以通过网站或您选择的其他方法触发将文本通过电子邮件发送给用户。

    【讨论】:

    • 我已经使用这种方法为HTML 服务了一段时间。但它是HTML。文本文件是普通的text。服务它不是问题。它将text 文件翻译成HTML,同时保留原始文件中的文本,以便于发送电子邮件。
    • @Rich_F 我一定遗漏了一些明显的东西。您已经将文本存储在某处,因为您将其传递给模板,对吧?
    • 是的。但是格式不会更改为HTML,同时尊重&lt;CR&gt;等。
    • @Rich_F 您能否在问题中添加文本示例(甚至几行)以及您希望 HTML 和电子邮件如何格式化的示例?我认为这会让事情变得清晰。
    • 明天会讲到。现在是凌晨 2 点。
    猜你喜欢
    • 2013-11-14
    • 2014-02-10
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2014-04-25
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多