module SimpleTemplates

A minimalistic templates engine

Constants

Delimiter

A Struct for a Delimiter that takes Regexp for the quoted start and quoted end of the placeholder as well as the start and end

Unescapes

A Struct for the unescaped symbols. You want this to mark the placeholder tags. Takes a character for start and another for the end tag

VERSION

Public Class Methods

parse(raw_template_string, allowed_placeholders = nil) click to toggle source

Builds a template renderer from given string template and list of allowed placeholders

@param raw_template_string String the template to render @param allowed_placeholders Array list of allowed placeholders @return [<SimpleTemplates::Template>]

A template cointaining a list of ASTs, errors and unparsed tokens

@example template without errors

template = SimpleTemplates.parse("Hi <name>", %w[name])
template.render({ name: "Bob" }) if template.errors.empty?
=> "Hi Bob"

@example template with errors

template = SimpleTemplates.parse("Hi <name>", %w[date])
template.errors
=> [...] # unknown placeholder
# File lib/simple_templates.rb, line 28
def self.parse(raw_template_string, allowed_placeholders = nil)
  Template.new(
    *Parser.new(
      Unescapes.new('<', '>'),
      Lexer.new(Delimiter.new(/\\</, /\\>/, /\</, /\>/), raw_template_string).
        tokenize,
      allowed_placeholders
    ).parse
  )
end