Commit 3c3eb2f7 authored by Holger Just's avatar Holger Just

[#790] Register variables with an API

parent dc5fd8bc
......@@ -472,7 +472,7 @@ module ApplicationHelper
liquid_variables = get_view_instance_variables_for_liquid
liquid_variables.merge!({'current_user' => User.current})
liquid_variables.merge!({'toc' => '{{toc}}'}) # Pass toc through to replace later
liquid_variables.merge!(ChiliProject::Liquid::Variables.macro_backwards_compatibility)
liquid_variables.merge!(ChiliProject::Liquid::Variables.all)
# Pass :view in a register so this view (with helpers) can be used inside of a tag
text = liquid_template.render(liquid_variables, :registers => {:view => self, :object => obj, :attribute => attr})
......
......@@ -15,14 +15,32 @@
module ChiliProject
module Liquid
module Variables
# Liquid "variables" that are used for backwards compatability with macros
#
# Variables are used in liquid like {{var}}
def self.macro_backwards_compatibility
{
'macro_list' => "Use the '{% variable_list %}' tag to see all Liquid variables and '{% tag_list %}' to see all of the Liquid tags."
}
class LazyVariable
def initialize(context, &block)
@block = block
@context = context
end
def to_liquid()
(@block.arity == 0) ? @block.call : @block.call(@context)
end
end
# Register a Liquid variable.
# Pass a value to register a fixed variable or a block to create a lazy
# evaluated variable. The block can take the current context
def self.register(name, value=nil, &block)
var = block_given? ? Proc.new(){|ctx| LazyVariable.new(ctx, &block)} : value
all[name.to_s] = var
end
def self.all
@variables ||= {}
end
# DEPRACATED: This is just a hint on how to use Liquid introspection
register "macro_list",
"Use the '{% variable_list %}' tag to see all Liquid variables and '{% tag_list %}' to see all of the Liquid tags."
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment