Class: Veewee::Templates

Inherits:
Object show all
Defined in:
lib/veewee/templates.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Templates

Returns a new instance of Templates


8
9
10
11
# File 'lib/veewee/templates.rb', line 8

def initialize(env)
  @env = env
  return self
end

Instance Attribute Details

#envObject

Returns the value of attribute env


6
7
8
# File 'lib/veewee/templates.rb', line 6

def env
  @env
end

Instance Method Details

#[](name) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/veewee/templates.rb', line 13

def [](name)
  result = nil
  valid_paths(env.template_path).each do |template_dir|
    template = Veewee::Template.new(name, File.join(template_dir, name), @env)
    if template.exists?
      result = template
      return result
    end
  end
  return nil
end

#each(&block) ⇒ Object

Fetch all Templates


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/veewee/templates.rb', line 26

def each(&block)
  templates = Hash.new

  valid_paths(env.template_path).each do |template_dir|

    env.logger.debug("[Template] Searching #{template_dir} for templates")

    subdirs = Dir.glob("#{template_dir}/*")
    subdirs.each do |sub|
      if File.directory?("#{sub}")
        name = sub.sub(/#{template_dir}\//, '')
        template = Veewee::Template.new(name, sub, @env)
        if template.exists?
          env.logger.debug("[Template] template '#{name}' found")
          templates[name] = template
        end
      end
    end
  end

  if templates.length == 0
    env.logger.debug("[Template] no templates found")
  end

  Hash[templates.sort].each(&block)
end