I'm writing a command-line tool that will ultimately output an HTML report. The tool is written in Ruby. (I am not using Rails). I'm trying to keep the logic of the application in one set of files, and the HTML templates (the .erb files) in another set.
I'm having a really annoying problem though: I can't successfully include one .erb file into another.
To be specific, I'm trying to do something like this (in pseudo-code):
<html> <head> <style type='text/css'> [include a stylesheet here] [and another one here] </style> </head> <body> <p>The rest of my document follows...
That example snippet is itself an erb file, which is being invoked from within the application logic.
I'm doing things this way so I can keep my stylesheets out of the main template to make it easier/cleaner to maintain the application. The end product (the report), though, needs to be a single, stand-alone HTML file which has no dependencies, and thus, I want to inline those stylesheets into the document head when the report is generated.
This seems like this should be easy, but I've been banging my head against a wall (and Googling, and RTMF'ing) for the last hour, and I'm not having any luck at all.
How is this supposed to be done? Thanks.
Script written in ERB, a templating language for Ruby; may include any type of plain text or source code, but also includes Ruby ERB code that generates additional text into the resulting file when run with the ERB template engine. ERB is often used for templating Web files such as . RB, . RHTML, .
An ERB object works by building a chunk of Ruby code that will output the completed template when run. If safe_level is set to a non-nil value, ERB code will be run in a separate thread with $SAFE set to the provided level. eoutvar can be used to set the name of the variable ERB will build up its output in.
An ERB template looks like a plain-text document interspersed with tags containing Ruby code. When evaluated, this tagged code can modify text in the template. Puppet passes data to templates via special objects and variables, which you can use in the tagged Ruby code to control the templates' output.
ERB templates can be nested by evaluating the sub-template from within <%= %> of the main template.
<%= ERB.new(sub_template_content).result(binding) %>
For example:
require "erb" class Page def initialize title, color @title = title @color = color end def render path content = File.read(File.expand_path(path)) t = ERB.new(content) t.result(binding) end end page = Page.new("Home", "#CCCCCC") puts page.render("home.html.erb")
home.html.erb:
<title><%= @title %></title> <head> <style type="text/css"> <%= render "home.css.erb" %> </style> </head>
home.css.erb:
body { background-color: <%= @color %>; }
produces:
<title>Home</title> <head> <style type="text/css"> body { background-color: #CCCCCC; } </style> </head>
I'm needing this in a Sinatra app, and I find that I can just call it the same way I called the original:
In the sinatra app, I call the index:
erb :index
Then, in the index template, I can do the same for any sub-template:
<div id="controls"> <%= erb :controls %>
..which shows the 'controls.erb' template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With