Class: Raw::TemplateFilter
- Inherits:
-
Object
- Object
- Raw::TemplateFilter
- Defined in:
- lib/raw/compiler/filter/template.rb
Overview
The basic Nitro Template filter.
Constant Summary collapse
- START_DELIM =
Set some pretty safe delimiters for templates.
"%{"
- END_DELIM =
"}\n"
Instance Method Summary collapse
-
#apply(source, buffer = "@out") ⇒ Object
Convert a template to actual Ruby code, ready to be evaluated.
Instance Method Details
#apply(source, buffer = "@out") ⇒ Object
Convert a template to actual Ruby code, ready to be evaluated.
source
-
The template source 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.
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 113 114 115 116 |
# File 'lib/raw/compiler/filter/template.rb', line 37 def apply(source, buffer = "@out") source = source.dup # Strip the xml header! (interracts with the following gsub!) source.gsub!(/<\?xml.*\?>/, "") # Transform include instructions <include href="xxx" /> # must be transformed before the processing instructions. # Useful to include fragments cached on disk #-- # gmosx, FIXME: NOT TESTED! test and add caching. # add load_statically_included fixes. #++ source.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. source.gsub!(/<(?:render|inject)\s+href=["'](.*?)["']\s+\/>/, %[<?r render "\\1" ?>]) # Transform the processing instructions, use <?r as a marker. source.gsub!(/<\?r\s+(.*?)\s+\?>/m) do |code| "#{END_DELIM}#{$1.squeeze(' ').chomp}\n#{buffer} << #{START_DELIM}" end # Transform alternative code tags (very useful in xsl # stylesheets). source.gsub!(/<ruby>(.*?)<\/ruby>/m) do |code| "#{END_DELIM}#{$1.squeeze(' ').chomp}\n#{buffer} << #{START_DELIM}" end # Also handle erb/asp/jsp style tags. Those tags *cannot* # be used with an xslt stylesheet. # # Example: # # <% 10.times do %> # Hello<br /> # <% end %> source.gsub!(/<%(.*?)%>/m) do |code| "#{END_DELIM}#{$1.squeeze(' ').chomp}\n#{buffer} << #{START_DELIM}" end # Alterative versions of interpolation (very useful in xsl # stylesheets). # # Example: # # Here is #\my_val\ source.gsub!(/\#\\(.*?)\\/, '#{\1}') # Alternative for entities (useful in xsl stylesheets). # # Examples: # # %nbsp;, %rquo; source.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 #{}. # # Example: # # This script was compiled at #[Time.now] source.gsub!(/\#\[(.*?)\]/) do |match| eval($1) end return "#{buffer} << #{START_DELIM}#{source}#{END_DELIM}" end |