Module: Crystal::Template

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

Constant Summary collapse

DIRECTORY_NAME =
"views"

Class Method Summary collapse

Class Method Details

.basic_render(options, &block) ⇒ Object



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
# File 'lib/crystal/template/template.rb', line 70

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, scope.current_dir
        # p [tname, calculate_prefixes(options), scope.format, 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)


40
41
42
43
# File 'lib/crystal/template/template.rb', line 40

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

.parse_arguments(*args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/crystal/template/template.rb', line 59

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

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



45
46
47
48
49
50
# File 'lib/crystal/template/template.rb', line 45

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

.render(*args, &block) ⇒ Object



12
13
14
15
# File 'lib/crystal/template/template.rb', line 12

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/crystal/template/template.rb', line 21

def render_layout *args
  options = parse_arguments(*args)
  options = options.stringify_keys!
  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



52
53
54
55
56
57
# File 'lib/crystal/template/template.rb', line 52

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