Class: Amrita2::Core::Template

Inherits:
Object
  • Object
show all
Includes:
Amrita2, Amrita2::CompileTimeContext, Filters, Runtime, Util, OptionSupport
Defined in:
lib/amrita2/template.rb,
lib/amrita2/gettext.rb

Overview

using Amrita2::Template

require "amrita2/template"
include Amrita2
tmpl = Template.new <<-END
  <<html<
    <<body<
      <<h1 :title>>
      <<p :body<
        <<:template>> is a html template libraly for <<:lang>>
END

puts tmpl.render_with(:title=>"hello world", :body=>{ :template=>"Amrita2", :lang=>"Ruby" })

Defined Under Namespace

Classes: Tracer

Constant Summary

Constants included from Util

Util::AMP_WITHOUT_REFRENCE, Util::DefaultAllowedScheme, Util::NAME, Util::NAMECHAR, Util::NOT_REFERENCE, Util::UrlInvalidChar

Constants included from Amrita2

FilterMethods, Hooki, SanitizedString

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Runtime

#amrita_get_context_value, #amrita_set_context_value, #end_tag, #new_element, #start_tag

Methods included from Util

sanitize_attribute_value, sanitize_text, sanitize_url

Constructor Details

#initialize(text, *opts, &block) ⇒ Template

Initialize Template object using text and opts. Optionaly specify block for setting filters.

t = Amrita2::Template.new(text) do |e, src, filters|
  if src == "aaa"
    filters << Amrita2::Filters::Default[456]
  end
end


1141
1142
1143
1144
1145
1146
1147
1148
1149
# File 'lib/amrita2/template.rb', line 1141

def initialize(text, *opts, &block)
  parse_opt(*opts)
  @text = text.dup
  @setuped = false
  @filter_proc = block
  @text_domain = nil
  @inline_ruby = true
  @amrita_prefix = @opt[:amrita_prefix] || "am:"
end

Instance Attribute Details

#amrita_prefixObject

Specify attribute prefix for dynamic element. Defaults to ‘am:’.



1125
1126
1127
# File 'lib/amrita2/template.rb', line 1125

def amrita_prefix
  @amrita_prefix
end

#inline_rubyObject

Specify weather use inline ruby. Defaults to true



1127
1128
1129
# File 'lib/amrita2/template.rb', line 1127

def inline_ruby
  @inline_ruby
end

#rootObject (readonly)

Returns the value of attribute root.



1130
1131
1132
# File 'lib/amrita2/template.rb', line 1130

def root
  @root
end

#text_domainObject

Returns the value of attribute text_domain.



46
47
48
# File 'lib/amrita2/gettext.rb', line 46

def text_domain
  @text_domain
end

#tracerObject (readonly)

Returns the value of attribute tracer.



1129
1130
1131
# File 'lib/amrita2/template.rb', line 1129

def tracer
  @tracer
end

Instance Method Details

#amrita_filter=(v) ⇒ Object



1155
1156
1157
# File 'lib/amrita2/template.rb', line 1155

def amrita_filter=(v)
  @opt[:amrita_filter] = v
end

#amrita_src=(v) ⇒ Object



1151
1152
1153
# File 'lib/amrita2/template.rb', line 1151

def amrita_src=(v)
  @opt[:amrita_src] = v
end

#compile_for_gettextObject



47
48
49
# File 'lib/amrita2/gettext.rb', line 47

def compile_for_gettext
  setup
end

#compiletime_binding=(b) ⇒ Object



1159
1160
1161
# File 'lib/amrita2/template.rb', line 1159

def compiletime_binding=(b)
  @opt[:compiletime_binding] = b
end

#get_erb_source_for_gettextObject



51
52
53
54
# File 'lib/amrita2/gettext.rb', line 51

def get_erb_source_for_gettext
  setup
  @root.get_erb_source
end

#render_with(value, binding_ = nil) ⇒ Object

render template with value and binding. use TOPLEVEL_BINDING if binding is nil.



1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
# File 'lib/amrita2/template.rb', line 1201

def render_with(value, binding_=nil)
  setup unless @setuped
  unless binding_
    if value.kind_of?(Binding)
      binding_ = value
    else
      binding_ = binding
    end
  end

  ret = nil

  if @opt[:process_xhtml]
    ret = ""
    ret << @root.xml_decl.to_s << "\n" if @root.xml_decl
    ret << @root.doctype.to_s << "\n"  if @root.doctype
    ret << @cls.new.render_with(value, binding_)
  else
    ret = @cls.new.render_with(value, binding_)
  end
  SanitizedString[ret]
end

#set_trace(io_or_type, &block) ⇒ Object

Print compiled ruby code and runtime trace to io_or_type

tmpl = Template.new "...."
tmpl.set_trace(STDOUT)
puts tmpl.render_with(...)


1170
1171
1172
# File 'lib/amrita2/template.rb', line 1170

def set_trace(io_or_type, &block)
  @tracer = Tracer.new(io_or_type, &block)
end

#setupObject



1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
# File 'lib/amrita2/template.rb', line 1174

def setup
  setup_opt
  @preprocessor = PreProcessor.new(@opt)
  cg = CodeGenerator.new
  preprocessed_text = @preprocessor.process(@text)

  if bc = @preprocessor.sections[:BeforeCompile]
    self.instance_eval do
      eval bc
    end
  end

  filter_setup do |e, src, filters|
    filters.unshift Trace.new if @tracer and @tracer.auto_trace?
    filters.unshift InlineRuby.new if @inline_ruby
  end

  compile(cg, preprocessed_text)
  @setuped = true
  cg.result
ensure
  @tracer.code(cg) if @tracer and @tracer.code_trace?
end