Class: Turing::CGIHandler::Template
- Inherits:
-
Object
- Object
- Turing::CGIHandler::Template
- Defined in:
- lib/turing/cgi_handler.rb
Overview
Simple template engine
Essentially just convenient wrapper around ERB which is used internally by Turing::CGIHandler.
Instance Method Summary collapse
-
#h(var) ⇒ Object
shortcut for
CGI.escapeHTML
. -
#initialize(what, variables = nil) ⇒ Template
constructor
Specify template file (
what
) andvariables
that will be pushed as instance variables to the Template. -
#render ⇒ Object
render given template and return result as string.
Constructor Details
#initialize(what, variables = nil) ⇒ Template
Specify template file (what
) and variables
that will be pushed as instance variables to the Template.
218 219 220 221 222 223 |
# File 'lib/turing/cgi_handler.rb', line 218 def initialize(what, variables = nil) # {{{ (variables || {}).each do |k,v| instance_variable_set("@" + k.to_s, v) end @__what__ = what end |
Instance Method Details
#h(var) ⇒ Object
shortcut for CGI.escapeHTML
can you say “Rails” ? :)
236 237 238 |
# File 'lib/turing/cgi_handler.rb', line 236 def h(var) # {{{ CGI.escapeHTML(var) end |
#render ⇒ Object
render given template and return result as string.
226 227 228 229 230 231 |
# File 'lib/turing/cgi_handler.rb', line 226 def render # {{{ erb = ERB.new(File.open(@__what__).read, nil, '%-') erb.result(binding) rescue raise "Failure rendering template #{@what}: #{$!}" end |