Ruby Sinatra嵌入了部分外部HTML文件

问题描述 投票:0回答:2

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

我想我可以把它放在数据库中,或者以某种格式存放在外部文件中,所以我可以做到这两点:

  • 将其导入erb文件以在Web上显示
  • 在电子邮件中使用它,以便以文本格式读取

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

这个文件的格式有哪些选择?我怎样才能将其翻译成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.
ruby sinatra embed erb
2个回答
1
投票

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

https://github.com/soundasleep/html2text_ruby

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

剩余:仍然有使用HTML文件作为partial的问题。

解决了:

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

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


0
投票

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

  • 通过网页将其格式化为HTML
  • 通过电子邮件发送简单

假设一个标准的Sinatra项目布局,其中views目录位于项目目录中,例如

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。通过网站或您选择的其他方法可以触发通过电子邮件发送给用户的文本。

© www.soinside.com 2019 - 2024. All rights reserved.