Module: Cadenza

Defined in:
lib/cadenza/racc_parser.rb,
lib/cadenza.rb,
lib/cadenza/error.rb,
lib/cadenza/lexer.rb,
lib/cadenza/token.rb,
lib/cadenza/parser.rb,
lib/cadenza/context.rb,
lib/cadenza/version.rb,
lib/cadenza/base_renderer.rb,
lib/cadenza/nodes/if_node.rb,
lib/cadenza/text_renderer.rb,
lib/cadenza/nodes/for_node.rb,
lib/cadenza/nodes/text_node.rb,
lib/cadenza/nodes/block_node.rb,
lib/cadenza/filesystem_loader.rb,
lib/cadenza/nodes/filter_node.rb,
lib/cadenza/nodes/inject_node.rb,
lib/cadenza/nodes/constant_node.rb,
lib/cadenza/nodes/document_node.rb,
lib/cadenza/nodes/variable_node.rb,
lib/cadenza/nodes/operation_node.rb,
lib/cadenza/nodes/generic_block_node.rb,
lib/cadenza/nodes/boolean_inverse_node.rb

Overview

racc_parser.rb : generated by racc

Defined Under Namespace

Modules: Version Classes: BaseRenderer, BlockNode, BlockNotDefinedError, BooleanInverseNode, ConstantNode, Context, DocumentNode, Error, FilesystemLoader, FilterNode, FilterNotDefinedError, ForNode, FunctionalVariableNotDefinedError, GenericBlockNode, IfNode, InjectNode, Lexer, OperationNode, ParseError, Parser, RaccParser, TemplateNotFoundError, TextNode, TextRenderer, Token, VariableNode

Constant Summary collapse

BaseContext =
Context.new

Class Method Summary collapse

Class Method Details

.render(template_text, scope = nil) ⇒ Object

this utility method sets up the standard Cadenza lexer/parser/renderer stack and renders the given template text with the given variable scope using the BaseContext. the result of rendering is returned as a string.

Parameters:

  • template_text (String)

    the content of the template to parse/render

  • scope (Hash) (defaults to: nil)

    any variables to define as a new scope for BaseContext in this template.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cadenza.rb', line 27

def self.render(template_text, scope=nil)
   template = Parser.new.parse(template_text)

   context = BaseContext.clone

   context.push(scope) if scope

   output = StringIO.new

   TextRenderer.new(output).render(template, context)

   output.string
end

.render_template(template_name, scope = nil) ⇒ Object

similar to #render except the given template name will be loaded using BaseContexts predefined list of loaders.

Parameters:

  • template_name (String)

    the name of the template to load then parse and render

  • scope (Hash) (defaults to: nil)

    any variables to define as a new scope for BaseContext in this template.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cadenza.rb', line 47

def self.render_template(template_name, scope=nil)
   context = BaseContext.clone

   context.push(scope) if scope

   template = context.load_template(template_name)

   output = StringIO.new
   
   TextRenderer.new(output).render(template, context)

   output.string
end