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
- #filename_for_find(parts, base, ext = nil) ⇒ 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 one or more templates 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.uniq end |
#filename_for_find(parts, base, ext = nil) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/texas/template/helper/base.rb', line 79 def filename_for_find(parts, base, ext = nil) path = [parts].flatten.map(&:to_s).map(&:dup) path.unshift base.to_s path.last << ".#{ext}" unless ext.empty? File.join(*path) 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", "some-chart"], [:pdf, :png], ["", "tmp", "tmp/build"])
# => will check
figures/some-chart.pdf
figures/some-chart.png
tmp/figures/some-chart.pdf
tmp/figures/some-chart.png
tmp/build/figures/some-chart.pdf
tmp/build/figures/some-chart.png
59 60 61 62 63 64 65 66 67 |
# File 'lib/texas/template/helper/base.rb', line 59 def find_template_file(parts, possible_exts = [], possible_paths = default_search_paths) possible_paths.each do |base| (possible_exts + [""]).each do |ext| filename = filename_for_find(parts, base, ext) 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
71 72 73 74 75 76 77 |
# File 'lib/texas/template/helper/base.rb', line 71 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 doesn't exists anywhere: #{parts.size > 1 ? parts : parts.first}") end end |
#partial(name, locals = {}) ⇒ Object
Renders a partial with the given locals.
Example:
<%= partial :some_partial, :some_value => 42 %>
91 92 93 |
# File 'lib/texas/template/helper/base.rb', line 91 def partial(name, locals = {}) render("_#{name}", locals) end |
#path_with_templates_basename ⇒ Object
Returns a subdir with the current template’s basename
Example:
# Given the following contents directory:
#
# contents/
# section-1/
# subsection-1-1.tex.erb
# contents.tex.erb
# section-1.tex-erb
# section-2.tex-erb
#
# section-1.tex.erb
<%= path_with_templates_basename %>
# => "section-1"
# section-2.tex.erb
<%= path_with_templates_basename %>
# => nil
42 43 44 45 |
# File 'lib/texas/template/helper/base.rb', line 42 def path_with_templates_basename subdir = Template.basename @output_filename File.directory?(subdir) ? subdir : nil end |
#render(options, locals = {}) ⇒ Object
Renders one or more templates with the given locals.
Example:
<%= render :template => "some_template" %>
# or by shorthand:
<%= render :some_template %>
# or render multiple templates with a single call:
<%= render %w(some_template some_other_template) %>
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/texas/template/helper/base.rb', line 108 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
122 123 124 125 126 127 |
# File 'lib/texas/template/helper/base.rb', line 122 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.
Example:
template_extensions
# => ["tex", "tex.erb", "md", "md.erb"]
135 136 137 |
# File 'lib/texas/template/helper/base.rb', line 135 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
Example:
# Given the following contents directory:
#
# contents/
# _some_partial.tex.erb
# contents.tex.erb
# other_latex.tex
# other_markdown.md.erb
# some_template.tex.erb
#
templates_by_glob("*.tex.erb")
# => ["_some_partial", "contents", "other_markdown", "some_template"]
155 156 157 158 159 160 161 |
# File 'lib/texas/template/helper/base.rb', line 155 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 |