class Ezamar::Template

This class is responsible for initializing and compiling the template.

Public Class Methods

new(template, options = {}) click to toggle source

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.

# File lib/ezamar/engine.rb, line 22
def initialize(template, options = {})
  @template, @options = template, options
  compile
end

Public Instance Methods

compile() click to toggle source

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
# 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) click to toggle source

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

# File lib/ezamar/engine.rb, line 70
def result(binding)
  eval(@compiled, binding, @options[:file]).strip
end