[#790] Register variables with an API
This commit is contained in:
parent
dc5fd8bc10
commit
3c3eb2f7e7
|
@ -472,7 +472,7 @@ module ApplicationHelper
|
||||||
liquid_variables = get_view_instance_variables_for_liquid
|
liquid_variables = get_view_instance_variables_for_liquid
|
||||||
liquid_variables.merge!({'current_user' => User.current})
|
liquid_variables.merge!({'current_user' => User.current})
|
||||||
liquid_variables.merge!({'toc' => '{{toc}}'}) # Pass toc through to replace later
|
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
|
# 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})
|
text = liquid_template.render(liquid_variables, :registers => {:view => self, :object => obj, :attribute => attr})
|
||||||
|
|
|
@ -15,14 +15,32 @@
|
||||||
module ChiliProject
|
module ChiliProject
|
||||||
module Liquid
|
module Liquid
|
||||||
module Variables
|
module Variables
|
||||||
# Liquid "variables" that are used for backwards compatability with macros
|
class LazyVariable
|
||||||
#
|
def initialize(context, &block)
|
||||||
# Variables are used in liquid like {{var}}
|
@block = block
|
||||||
def self.macro_backwards_compatibility
|
@context = context
|
||||||
{
|
end
|
||||||
'macro_list' => "Use the '{% variable_list %}' tag to see all Liquid variables and '{% tag_list %}' to see all of the Liquid tags."
|
|
||||||
}
|
def to_liquid()
|
||||||
|
(@block.arity == 0) ? @block.call : @block.call(@context)
|
||||||
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue