Class Amrita::Template
In: lib/amrita/template.rb
Parent: Object
Methods
[]    _dump    accel_mode    compiler_mode    create_amulet    define_amulet    define_amulet_all    do_expand    expand    extract_amulet    get_ivars_for_dump    get_opts_for_save    get_parser_class    need_update?    new    pureruby_mode    set_ivars_from    set_opts_from    setup    setup_amulet_seed    setup_compiler    setup_ep    setup_template    setup_vm    source_mtime   
Attributes
:amrita_id  [RW] 

The name of attribute that will be used for template expandion by amrita. You will need to set this if you use id attribute fom DOM. For expample, if this was set to "amrita_id", you can use amrita_id for amrita and id for DOM.

:amulet_attr  [RW] 
:amulet_seeds  [RW] 
:asxml  [RW] 

If set, the output is an xhtml document.

:compiler  [R] 
:debug_compiler  [RW] 

debug compiler

:delete_id  [RW] 

delete id attribute of output if set. Default is true

:delete_id_on_copy  [RW] 

delete id attribute of output even if the elements are copied. Default is true

:ep  [R] 
:escaped_id  [RW] 

The name of attribute that turns into id. You will need to set this if you use id attribute for DOM/CSS/etc... For expample, if this was set to "__id", you can use id for amrita and __id for DOM/CSS/etc....

:iset  [R] 
:lazy_evaluation  [RW] 
:optimize_bytecode  [RW] 
:partial_compile  [RW] 
:src  [R] 

The source code that generated by template compiler

:tag_info  [RW] 
:template  [R] 
:use_accelerater  [RW] 
:use_compiler  [RW] 

If Set, use compiler.

:use_simplespan  [RW] 
:vm  [R] 
:xml  [RW] 

If set, use REXML-based parser instead of Amrita's own html-parser

Included modules
Amrita
Public Class methods
new()
# File lib/amrita/template.rb, line 91
    def initialize
      @template = @iset = @ep = @compiler = @vm = nil
      @xml = @asxml = false
      @delete_id = @delete_id_on_copy = true
      @use_simplespan = true
      @escaped_id = @amrita_id = nil
      @debug_compiler = false
      @tag_info = DefaultHtmlTagInfo
      @amulet_seeds = {}
      if Amrita::accelerator_loaded?
        accel_mode
      else
        pureruby_mode
      end
    end
Public Instance methods
accel_mode()
# File lib/amrita/template.rb, line 107
    def accel_mode
      @use_compiler =  false
      @partial_compile = true
      @lazy_evaluation = true
      @optimize_bytecode = false
    end
pureruby_mode()
# File lib/amrita/template.rb, line 114
    def pureruby_mode
      @use_compiler =  true
      @partial_compile = true
      @lazy_evaluation = true
      @optimize_bytecode = false
    end
compiler_mode()
# File lib/amrita/template.rb, line 121
    def compiler_mode
      @use_compiler =  true
      @partial_compile = false
      @lazy_evaluation = false
      @optimize_bytecode = true
    end
expand(stream, model)
  1. load template if it was changed
  2. compile template if use_compiler was set.
  3. expand template with model
  4. print template to stream
# File lib/amrita/template.rb, line 137
    def expand(stream, model)
      setup
      do_expand(stream, model)
    end
setup()
# File lib/amrita/template.rb, line 142
    def setup
      setup_ep unless @ep
      setup_template if need_update?
      setup_vm
    end
do_expand(stream, model)
# File lib/amrita/template.rb, line 148
    def do_expand(stream, model)
      @vm.out = stream
      @vm.register = model
      @vm.go
      @vm.out
    end
setup_ep()
# File lib/amrita/template.rb, line 155
    def setup_ep
      @ep = ElementProcessor.new(@tag_info)
      @ep.amrita_id = amrita_id
      @ep.escaped_id = escaped_id
      @ep.delete_id = delete_id
      @ep.delete_id_on_copy = delete_id_on_copy
    end
get_parser_class()
# File lib/amrita/template.rb, line 163
    def get_parser_class
      if @xml
        require 'amrita/xml'
        Amrita::XMLParser
      else
        Amrita::HtmlParser
      end
    end
setup_compiler()
# File lib/amrita/template.rb, line 172
    def setup_compiler
      setup_ep unless @ep
      @compiler = Compiler.new(ep)
      @compiler.use_simplespan = use_simplespan
    end
setup_template()
# File lib/amrita/template.rb, line 178
    def setup_template
      @template = load_template
      setup_compiler unless @compiler
      @iset = compiler.compile(@template)
    end
setup_vm()
# File lib/amrita/template.rb, line 184
    def setup_vm
      unless @vm
        @vm = VirtualMachine.new(@ep)
        @vm.use_compiler = use_compiler
        @vm.debug = debug_compiler
        @vm.lazy_evaluation = lazy_evaluation
        @vm.partial_compile = partial_compile
        @vm.optimize_bytecode = optimize_bytecode
      end
      @vm.load_bytecode(@iset)
      @vm.dump_bytecode if @debug_compiler
    end
