module YARD::Templates::Template
def T(*path) # rubocop:disable Style/MethodName
-
(Template)
- the loaded template module
Parameters:
-
path
(Array
) -- the path of the template
def T(*path) # rubocop:disable Style/MethodName path.unshift(options.template) if options.template path.push(options.format) if options.format self.class.T(*path) end
def add_options(opts = nil)
def add_options(opts = nil) return(yield) if opts.nil? && block_given? cur_opts = options if block_given? self.options = options.merge(opts) if block_given? value = yield self.options = cur_opts value end end
def cache(section)
def cache(section) content = @cache[section.to_sym] return content if content file = cache_filename(section) @cache_filename[section.to_sym] = file raise ArgumentError, "no template for section '#{section}' in #{self.class.path}" unless file @cache[section.to_sym] = IO.read(file) end
def cache_filename(section)
def cache_filename(section) @cache_filename[section.to_sym] ||= self.class.find_file(erb_file_for(section)) end
def erb(section, &block)
-
(String)
- the contents of the ERB rendered section
Other tags:
- Yield: - calls subsections to be rendered
Parameters:
-
section
(String, Symbol
) -- the section name
def erb(section, &block) method_name = ErbCache.method_for(cache_filename(section)) do erb_with(cache(section), cache_filename(section)) end send(method_name, &block) end
def erb_file_for(section)
def erb_file_for(section) "#{section}.erb" end
def erb_with(content, filename = nil)
def erb_with(content, filename = nil) erb = if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+ ERB.new(content, :trim_mode => options.format == :text ? '<>' : nil) else ERB.new(content, nil, options.format == :text ? '<>' : nil) end erb.filename = filename if filename erb end
def file(basename, allow_inherited = false)
- See: ClassMethods#find_nth_file -
See: ClassMethods#find_file -
Returns:
-
(String)
- the contents of a file identified by +basename+. All
Parameters:
-
allow_inherited
(Boolean
) -- whether inherited templates can -
basename
(String
) -- the name of the file
def file(basename, allow_inherited = false) file = self.class.find_file(basename) raise ArgumentError, "no file for '#{basename}' in #{self.class.path}" unless file data = IO.read(file) if allow_inherited superfile = self.class.find_nth_file(basename, 2) data.gsub!('{{{__super__}}}', superfile ? IO.read(superfile) : "") end data end
def include_extra(template, options)
-
(void)
-
Parameters:
-
options
(SymbolHash
) -- the options hash containing all template information -
template
(Template
) -- the template object to mixin the extra includes.
def include_extra(template, options) extra_includes.each do |mod| mod = mod.call(options) if mod.is_a?(Proc) next unless mod.is_a?(Module) template.extend(mod) end end
def included(klass)
- Private: -
def included(klass) klass.extend(ClassMethods) end
def init
- See: #sections -
Other tags:
- Example: A default set of sections -
def init end
def initialize(opts = TemplateOptions.new)
def initialize(opts = TemplateOptions.new) opts_class = opts.class opts_class = TemplateOptions if opts_class == Hash @cache = {} @cache_filename = {} @sections = [] @options = opts_class.new add_options(opts) Template.include_extra(self, options) init end
def inspect
def inspect "Template(#{self.class.path}) [section=#{section.name}]" end
def options=(value)
def options=(value) @options = value set_ivars end
def render_section(section, &block)
def render_section(section, &block) section = section.name if section.is_a?(Section) case section when Section, String, Symbol if respond_to?(section) send(section, &block) else erb(section, &block) end when Module, Template section.run(options, &block) if section.is_a?(Template) end || "" end
def run(opts = nil, sects = sections, start_at = 0, break_first = false, &block)
-
(String)
- the rendered sections joined together
Other tags:
- Yieldparam: opts - any extra options to yield
Other tags:
- Yield: - calls for the subsections to be rendered
Parameters:
-
break_first
(Boolean
) -- if true, renders only the first section -
start_at
(Fixnum
) -- the index in the section list to start from -
sects
(Section, Array
) -- a section list of sections to render -
opts
(Hash, nil
) -- any extra options to apply to sections
def run(opts = nil, sects = sections, start_at = 0, break_first = false, &block) out = String.new("") return out if sects.nil? sects = sects[start_at..-1] if start_at > 0 sects = Section.new(nil, sects) unless sects.is_a?(Section) add_options(opts) do sects.each do |s| self.section = s subsection_index = 0 value = render_section(section) do |*args| value = with_section do run(args.first, section, subsection_index, true, &block) end subsection_index += 1 value end out << (value || "") break if break_first end end out end
def sections(*args)
-
args
(Array
) -- the sections
Other tags:
- Example: Sections with subsections -
Example: Sets a set of method and erb sections -
Example: Sets a set of erb sections -
def sections(*args) @sections = Section.new(nil, *args) unless args.empty? @sections end
def set_ivars
def set_ivars options.each do |k, v| instance_variable_set("@#{k}", v) end end
def superb(sect = section, &block)
-
(String)
- the rendered ERB file in any of the inherited template
Parameters:
-
sect
(Symbol, String
) -- if provided, uses a specific section name
def superb(sect = section, &block) filename = self.class.find_nth_file(erb_file_for(sect), 2) return "" unless filename method_name = ErbCache.method_for(filename) { erb_with(IO.read(filename), filename) } send(method_name, &block) end
def with_section
def with_section sect = section value = yield self.section = sect value end
def yieldall(opts = nil, &block)
-
opts
(Hash
) -- extra options to be applied to subsections
def yieldall(opts = nil, &block) with_section { run(opts, section, &block) } end