Class: Liquid::Template
- Inherits:
-
Object
- Object
- Liquid::Template
- Defined in:
- lib/liquid/template.rb
Overview
Templates are central to liquid. Interpretating templates is a two step process. First you compile the source code you got. During compile time some extensive error checking is performed. your code should expect to get some SyntaxErrors.
After you have a compiled template you can then render
it. You can use a compiled template over and over again and keep it cached.
Example:
template = Liquid::Template.parse(source)
template.render('user_name' => 'bob')
Constant Summary collapse
- @@file_system =
BlankFileSystem.new
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Class Method Summary collapse
- .file_system ⇒ Object
- .file_system=(obj) ⇒ Object
-
.parse(source, context = {}) ⇒ Object
creates a new
Template
object from liquid source code. -
.register_filter(mod) ⇒ Object
Pass a module with filter methods which should be available to all liquid views.
- .register_tag(name, klass) ⇒ Object
- .tags ⇒ Object
Instance Method Summary collapse
- #_walk(list, memo = {}, &block) ⇒ Object
- #assigns ⇒ Object
- #errors ⇒ Object
-
#initialize ⇒ Template
constructor
creates a new
Template
from an array of tokens. - #instance_assigns ⇒ Object
-
#parse(source, context = {}) ⇒ Object
Parse source code.
- #registers ⇒ Object
-
#render(*args) ⇒ Object
Render takes a hash with local variables.
- #render!(*args) ⇒ Object
- #walk(memo = {}, &block) ⇒ Object
Constructor Details
#initialize ⇒ Template
creates a new Template
from an array of tokens. Use Template.parse
instead
52 53 |
# File 'lib/liquid/template.rb', line 52 def initialize end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
17 18 19 |
# File 'lib/liquid/template.rb', line 17 def root @root end |
Class Method Details
.file_system ⇒ Object
21 22 23 |
# File 'lib/liquid/template.rb', line 21 def file_system @@file_system end |
.file_system=(obj) ⇒ Object
25 26 27 |
# File 'lib/liquid/template.rb', line 25 def file_system=(obj) @@file_system = obj end |
.parse(source, context = {}) ⇒ Object
creates a new Template
object from liquid source code
44 45 46 47 48 |
# File 'lib/liquid/template.rb', line 44 def parse(source, context = {}) template = Template.new template.parse(source, context) template end |
.register_filter(mod) ⇒ Object
Pass a module with filter methods which should be available to all liquid views. Good for registering the standard library
39 40 41 |
# File 'lib/liquid/template.rb', line 39 def register_filter(mod) Strainer.global_filter(mod) end |
.register_tag(name, klass) ⇒ Object
29 30 31 |
# File 'lib/liquid/template.rb', line 29 def register_tag(name, klass) [name.to_s] = klass end |
.tags ⇒ Object
33 34 35 |
# File 'lib/liquid/template.rb', line 33 def @tags ||= {} end |
Instance Method Details
#_walk(list, memo = {}, &block) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/liquid/template.rb', line 140 def _walk(list, memo = {}, &block) list.each do |node| saved_memo = memo.clone # puts "fetch ! #{node.respond_to?(:name) ? node.name : 'String'} / #{node.respond_to?(:nodelist)}" if block_given? # puts "youpi ! #{node.name}" _memo = yield(node, memo) || {} memo.merge!(_memo) end if node.respond_to?(:nodelist) && !node.nodelist.blank? self._walk(node.nodelist, memo, &block) end memo = saved_memo end memo end |
#assigns ⇒ Object
66 67 68 |
# File 'lib/liquid/template.rb', line 66 def assigns @assigns ||= {} end |
#errors ⇒ Object
74 75 76 |
# File 'lib/liquid/template.rb', line 74 def errors @errors ||= [] end |
#instance_assigns ⇒ Object
70 71 72 |
# File 'lib/liquid/template.rb', line 70 def instance_assigns @instance_assigns ||= {} end |
#parse(source, context = {}) ⇒ Object
Parse source code. Returns self for easy chaining
57 58 59 60 |
# File 'lib/liquid/template.rb', line 57 def parse(source, context = {}) @root = Document.new(tokenize(source), context.merge!(:template => self)) self end |
#registers ⇒ Object
62 63 64 |
# File 'lib/liquid/template.rb', line 62 def registers @registers ||= {} end |
#render(*args) ⇒ Object
Render takes a hash with local variables.
if you use the same filters over and over again consider registering them globally with Template.register_filter
Following options can be passed:
* <tt>filters</tt> : array with local filters
* <tt>registers</tt> : hash with register variables. Those can be accessed from
filters and tags and might be useful to integrate liquid more with its host application
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/liquid/template.rb', line 89 def render(*args) return '' if @root.nil? context = case args.first when Liquid::Context args.shift when Hash Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors) when nil Context.new(assigns, instance_assigns, registers, @rethrow_errors) else raise ArgumentError, "Expect Hash or Liquid::Context as parameter" end case args.last when Hash = args.pop if [:registers].is_a?(Hash) self.registers.merge!([:registers]) end if [:filters] context.add_filters([:filters]) end when Module context.add_filters(args.pop) when Array context.add_filters(args.pop) end begin # render the nodelist. # for performance reasons we get a array back here. join will make a string out of it result = @root.render(context) result.respond_to?(:join) ? result.join : result ensure @errors = context.errors end end |
#render!(*args) ⇒ Object
131 132 133 |
# File 'lib/liquid/template.rb', line 131 def render!(*args) @rethrow_errors = true; render(*args) end |
#walk(memo = {}, &block) ⇒ Object
135 136 137 138 |
# File 'lib/liquid/template.rb', line 135 def walk(memo = {}, &block) # puts @root.nodelist.inspect self._walk(@root.nodelist, memo, &block) end |