source_mtime()
# File lib/amrita/template.rb, line 197
    def source_mtime
      nil
    end
need_update?()
# File lib/amrita/template.rb, line 202
    def need_update?
      not @template 
    end
define_amulet_all()
# File lib/amrita/template.rb, line 206
    def define_amulet_all
      @template = load_template
      ids = @template.children.collect do |e|
        e.amrita_id.intern if e.amrita_id
      end.compact
      define_amulet(*ids)
    end
define_amulet(*args)
# File lib/amrita/template.rb, line 214
    def define_amulet(*args)
      @amulet_attr = nil unless defined? @amulet_attr
      h = nil
      if args[-1].kind_of?(Hash)
        h = args.pop
      else
        h = {}
      end

      args.each do |x|
        case x
        when Hash
          x.each do |k, v|
            h[k] = v
          end
        when String, Symbol
          h[x] = {}
        else
          raise "illeagal param for declar_amulet"
        end
      end
      
      h.each do |k, v|
        case v
        when Class
          h[k] = { :amulet_class=>v }
        when Hash
        else
          raise "illeagal parameter #{v.inspect}"
        end
      end
      setup_amulet_seed(h)
    end
extract_amulet(x, h)
# File lib/amrita/template.rb, line 248
    def extract_amulet(x, h)
      case x
      when Element
        aid = x.amrita_id
        aid = x[amulet_attr.intern] if amulet_attr
        if aid
          aid = aid.intern
          spec = h[aid]
          if spec
            h.delete(aid)
            x.hide_amrita_id!
            seedklass = spec[:seedclass] || AmuletSeed
            @amulet_seeds[aid] = seedklass.new(self, x, spec)
            spec[:placeholder] or e(:span, :id=>aid)
          else
            x.apply_to_children do |xx|
              extract_amulet(xx, h)
            end
          end
        else
          x.apply_to_children do |xx|
            extract_amulet(xx, h)
          end
        end
      else
        x.apply_to_children do |xx|
          extract_amulet(xx, h)
        end
      end
    end
setup_amulet_seed(h)
# File lib/amrita/template.rb, line 279
    def setup_amulet_seed(h)
      setup_compiler
      @template = load_template
      # wrap with dummy span to make enable extracting top node
      t = e(:span) { @template }.apply_to_children do |x|
        extract_amulet(x, h)
      end
      @template = t.body
      raise "unknown id #{h.to_a[0][0]}" if h.size > 9
      @iset = compiler.compile(@template)
    end
create_amulet(aid, *args, &block)
# File lib/amrita/template.rb, line 291
    def create_amulet(aid, *args, &block)
      seed = @amulet_seeds[aid]
      raise "Amulet #{aid} was  not declared" unless seed
      seed.create_amulet(*args, &block)
    end
[](*args)
# File lib/amrita/template.rb, line 297
    def [](*args)
      aid = args.shift
      if args.size == 0
        @amulet_seeds[aid]
      else
        @amulet_seeds[aid][*args]
      end
    end
get_opts_for_save()
# File lib/amrita/template.rb, line 306
    def get_opts_for_save
      {
        :xml => @xml,
        :asxml => @asxml,
        :delete_id => @delete_id ,
        :delete_id_on_copy => @delete_id_on_copy,
        :escaped_id => @escaped_id,
        :amrita_id => @amrita_id,
        :use_compiler => @use_compiler,
        :lazy_evaluation => @lazy_evaluation,
        :optimize_bytecode => @optimize_bytecode,
        :use_simplespan => @use_simplespan,
        :debug_compiler => @debug_compiler
      }
    end
set_opts_from(opts)
# File lib/amrita/template.rb, line 322
    def set_opts_from(opts)
      @xml = opts[:xml]
      @asxml = opts[:asxml]
      @delete_id = opts[:delete_id]
      @delete_id_on_copy = opts[:delete_id_on_copy]
      @escaped_id = opts[:escaped_id]
      @amrita_id = opts[:amrita_id]
      @use_compiler = opts[:use_compiler]
      @lazy_evaluation = opts[:lazy_evaluation]
      @optimize_bytecode = opts[:optimize_bytecode]
      @use_simplespan = opts[:use_simplespan]
      @debug_compiler = opts[:debug_compiler]
    end
get_ivars_for_dump()
# File lib/amrita/template.rb, line 336
    def get_ivars_for_dump
      [@template, @iset, @amulet_seeds, get_opts_for_save]
    end
set_ivars_from(a)
# File lib/amrita/template.rb, line 340
    def set_ivars_from(a)
      @template, @iset, @amulet_seeds, opts = *a
      set_opts_from(opts)
    end
_dump(level)
# File lib/amrita/template.rb, line 345
    def _dump(level)
      Marshal::dump(get_ivars_for_dump, level)
    end