class SimpleTemplates::Template

A Template is a renderable collection of SimpleTemplates::AST nodes.

@!attribute [r] ast

@return <Array[SimpleTemplates::AST::Node]> a list of renderable nodes

@!attribute [r] errors

@return <Array[SimpleTemplates::Parser::Error]> a list of errors found
  during parsing

@!attribute [r] remaining_tokens

@return <Array[SimpleTemplates::Lexer::Token]> a list of the remaining
  not parsed tokens

Attributes

ast[R]
errors[R]
remaining_tokens[R]

Public Class Methods

new(ast = [], errors = [], remaining_tokens = []) click to toggle source

Initializes a new Template @param ast <Array> list of AST nodes @param errors <Array> a list of errors

found during parsing

@param remaining_tokens <Array> list of

unparsed tokens from the input
# File lib/simple_templates/template.rb, line 28
def initialize(ast = [], errors = [], remaining_tokens = [])
  @ast              = ast.clone.freeze
  @errors           = errors.clone.freeze
  @remaining_tokens = remaining_tokens.clone.freeze
end

Public Instance Methods

==(other) click to toggle source

Compares a Template with another by comparing the ast, errors and the remaining_tokens of each one

# File lib/simple_templates/template.rb, line 59
def ==(other)
  ast == other.ast &&
    errors == other.errors &&
    remaining_tokens == other.remaining_tokens
end
placeholder_names() click to toggle source

Returns all placeholder names used in the template. @return [Set<String>] Placeholders content

# File lib/simple_templates/template.rb, line 36
def placeholder_names
  placeholders.map(&:contents).to_set
end
placeholders() click to toggle source

Returns all the SimpleTemplates::AST::Placeholder nodes in the ast

list of the instance

@return [Set<SimpleTemplates::AST::Placeholder>]

# File lib/simple_templates/template.rb, line 53
def placeholders
  ast.select{ |node| SimpleTemplates::AST::Placeholder === node }.to_set
end
render(substitutions) click to toggle source

Accepts a hash with the placeholder names as keys and the values for substitution @param substitutions [Hash{Symbol => String}] a hash with the placeholder

name as the key and the substitution for that placeholder as value

@return [String] The concatenated result of rendering the substitutions

# File lib/simple_templates/template.rb, line 45
def render(substitutions)
  raise errors.map(&:message).join(", ") unless errors.empty?
  ast.map { |node| node.render(substitutions) }.join
end