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
- START_DELIM =
Set some pretty safe delimiters for templates.
"<<NITRO_TEMPLATE_DELIMETER\n"
- END_DELIM =
"\nNITRO_TEMPLATE_DELIMETER\n"
Instance Method Summary collapse
-
#compile_template(template, buffer = '@out', base_dir = File.expand_path(Template.root)) ⇒ Object
Convert a template to actual Ruby code, ready to be evaluated.
-
#evaluate_template(ruby, the_binding = nil) ⇒ Object
Evaluate the template.
-
#process_template(template, buffer = 'out', the_binding = nil) ⇒ Object
Compile and render the template.
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.
33 34 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 |
# File 'lib/nitro/template.rb', line 33 def compile_template(template, buffer = '@out', base_dir = File.(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 href=["'](.*?)["'](.*)(.?)\/>/) do |match| "<?r File.read( '\#{@dispatcher.root}/#$1' ?>" end # xform render/inject instructions <render href="xxx" /> # must be transformed before the processinc instructions. text.gsub!(/<(render|inject) href=["'](.*?)["'](.*)(.?)\/>/) do |match| "<?r render '#$2' ?>" end # 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!(/\?>/, "; #{buffer} << #{START_DELIM}") text.gsub!(/<\?r(\s?)/, "#{END_DELIM}; ") # Transform alternative code tags. # (very useful in xsl stylesheets) 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!(/%>/, "; #{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.
122 123 124 |
# File 'lib/nitro/template.rb', line 122 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.
128 129 130 |
# File 'lib/nitro/template.rb', line 128 def process_template(template, buffer = 'out', the_binding = nil) evaluate_template(compile_template(template, buffer), the_binding) end |