Module: Rad::Template

Defined in:
lib/rad/template/template.rb

Constant Summary collapse

DIRECTORY_NAME =

DIRECTORY_NAME = “/views”

""

Class Method Summary collapse

Class Method Details

.basic_render(options, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rad/template/template.rb', line 72

def basic_render options, &block        
  options = options.clone.to_openobject
  
  with_context options do |context|        
    context.content_block ||= block            

    with_scope options, context do |scope|              
      unless file = options.file                               
        file = find_template options.template!, calculate_prefixes(options), scope.format, exact_format?(options), scope.current_dir
        raise "No template '#{options.template!}'!" unless file
      end
      
      scope.current_dir = dirname(file)
      
      template = create_tilt_template file
      
      result = with_template context, template do
        render_template template, options, &context.content_block
      end
      
      return result, context
    end            
  end
end

.exist?(tname, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/rad/template/template.rb', line 42

def exist? tname, options = {}
  options = options.to_openobject
  !!find_template(tname, calculate_prefixes(options), options.format, exact_format?(options), options.current_dir)
end

.parse_arguments(*args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/rad/template/template.rb', line 61

def parse_arguments *args
  options = args.extract_options!.to_openobject
  if args.size == 1
    options.template = args.first
  else 
    raise "Invalid input" if args.size != 0
  end     
  
  options
end

.pathsObject



7
# File 'lib/rad/template/template.rb', line 7

def paths; @paths ||= [] end

.read(tname, options = {}) ⇒ Object



47
48
49
50
51
52
# File 'lib/rad/template/template.rb', line 47

def read tname, options = {}
  options = options.to_openobject
  file = find_template tname, calculate_prefixes(options), options.format, exact_format?(options), options.current_dir
  raise "No template '#{tname}'!" unless file
  File.read file
end

.render(*args, &block) ⇒ Object



15
16
17
18
# File 'lib/rad/template/template.rb', line 15

def render *args, &block
  result, context = basic_render(parse_arguments(*args), &block)
  result
end

.render_layout(*args) ⇒ Object

def render_and_return_context *args, &block

basic_render(*parse_arguments(*args), &block)

end



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rad/template/template.rb', line 24

def render_layout *args
  options = parse_arguments(*args)        
  options.must.include :content
  options.must.include :context
  
  result, context = basic_render options do |*args|
    if args.empty?
      options.content
    else
      args.size.must_be == 1
      variable_name = args.first.to_s
      self.context.content_variables[variable_name]
    end
  end
  
  result
end

.template_name_with_prefix(tname, prefix) ⇒ Object



54
55
56
57
58
59
# File 'lib/rad/template/template.rb', line 54

def template_name_with_prefix tname, prefix      
  index = tname.rindex('/')
  index = index ? index + 1 : 0
  tname = tname.clone
  tname.insert index, prefix
end