mirror of
https://github.com/kemko/liquid.git
synced 2026-01-04 17:25:41 +03:00
if blocks work with filtered variables
This commit is contained in:
@@ -22,7 +22,7 @@
|
||||
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
||||
|
||||
module Liquid
|
||||
FilterSperator = /\|/
|
||||
FilterSeparator = /\|/
|
||||
ArgumentSeparator = ','
|
||||
FilterArgumentSeparator = ':'
|
||||
VariableAttributeSeparator = '.'
|
||||
@@ -33,7 +33,12 @@ module Liquid
|
||||
VariableStart = /\{\{/
|
||||
VariableEnd = /\}\}/
|
||||
VariableIncompleteEnd = /\}\}?/
|
||||
QuotedFragment = /"[^"]+"|'[^']+'|[^\s,|]+/
|
||||
QuotedFragment = /"[^"]+"|'[^']+'|[^\s,\|]+/
|
||||
StrictQuotedFragment = /"[^"]+"|'[^']+'|[^\s,\|,\:,\,]+/
|
||||
FirstFilterArgument = /#{FilterArgumentSeparator}(?:#{StrictQuotedFragment})/
|
||||
OtherFilterArgument = /#{ArgumentSeparator}(?:#{StrictQuotedFragment})/
|
||||
SpacelessFilter = /#{FilterSeparator}(?:#{StrictQuotedFragment})(?:#{FirstFilterArgument}(?:#{OtherFilterArgument})*)?/
|
||||
Expression = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/
|
||||
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/
|
||||
TemplateParser = /(#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd})/
|
||||
VariableParser = /\[[^\]]+\]|#{VariableSegment}+/
|
||||
|
||||
@@ -133,6 +133,9 @@ module Liquid
|
||||
:blank?
|
||||
when 'empty'
|
||||
:empty?
|
||||
# filtered variables
|
||||
when SpacelessFilter
|
||||
filtered_variable(key)
|
||||
# Single quoted strings
|
||||
when /^'(.*)'$/
|
||||
$1.to_s
|
||||
@@ -221,5 +224,9 @@ module Liquid
|
||||
|
||||
object
|
||||
end
|
||||
|
||||
def filtered_variable(markup)
|
||||
Variable.new(markup).render(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,7 +13,7 @@ module Liquid
|
||||
#
|
||||
class If < Block
|
||||
SyntaxHelp = "Syntax Error in tag 'if' - Valid syntax: if [expression]"
|
||||
Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/
|
||||
Syntax = /(#{Expression})\s*([=!<>a-z_]+)?\s*(#{Expression})?/
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
|
||||
|
||||
@@ -19,8 +19,8 @@ module Liquid
|
||||
@filters = []
|
||||
if match = markup.match(/\s*(#{QuotedFragment})/)
|
||||
@name = match[1]
|
||||
if markup.match(/#{FilterSperator}\s*(.*)/)
|
||||
filters = Regexp.last_match(1).split(/#{FilterSperator}/)
|
||||
if markup.match(/#{FilterSeparator}\s*(.*)/)
|
||||
filters = Regexp.last_match(1).split(/#{FilterSeparator}/)
|
||||
|
||||
filters.each do |f|
|
||||
if matches = f.match(/\s*(\w+)/)
|
||||
|
||||
@@ -112,6 +112,13 @@ class IfElseTest < Test::Unit::TestCase
|
||||
assert_template_result('elsif','{% if false %}if{% elsif true %}elsif{% endif %}')
|
||||
end
|
||||
|
||||
def test_with_filtered_expressions
|
||||
assert_template_result('yes','{% if "BLAH"|downcase == "blah" %}yes{% endif %}')
|
||||
assert_template_result('yes','{% if "FOO BAR"|truncatewords:1,"--" == "FOO--" %}yes{% endif %}')
|
||||
assert_template_result('yes','{% if "FOO BAR"|truncatewords:1,"--"|downcase == "foo--" %}yes{% endif %}')
|
||||
assert_template_result('yes','{% if "foo--" == "FOO BAR"|truncatewords:1,"--"|downcase %}yes{% endif %}')
|
||||
end
|
||||
|
||||
def test_syntax_error_no_variable
|
||||
assert_raise(SyntaxError){ assert_template_result('', '{% if jerry == 1 %}')}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user