Module: Cadenza

Defined in:
lib/cadenza/racc_parser.rb,
lib/cadenza.rb,
lib/cadenza/cli.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/context/stack.rb,
lib/cadenza/nodes/if_node.rb,
lib/cadenza/text_renderer.rb,
lib/cadenza/context/blocks.rb,
lib/cadenza/context_object.rb,
lib/cadenza/nodes/for_node.rb,
lib/cadenza/block_hierarchy.rb,
lib/cadenza/context/filters.rb,
lib/cadenza/context/loaders.rb,
lib/cadenza/nodes/text_node.rb,
lib/cadenza/source_renderer.rb,
lib/cadenza/nodes/block_node.rb,
lib/cadenza/filesystem_loader.rb,
lib/cadenza/nodes/filter_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/filtered_value_node.rb,
lib/cadenza/nodes/boolean_inverse_node.rb,
lib/cadenza/context/functional_variables.rb

Overview

racc_parser.rb : generated by racc

Defined Under Namespace

Modules: Cli, Version Classes: BaseRenderer, BlockHierarchy, BlockNode, BooleanInverseNode, ConstantNode, Context, ContextObject, DocumentNode, Error, FilesystemLoader, FilterNode, FilteredValueNode, ForNode, GenericBlockNode, IfNode, Lexer, OperationNode, ParseError, Parser, RaccParser, SourceRenderer, TextNode, TextRenderer, Token, VariableNode

Constant Summary collapse

BaseContext =
Context.new
BlockNotDefinedError =
Class.new(Cadenza::Error)
FilterNotDefinedError =
Class.new(Cadenza::Error)
TemplateNotFoundError =
Class.new(Cadenza::Error)
FunctionalVariableNotDefinedError =
Class.new(Cadenza::Error)

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.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cadenza.rb', line 43

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.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cadenza.rb', line 63

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