Module: Ronin::Templates::Template

Includes:
DataPaths::Finders
Included in:
Erb
Defined in:
lib/ronin/templates/template.rb

Overview

The Template module allows a class to find templates and manage multiple template directories. The Template module also can find templates within data/ directories using DataPaths::Finders.

Instance Method Summary collapse

Instance Method Details

#enter_template(sub_path) {|path| ... } ⇒ Object (protected)

Finds the template, pushing the directory that the template resides within to #template_dirs, calls the given block and then pops the directory off of #template_dirs.

Examples:

enter_template('sub/path/template.erb') do |path|
  # do stuff with the full path
end

Parameters:

  • sub_path (String)

    The relative path of the template to find.

Yields:

  • (path)

    The block to be called after the directory of the template has been pushed onto #template_dirs. After the block has returned, the directory will be popped off of #template_dirs.

Yield Parameters:

  • path (String)

    The absolute path of the template.

Returns:

  • (Object)

    Result of the given block.

Raises:

  • (RuntimeError)

    The template was not found.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ronin/templates/template.rb', line 118

def enter_template(sub_path)
  sub_path = sub_path.to_s

  unless (path = find_template(sub_path))
    raise(RuntimeError,"could not find template #{sub_path.dump}")
  end

  template_dirs.unshift(File.dirname(path))

  result = yield(path)

  template_dirs.shift
  return result
end

#find_template(sub_path) ⇒ String? (protected)

Finds the template within the #template_dir or uses DataPaths::Finders#find_data_file to search through all data/ directories for the template.

Examples:

find_template 'sub/path/template.erb'

Parameters:

  • sub_path (String)

    The relative path of the template to find.

Returns:

  • (String, nil)

    Returns the absolute path to the template, or nil if the template could not be found.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ronin/templates/template.rb', line 77

def find_template(sub_path)
  sub_path = sub_path.to_s

  if template_dir
    path = File.expand_path(File.join(template_dir,sub_path))

    return path if File.file?(path)
  end

  return find_data_file(sub_path)
end

#read_template(template_path) {|template| ... } ⇒ Object (protected)

Finds and reads the contents of a template.

Examples:

read_template 'path/to/_include.txt'
# => "..."

Calling read_template with a block

read_template 'path/to/_include.txt' do |contents|
  # ...
end

Parameters:

  • template_path (String)

    The relative path to the template.

Yields:

  • (template)

    The given block will receive the contents of the template.

Yield Parameters:

  • template (String)

    The contents of the template.



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ronin/templates/template.rb', line 156

def read_template(template_path)
  enter_template(template_path) do |path|
    contents = File.read(path)

    if block_given?
      yield(contents)
    else
      contents
    end
  end
end

#template_dirString? (protected)

The first path in #template_dirs, that will be used to search for other templates in.

Returns:



56
57
58
# File 'lib/ronin/templates/template.rb', line 56

def template_dir
  template_dirs.first
end

#template_dirsArray (protected)

A stack of directories to search for other templates within.

Returns:

  • (Array)

    The stack of directory paths.



42
43
44
# File 'lib/ronin/templates/template.rb', line 42

def template_dirs
  @template_dirs ||= []
end