Class: Teer::Parser
- Inherits:
-
Object
- Object
- Teer::Parser
- Defined in:
- lib/teer/parser.rb
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #register_helper(func, &fn) ⇒ Object
- #render(template = '', ctx = {}) ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
5 6 7 |
# File 'lib/teer/parser.rb', line 5 def initialize @helpers = {} end |
Instance Method Details
#register_helper(func, &fn) ⇒ Object
9 10 11 |
# File 'lib/teer/parser.rb', line 9 def register_helper(func, &fn) @helpers[func.to_sym] = fn end |
#render(template = '', ctx = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/teer/parser.rb', line 13 def render(template = '', ctx = {}) rendered_template = template.dup template.scan(/{{\s*[\w\. ]+\s*}}/).map do |substr| key, func = parse_func(substr) value = value_from_ctx(ctx, key) if func raise ArgumentError, "Missing helper: '#{func}'" if !@helpers[func.to_sym] value = @helpers[func.to_sym].call(ctx, value) end rendered_template[substr] = value.to_s end rendered_template end |