Created
June 24, 2013 05:52
-
-
Save agilecreativity/5847981 to your computer and use it in GitHub Desktop.
Erubis example with ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
## encoding: utf-8 | |
require 'pry' | |
require 'erubis' | |
module Example | |
# dummy data for testing | |
class Sample | |
def self.sample_list | |
%w(first second thrid) | |
end | |
def self.sample_hash | |
{ first: "first value", second: "second value", thrid: "third value" } | |
end | |
end | |
class ErubisDemo | |
def render(input_template = 'tempalte.erb') | |
input_template = File.read(input_template) | |
template = Erubis::Eruby.new(input_template) | |
result = template.result sample_list: Sample.sample_list, | |
sample_hash: Sample.sample_hash | |
result | |
end | |
end | |
end | |
if __FILE__ == $0 | |
include Example | |
demo = ErubisDemo.new | |
result = demo.render File.dirname(__FILE__) + "/../templates/template.erb" | |
puts result | |
# Output: | |
# Sample List: | |
# list first | |
# list second | |
# list thrid | |
# | |
# Sample Hash: | |
# hash[first] = first value | |
# hash[second] = second value | |
# hash[thrid] = third value | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment