mirror of
https://github.com/jekyll/jekyll.git
synced 2026-02-02 02:35:17 -05:00
To use, just include `Jekyll::LiquidExtensions` as you please:
```ruby
class SayHi < Liquid::Tag
include Jekyll::LiquidExtensions
def initialize(tag_name, markup, tokens)
@markup = markup.strip
end
def render(context)
"hi #{lookup_variable(context, @markup)}"
end
end
```
Fixes #2071.
23 lines
497 B
Ruby
23 lines
497 B
Ruby
module Jekyll
|
|
module LiquidExtensions
|
|
|
|
# Lookup a Liquid variable in the given context.
|
|
#
|
|
# context - the Liquid context in question.
|
|
# variable - the variable name, as a string.
|
|
#
|
|
# Returns the value of the variable in the context
|
|
# or the variable name if not found.
|
|
def lookup_variable(context, variable)
|
|
lookup = context
|
|
|
|
variable.split(".").each do |value|
|
|
lookup = lookup[value]
|
|
end
|
|
|
|
lookup || variable
|
|
end
|
|
|
|
end
|
|
end
|