Module: Nitro::TemplateMixin

Included in:
FileTemplate, Template, Template
Defined in:
lib/nitro/template.rb

Overview

A template is a text file with embeded Ruby code. The template processor converts the original text file to ruby code and then evaluates this code to produce the result of the template transformation.

Constant Summary collapse

DELIM_HSH =

Set some pretty safe delimiters for templates.

Digest::MD5.hexdigest(Kernel.rand.to_s)
START_DELIM =
"<<NITRO_TEMPLATE_DELIMETER#{DELIM_HSH}\n"
END_DELIM =
"\nNITRO_TEMPLATE_DELIMETER#{DELIM_HSH}\n"

Instance Method Summary collapse

Instance Method Details

#compile_template(template, buffer = '@out', base_dir = File.expand_path(Template.root)) ⇒ Object

Convert a template to actual Ruby code, ready to be evaluated.

template

The template as a String.

buffer

The variable to act as a buffer where the ruby code for this template will be generated. Passed as a String.

base_dir

The base directory where the templates reside.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/nitro/template.rb', line 35

def compile_template(template, buffer = '@out', base_dir = File.expand_path(Template.root))
  text = template.dup

  # gmosx, THINK: what is this ?!?

  buffer = '@out' unless buffer.is_a?(String) or buffer.is_a?(Symbol)

  # Strip the xml header! (interracts with the following gsub!)

  text.gsub!(/<\?xml.*\?>/, '')

  # Transform include instructions <include href="xxx" />
  # must be transformed before the processinc instructions.
  # Useful to include fragments cached on disk
  #--
  # gmosx, FIXME: NOT TESTED! test and add caching.
  # add load_statically_included fixes.
  #++
  
  text.gsub!(/<include\s+href=["'](.*?)["']\s+\/>/, %[<?r File.read("\#{@dispatcher.root}/\\1") ?>])
  
  # xform render/inject instructions <render href="xxx" />
  # must be transformed before the processinc instructions.

  text.gsub!(/<(?:render|inject)\s+href=["'](.*?)["']\s+\/>/, %[<?r render "\\1" ?>])

  # Remove <root> elements. typically removed by xslt but lets
  # play it safe. The <root> element is typically added to
  # template files to make them XHTML valid.
  
  text.gsub!(/<(\/)?root>/, '')
  
  # Transform the processing instructions, use <?r as
  # a marker.
  
  text.gsub!(/<\?r\s+(.*?)\s+\?>/m, "#{END_DELIM}; \\1; #{buffer} << #{START_DELIM}")
  #text.gsub!(/<\?r(\s?)/, "#{END_DELIM}; ")
  #text.gsub!(/\?>/, "; #{buffer} << #{START_DELIM}")
  
  # Transform alternative code tags. 
  # (very useful in xsl stylesheets)
  
  text.gsub!(/<ruby>(.*?)<\/ruby>/m, "#{END_DELIM}; \\1; #{buffer} << #{START_DELIM}")
  #text.gsub!(/<\/ruby>/, "; #{buffer} << #{START_DELIM}")
  #text.gsub!(/<ruby>/, "#{END_DELIM}; ")

  # Also handle erb/asp/jsp style tags. Those tags
  # *cannot* be used with an xslt stylesheet.

  text.gsub!(/<%(.*?)%>/m, "#{END_DELIM}; \\1; #{buffer} << #{START_DELIM}")
  #text.gsub!(/%>/, "; #{buffer} << #{START_DELIM}")
  #text.gsub!(/<%/, "#{END_DELIM}; ")

  # Alterative versions of interpolation.
  # (very useful in xsl stylesheets)
  # Example: #\my_val\
  
  text.gsub!(/\#\\(.*?)\\/, '#{\1}')

  # Alternative for entities.
  # (useful in xsl stylesheets)
  # Examples: %nbsp;, %rquo;

  text.gsub!(/%(#\d+|\w+);/, '&\1;')

  # Compile time ruby code. This code is evaluated when
  # compiling the template and the result injected directly
  # into the result. Usefull for example to prevaluate
  # localization. Just use the #[] marker instead of #{}.
  
  text.gsub!(/\#\[(.*?)\]/) do |match|
    eval($1)
  end

  text =  "#{buffer} << #{START_DELIM}#{text}#{END_DELIM}"

  return text
end

#evaluate_template(ruby, the_binding = nil) ⇒ Object

Evaluate the template.

ruby

A String containing the compiled template code.

binding

The evaluation binding for the rendering.



123
124
125
# File 'lib/nitro/template.rb', line 123

def evaluate_template(ruby, the_binding = nil)
  eval(ruby, the_binding)
end

#process_template(template, buffer = 'out', the_binding = nil) ⇒ Object

Compile and render the template.



129
130
131
# File 'lib/nitro/template.rb', line 129

def process_template(template, buffer = 'out', the_binding = nil)
  evaluate_template(compile_template(template, buffer), the_binding)
end