Ruby Sinatra 嵌入 erb 部分外部 HTML 文件
Ruby Sinatra Embed erb Partial External HTML File
我需要在我的网站上保留 "Purchase Contract" 类型的报告。我正在使用 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.
据我了解,您有一部分文本(存储在数据库或文件中,存储在何处并不重要)并且您想要:
- 通过网页
将其格式化为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
,用这个gem转换成text
:
https://github.com/soundasleep/html2text_ruby
如果 HTML
足够简单,则工作正常。
剩余:将HTML
文件用作partial
时仍然存在问题。
已解决:
@text = markdown File.read('views/privacy.md')
所以将源文件作为markdown
文件,可以翻译成HTML
。当我需要 email
版本时,我需要转换为 HTML
,然后使用 HTML2text
gem 转换为 text
。 https://rubygems.org/gems/html2text
我需要在我的网站上保留 "Purchase Contract" 类型的报告。我正在使用 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.
据我了解,您有一部分文本(存储在数据库或文件中,存储在何处并不重要)并且您想要:
- 通过网页 将其格式化为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
,用这个gem转换成text
:
https://github.com/soundasleep/html2text_ruby
如果 HTML
足够简单,则工作正常。
剩余:将HTML
文件用作partial
时仍然存在问题。
已解决:
@text = markdown File.read('views/privacy.md')
所以将源文件作为markdown
文件,可以翻译成HTML
。当我需要 email
版本时,我需要转换为 HTML
,然后使用 HTML2text
gem 转换为 text
。 https://rubygems.org/gems/html2text