Class: Rad::Template

Inherits:
Object show all
Defined in:
lib/rad/template/_require.rb,
lib/rad/template/_template.rb

Defined Under Namespace

Classes: Context, RelativePathResolver

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTemplate

Returns a new instance of Template.



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

def initialize
  @relative_path_resolver = RelativePathResolver.new
end

Instance Attribute Details

#prefixesObject

Returns the value of attribute prefixes.



4
5
6
# File 'lib/rad/template/_template.rb', line 4

def prefixes
  @prefixes
end

#relative_path_resolverObject

Returns the value of attribute relative_path_resolver.



4
5
6
# File 'lib/rad/template/_template.rb', line 4

def relative_path_resolver
  @relative_path_resolver
end

Instance Method Details

#basic_render(options, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rad/template/_template.rb', line 25

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

    with_scope options, context do |scope|
      file = (
        options[:file] ||
        find_template(options[:template], options.merge(scope)) ||
        (options[:if_not_exist] && find_template(options[:if_not_exist], options.merge(scope))) ||
        raise("no template '#{options[:template]}'!")
      )

      scope[:current_dir] = dirname(file)

      template = create_tilt_template file

      result = with_template context, template do
        render_template template, context, options, &context.content_block
      end

      return result, context
    end
  end
end

#directory_nameObject



80
# File 'lib/rad/template/_template.rb', line 80

def directory_name; "" end

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

Returns:

  • (Boolean)


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

def exist? tname, options = {}
  !!find_template(tname, options)
end

#find_file(tname, prefixes, format, exact_format, directories) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rad/template/_template.rb', line 61

def find_file tname, prefixes, format, exact_format, directories
  prefixes = prefixes || self.prefixes
  prefixes.each do |prefix|
    tname = template_name_with_prefix("#{directory_name}#{tname}", prefix)
    file = if tname.include? '.'
      _find_file(tname, directories) || _find_file("#{tname}.*", directories)
    else
      if format
        _find_file("#{tname}.#{format}.*", directories) or
          _find_file("#{tname}.*", directories, exact_format)
      else
        _find_file("#{tname}.*", directories)
      end
    end
    return file if file
  end
  return nil
end

#parse_arguments(*args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/rad/template/_template.rb', line 50

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

  options
end

#pathsObject



82
# File 'lib/rad/template/_template.rb', line 82

def paths; @paths ||= [] end

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



20
21
22
23
# File 'lib/rad/template/_template.rb', line 20

def read tname, options = {}
  file = find_template(tname, options) || raise("no template '#{tname}'!")
  File.read file
end

#render(*args, &block) ⇒ Object



11
12
13
14
# File 'lib/rad/template/_template.rb', line 11

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