Module: ErbSandbox
- Defined in:
- lib/erb_sandbox.rb,
lib/erb_sandbox/exceptions.rb
Overview
Module contains methods for rendering ERB templates in “sandbox” so code in template will not be able to use your environment (methods, classes, …) It’s useful when you want to encapsulate your program from ERB (e.g. encapsulate models)
Defined Under Namespace
Classes: StatusIsNotZero
Class Method Summary collapse
-
.render(template, variables = {}) ⇒ String
uses ‘erb` (yes-yes, it’s slow:( ) to render template with some predefined variables example: ErbSandbox.render ‘Hello, <%= user %>’, user: ‘dude’ # => “Hello, dude”.
-
.render_file(path, variables = {}) ⇒ Object
wrapping on ErbSandbox.render(File.read(path), variables).
Class Method Details
.render(template, variables = {}) ⇒ String
uses ‘erb` (yes-yes, it’s slow:( ) to render template with some predefined variables example:
ErbSandbox.render 'Hello, <%= user %>', user: 'dude' # => "Hello, dude"
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/erb_sandbox.rb', line 21 def self.render(template, variables = {}) file = Tempfile.new 'erb_sandbox', encoding: 'UTF-8' file.write "<%#-*- coding: UTF-8 -*-%>\n" file.write variables_init_code variables file.write template file.close output, status = Open3.capture2e "erb -T 1 #{file.path}" # supress stderr file.unlink fail(StatusIsNotZero, output) unless status.to_i.zero? output end |
.render_file(path, variables = {}) ⇒ Object
wrapping on ErbSandbox.render(File.read(path), variables)
40 41 42 |
# File 'lib/erb_sandbox.rb', line 40 def self.render_file(path, variables = {}) render File.read(path), variables end |