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) ⇒ 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
-
.tokenize(source) ⇒ Object
Uses the
Liquid::TokenizationRegexp
regexp to tokenize the passed source.
Instance Method Summary collapse
-
#initialize(tokens = []) ⇒ Template
constructor
creates a new
Template
from an array of tokens. -
#render(assigns = {}, options = nil) ⇒ Object
Render takes a hash with local variables.
Constructor Details
#initialize(tokens = []) ⇒ Template
creates a new Template
from an array of tokens. Use Template.parse
instead
59 60 61 |
# File 'lib/liquid/template.rb', line 59 def initialize(tokens = []) @root = Document.new(tokens) 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
20 21 22 |
# File 'lib/liquid/template.rb', line 20 def self.file_system @@file_system end |
.file_system=(obj) ⇒ Object
24 25 26 |
# File 'lib/liquid/template.rb', line 24 def self.file_system=(obj) @@file_system = obj end |
.parse(source) ⇒ Object
creates a new Template
object from liquid source code
43 44 45 |
# File 'lib/liquid/template.rb', line 43 def self.parse(source) self.new(tokenize(source)) 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
38 39 40 |
# File 'lib/liquid/template.rb', line 38 def self.register_filter(mod) Strainer.global_filter(mod) end |
.register_tag(name, klass) ⇒ Object
28 29 30 |
# File 'lib/liquid/template.rb', line 28 def self.register_tag(name, klass) [name.to_s] = klass end |
.tags ⇒ Object
32 33 34 |
# File 'lib/liquid/template.rb', line 32 def self. ||= {} end |
.tokenize(source) ⇒ Object
Uses the Liquid::TokenizationRegexp
regexp to tokenize the passed source
48 49 50 51 52 53 54 55 56 |
# File 'lib/liquid/template.rb', line 48 def self.tokenize(source) return [] if source.to_s.empty? tokens = source.split(TokenizationRegexp) # removes the rogue empty element at the beginning of the array tokens.shift if tokens[0] and tokens[0].empty? tokens end |
Instance Method Details
#render(assigns = {}, options = nil) ⇒ 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 and might be useful to integrate liquid more with its host application
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/liquid/template.rb', line 74 def render(assigns = {}, = nil) = { :filters => } unless .is_a?(Hash) context = Context.new(assigns, [:registers]) # Apply all filters [[:filters]].flatten.each do |filter| context.add_filters(filter) end # render the nodelist. # for performance reasons we get a array back here. to_s will make a string out of it @root.render(context).to_s end |