Module: Texas::Template::Helper::Base
- Included in:
- Runner::Base
- Defined in:
- lib/texas/template/helper/base.rb
Overview
Basic helper methods for finding files and template handling
Instance Method Summary collapse
- #default_search_paths ⇒ Object
-
#find_template_file(parts, possible_exts = [], possible_paths = default_search_paths) ⇒ Object
Searches for the given file in
possible_paths
, also checking forpossible_exts
as extensions. -
#find_template_file!(parts, possible_exts = [], possible_paths = default_search_paths) ⇒ Object
Searches for the given file and raises an error if it is not found anywhere.
-
#partial(name, locals = {}) ⇒ Object
Renders a partial with the given locals.
-
#path_with_templates_basename ⇒ Object
Returns a subdir with the current template’s basename.
-
#render(options, locals = {}) ⇒ Object
Renders a template with the given locals.
- #render_as_array(options) ⇒ Object
-
#template_extensions ⇒ Object
Returns all extensions the Template::Runner can handle.
-
#templates_by_glob(glob = "*") ⇒ Object
Returns all templates in the current template’s path matching the given glob.
Instance Method Details
#default_search_paths ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/texas/template/helper/base.rb', line 10 def default_search_paths [ __path__, path_with_templates_basename, build_path, build.root ].compact end |
#find_template_file(parts, possible_exts = [], possible_paths = default_search_paths) ⇒ Object
Searches for the given file in possible_paths
, also checking for possible_exts
as extensions
Example:
find_template_file(["figures", "titel"], [:pdf, :png])
# => will check
figures/titel.pdf
figures/titel.png
tmp/figures/titel.pdf
tmp/figures/titel.png
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/texas/template/helper/base.rb', line 41 def find_template_file(parts, possible_exts = [], possible_paths = default_search_paths) possible_paths.each do |base| ([""] + possible_exts).each do |ext| path = parts.clone.map(&:to_s).map(&:dup) path.unshift base.to_s path.last << ".#{ext}" unless ext.empty? filename = File.join(*path) return filename if File.exist?(filename) && !File.directory?(filename) end end nil end |
#find_template_file!(parts, possible_exts = [], possible_paths = default_search_paths) ⇒ Object
Searches for the given file and raises an error if it is not found anywhere
56 57 58 59 60 61 62 |
# File 'lib/texas/template/helper/base.rb', line 56 def find_template_file!(parts, possible_exts = [], possible_paths = default_search_paths) if filename = find_template_file(parts, possible_exts, possible_paths) filename else raise TemplateError.new(self, "File doesnot exists anywhere: #{parts.size > 1 ? parts : parts.first}") end end |
#partial(name, locals = {}) ⇒ Object
Renders a partial with the given locals.
66 67 68 |
# File 'lib/texas/template/helper/base.rb', line 66 def partial(name, locals = {}) render("_#{name}", locals) end |
#path_with_templates_basename ⇒ Object
Returns a subdir with the current template’s basename
Example: In /example/introduction.tex.erb this method returns “/example/introduction” if that directory exists and nil if it doesn’t.
26 27 28 29 |
# File 'lib/texas/template/helper/base.rb', line 26 def path_with_templates_basename subdir = Template.basename @output_filename File.directory?(subdir) ? subdir : nil end |
#render(options, locals = {}) ⇒ Object
Renders a template with the given locals.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/texas/template/helper/base.rb', line 72 def render(, locals = {}) if [String, Symbol].include?(.class) = {:templates => []} end if name = [:template] [:templates] = [name] end if glob = [:glob] [:templates] = templates_by_glob(glob) end [:locals] = locals unless locals.empty? render_as_array().join([:join].to_s) end |
#render_as_array(options) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/texas/template/helper/base.rb', line 86 def render_as_array() [:templates].map do |name| template_file = find_template_file!([name], template_extensions) Texas::Template.create(template_file, build).__run__([:locals]) end end |
#template_extensions ⇒ Object
Returns all extensions the Template::Runner can handle.
95 96 97 |
# File 'lib/texas/template/helper/base.rb', line 95 def template_extensions Texas::Template.known_extensions end |
#templates_by_glob(glob = "*") ⇒ Object
Returns all templates in the current template’s path matching the given glob
101 102 103 104 105 106 107 |
# File 'lib/texas/template/helper/base.rb', line 101 def templates_by_glob(glob = "*") files = Dir[File.join(__path__, glob)] templates = files.map do |f| Texas::Template.basename(f).gsub(__path__, '') end templates.uniq.sort end |