Class: Nitro::StaticInclude

Inherits:
Object
  • Object
show all
Defined in:
lib/nitro/compiler/include.rb

Overview

Performs static includes. Typically you should include this compiler as the first stage of the compile pipeline.

This compiler is extremely helpful, typically you would want to use static includes in many many cases.

Class Method Summary collapse

Class Method Details

.transform(text, compiler) ⇒ Object

Statically include sub-template files. The target file is included at compile time. If the given path is relative, the template_root stack of the controller is traversed. If an absolute path is provided, templates are searched only in Template.root

gmosx: must be xformed before the <?r pi.

Example

<?include href=“root/myfile.sx” ?>



21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/nitro/compiler/include.rb', line 21

def self.transform(text, compiler)
  controller = compiler.controller
  
  return text.gsub(/<\?include href=["|'](.*?)["|'](.*)\?>/) do |match|
    found = false

    if $1[0] == ?/
      # Absolute path, search only in the application template
      # root.

      href = $1[1, 999999] # hack!!
      template_stack = [ Glue::Template.root ]
    else
      # Relative path, traverse the template_root stack.
      
      href = $1
      template_stack = controller.instance_variable_get(:@template_root)
    end
      
    for template_root in template_stack
      if File.exist?(filename = "#{template_root}/#{href}")
        found = true
        break
      end

      if File.exist?(filename = "#{template_root}/#{href}.xinc")
        found = true
        break
      end

      if File.exist?(filename = "#{template_root}/#{href}.xhtml")
        found = true
        break
      end
    end
    
    unless found
      raise "Cannot statically include '#{href}'"
    end      

    itext = File.read(filename)
    itext.gsub!(/<\?xml.*\?>/, '')
    itext.gsub!(/<\/?root(.*?)>/m, ' ');
    itext
  end
end