Rich_F
Rich_F

Reputation: 2086

Ruby Sinatra Embed erb Partial External HTML File

I have a need to hold a "Purchase Contract" type of report in my website. I am using Sinatra using erb files to deliver content. I would like to email the current report (the versions will change) out when people sign up for various items.

I'm thinking I can house it in the database, or an external file, in some kind of format, so I can do the both:

So basically I need it in a format that's basic as possible, but it has to translate into HTML (erb) and text.

What are my options with the format of this file? And how can I translate that into HTML? I've looked at markdown and it's not very pretty with the gems that I find that translate to text. Seeing that it needs plain text as well as HTML I'm a bit lost as to how to get this done.

File Snippet

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.

Upvotes: 0

Views: 473

Answers (2)

Rich_F
Rich_F

Reputation: 2086

Solution: Save the file as HTML and use this gem for conversion into text:

https://github.com/soundasleep/html2text_ruby

Works fine if the HTML is simple enough.

Remaining: Still have the issue as using the HTML file as a partial.

Solved:

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

So park the source file as a markdown file, which can translate to HTML. When I need the email version, I need to translate to HTML then to text using the HTML2text gem. https://rubygems.org/gems/html2text

Upvotes: 1

ian
ian

Reputation: 12251

As I understand it, you have a portion of text (stored in a database or a file, it doesn't really matter where) and you want to:

  • serve this up formatted as HTML via a webpage
  • send it plain via email

Assuming a standard Sinatra project layout where the views directory lives in the project dir, e.g.

project-root/
  app.rb
  views/

and a route to deliver the text in app.rb:

get "/sometext" do

end

If you put the erb template in the views directory and as the last line of the route make a call to the erb template renderer you should get the output in HTML. e.g.

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

In the Sinatra app

# 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

Now when a user visits http://example.org/sometext/485995 they will receive HTML. Emailing the text to the user could be triggered via the website or some other method of your choice.

Upvotes: 0

Related Questions