Class: Ezamar::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/ezamar/engine.rb

Overview

This class is responsible for initializing and compiling the template.

Instance Method Summary collapse

Constructor Details

#initialize(template, options = {}) ⇒ Template

Take a template (anything that responds to ::to_str) and options. At the moment the only option used is :file, which is used to tell Kernel::eval how to produce better backtraces.



22
23
24
25
# File 'lib/ezamar/engine.rb', line 22

def initialize(template, options = {})
  @template, @options = template, options
  compile
end

Instance Method Details

#compileObject

All ye who seek magic, look elsewhere, this method is ASAP (as simple as possible)

There are some simple gsubs that build a final template which is evaluated

The rules are following: <?r rubycode ?>

evaluate the code inside the tag, this is considered XHTML-valid and so is the
preferred method for executing code inside your templates.
The return-value is ignored

<% rubycode %>

The same as <?r ?>, ERB-style and not valid XHTML, but should give someone who
is already familiar with ERB some common ground

#{ rubycode }

You know this from normal ruby already and it's actually nothing else.
Interpolation at the position in the template, isn't any special taggy format
and therefor safe to use.

<%= rubycode %>

The result of this will be interpolated at the position in the template.
Not valid XHTML either.

TODO

- provide C version or maybe use erbuis


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ezamar/engine.rb', line 50

def compile
  temp = @template.dup
  start_heredoc = "T" << Digest::SHA1.hexdigest(temp)
  start_heredoc, end_heredoc = "\n<<#{start_heredoc}\n", "\n#{start_heredoc}\n"
  bufadd = "_out_ << "

  temp.gsub!(/<%(?!=)\s*(.*?)\s*%>/m,
        "#{end_heredoc} \\1; #{bufadd} #{start_heredoc}")
  temp.gsub!(/<\?r\s+(.*?)\s+\?>/m,
        "#{end_heredoc} \\1; #{bufadd} #{start_heredoc}")
  temp.gsub!(/<%=\s*(.*?)\s*%>/m,
        "#{end_heredoc} #{bufadd} (\\1); #{bufadd} #{start_heredoc}")

  @compiled = "_out_ = ''
  #{bufadd} #{start_heredoc} #{temp} #{end_heredoc}
  _out_"
end

#result(binding) ⇒ Object

Takes a binding and evals it with the previously set options.



70
71
72
# File 'lib/ezamar/engine.rb', line 70

def result(binding)
  eval(@compiled, binding, @options[:file]).strip
end