mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-26 13:45:13 -07:00
Merge old Shopify/liquid
This commit is contained in:
+1
-1
@@ -95,7 +95,7 @@ module Liquid
|
|||||||
rescue ::StandardError => e
|
rescue ::StandardError => e
|
||||||
context.handle_error(e)
|
context.handle_error(e)
|
||||||
end
|
end
|
||||||
end
|
end.join
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ module Liquid
|
|||||||
# This will parse the template with a LocalFileSystem implementation rooted at 'template_path'.
|
# This will parse the template with a LocalFileSystem implementation rooted at 'template_path'.
|
||||||
class BlankFileSystem
|
class BlankFileSystem
|
||||||
# Called by Liquid to retrieve a template file
|
# Called by Liquid to retrieve a template file
|
||||||
def read_template_file(context, template_name)
|
def read_template_file(template_path, context)
|
||||||
raise FileSystemError, "This liquid context does not allow includes."
|
raise FileSystemError, "This liquid context does not allow includes."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -38,8 +38,7 @@ module Liquid
|
|||||||
@root = root
|
@root = root
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_template_file(context, template_name)
|
def read_template_file(template_path, context)
|
||||||
template_path = context[template_name]
|
|
||||||
full_path = full_path(template_path)
|
full_path = full_path(template_path)
|
||||||
raise FileSystemError, "No such template '#{template_path}'" unless File.exists?(full_path)
|
raise FileSystemError, "No such template '#{template_path}'" unless File.exists?(full_path)
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ module Liquid
|
|||||||
row = 1
|
row = 1
|
||||||
col = 0
|
col = 0
|
||||||
|
|
||||||
result = ["<tr class=\"row1\">\n"]
|
result = "<tr class=\"row1\">\n"
|
||||||
context.stack do
|
context.stack do
|
||||||
|
|
||||||
collection.each_with_index do |item, index|
|
collection.each_with_index do |item, index|
|
||||||
@@ -56,17 +56,18 @@ module Liquid
|
|||||||
|
|
||||||
col += 1
|
col += 1
|
||||||
|
|
||||||
result << ["<td class=\"col#{col}\">"] + render_all(@nodelist, context) + ['</td>']
|
result << "<td class=\"col#{col}\">" << render_all(@nodelist, context) << '</td>'
|
||||||
|
|
||||||
if col == cols and not (index == length - 1)
|
if col == cols and not (index == length - 1)
|
||||||
col = 0
|
col = 0
|
||||||
row += 1
|
row += 1
|
||||||
result << ["</tr>\n<tr class=\"row#{row}\">"]
|
result << "</tr>\n<tr class=\"row#{row}\">"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
result + ["</tr>\n"]
|
result << "</tr>\n"
|
||||||
|
result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ module Liquid
|
|||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
output = super
|
output = super
|
||||||
context.scopes.last[@to] = output.join
|
context.scopes.last[@to] = output
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -31,20 +31,16 @@ module Liquid
|
|||||||
context.stack do
|
context.stack do
|
||||||
execute_else_block = true
|
execute_else_block = true
|
||||||
|
|
||||||
@blocks.inject([]) do |output, block|
|
output = ''
|
||||||
|
@blocks.each do |block|
|
||||||
if block.else?
|
if block.else?
|
||||||
|
|
||||||
return render_all(block.attachment, context) if execute_else_block
|
return render_all(block.attachment, context) if execute_else_block
|
||||||
|
|
||||||
elsif block.evaluate(context)
|
elsif block.evaluate(context)
|
||||||
|
|
||||||
execute_else_block = false
|
execute_else_block = false
|
||||||
output += render_all(block.attachment, context)
|
output << render_all(block.attachment, context)
|
||||||
end
|
end
|
||||||
|
|
||||||
output
|
|
||||||
end
|
end
|
||||||
|
output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ module Liquid
|
|||||||
|
|
||||||
segment.reverse! if @reversed
|
segment.reverse! if @reversed
|
||||||
|
|
||||||
result = []
|
result = ''
|
||||||
|
|
||||||
length = segment.length
|
length = segment.length
|
||||||
|
|
||||||
|
|||||||
@@ -24,10 +24,8 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
file_system = context.registers[:file_system] || Liquid::Template.file_system
|
source = _read_template_from_file_system(context)
|
||||||
source = file_system.read_template_file(context, @template_name)
|
|
||||||
partial = Liquid::Template.parse(source)
|
partial = Liquid::Template.parse(source)
|
||||||
|
|
||||||
variable = context[@variable_name || @template_name[1..-2]]
|
variable = context[@variable_name || @template_name[1..-2]]
|
||||||
|
|
||||||
context.stack do
|
context.stack do
|
||||||
@@ -36,20 +34,31 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
if variable.is_a?(Array)
|
if variable.is_a?(Array)
|
||||||
|
|
||||||
variable.collect do |variable|
|
variable.collect do |variable|
|
||||||
context[@template_name[1..-2]] = variable
|
context[@template_name[1..-2]] = variable
|
||||||
partial.render(context)
|
partial.render(context)
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
||||||
context[@template_name[1..-2]] = variable
|
context[@template_name[1..-2]] = variable
|
||||||
partial.render(context)
|
partial.render(context)
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def _read_template_from_file_system(context)
|
||||||
|
file_system = context.registers[:file_system] || Liquid::Template.file_system
|
||||||
|
|
||||||
|
# make read_template_file call backwards-compatible.
|
||||||
|
case file_system.method(:read_template_file).arity
|
||||||
|
when 1
|
||||||
|
file_system.read_template_file(context[@template_name])
|
||||||
|
when 2
|
||||||
|
file_system.read_template_file(context[@template_name], context)
|
||||||
|
else
|
||||||
|
raise ArgumentError, "file_system.read_template_file expects two parameters: (template_name, context)"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('include', Include)
|
Template.register_tag('include', Include)
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
module Liquid
|
||||||
|
class Raw < Block
|
||||||
|
def parse(tokens)
|
||||||
|
@nodelist ||= []
|
||||||
|
@nodelist.clear
|
||||||
|
|
||||||
|
while token = tokens.shift
|
||||||
|
if token =~ FullToken
|
||||||
|
if block_delimiter == $1
|
||||||
|
end_tag
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@nodelist << token if not token.empty?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Template.register_tag('raw', Raw)
|
||||||
|
end
|
||||||
|
|
||||||
@@ -121,7 +121,8 @@ module Liquid
|
|||||||
begin
|
begin
|
||||||
# render the nodelist.
|
# render the nodelist.
|
||||||
# for performance reasons we get a array back here. join will make a string out of it
|
# for performance reasons we get a array back here. join will make a string out of it
|
||||||
@root.render(context).join
|
result = @root.render(context)
|
||||||
|
result.respond_to?(:join) ? result.join : result
|
||||||
ensure
|
ensure
|
||||||
@errors = context.errors
|
@errors = context.errors
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ module Liquid
|
|||||||
return '' if @name.nil?
|
return '' if @name.nil?
|
||||||
@filters.inject(context[@name]) do |output, filter|
|
@filters.inject(context[@name]) do |output, filter|
|
||||||
filterargs = filter[1].to_a.collect do |a|
|
filterargs = filter[1].to_a.collect do |a|
|
||||||
context[a]
|
context[a]
|
||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
output = context.invoke(filter[0], output, *filterargs)
|
output = context.invoke(filter[0], output, *filterargs)
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ module Breakpoint
|
|||||||
end
|
end
|
||||||
|
|
||||||
instance_methods.each do |method|
|
instance_methods.each do |method|
|
||||||
next if method[/^__.+__$/]
|
next if method[/^(__.+__|object_id)$/]
|
||||||
undef_method method
|
undef_method method
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ class FileSystemTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_default
|
def test_default
|
||||||
assert_raise(FileSystemError) do
|
assert_raise(FileSystemError) do
|
||||||
BlankFileSystem.new.read_template_file({'dummy'=>'smarty'}, "dummy")
|
BlankFileSystem.new.read_template_file("dummy", {'dummy'=>'smarty'})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class TestFileSystem
|
class TestFileSystem
|
||||||
def read_template_file(context, template_name)
|
def read_template_file(template_path, context)
|
||||||
template_path = context[template_name]
|
|
||||||
case template_path
|
case template_path
|
||||||
when "product"
|
when "product"
|
||||||
"Product: {{ product.title }} "
|
"Product: {{ product.title }} "
|
||||||
@@ -35,8 +34,7 @@ class TestFileSystem
|
|||||||
end
|
end
|
||||||
|
|
||||||
class OtherFileSystem
|
class OtherFileSystem
|
||||||
def read_template_file(context, template_name)
|
def read_template_file(template_path, context)
|
||||||
template_path = context[template_name]
|
|
||||||
'from OtherFileSystem'
|
'from OtherFileSystem'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -106,7 +104,7 @@ class IncludeTagTest < Test::Unit::TestCase
|
|||||||
def test_recursively_included_template_does_not_produce_endless_loop
|
def test_recursively_included_template_does_not_produce_endless_loop
|
||||||
|
|
||||||
infinite_file_system = Class.new do
|
infinite_file_system = Class.new do
|
||||||
def read_template_file(context, template_name)
|
def read_template_file(template_path, context)
|
||||||
"-{% include 'loop' %}"
|
"-{% include 'loop' %}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -119,6 +117,18 @@ class IncludeTagTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_backwards_compatability_support_for_overridden_read_template_file
|
||||||
|
infinite_file_system = Class.new do
|
||||||
|
def read_template_file(template_path) # testing only one argument here.
|
||||||
|
"- hi mom"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Liquid::Template.file_system = infinite_file_system.new
|
||||||
|
|
||||||
|
Template.parse("{% include 'hi_mom' %}").render!
|
||||||
|
end
|
||||||
|
|
||||||
def test_dynamically_choosen_template
|
def test_dynamically_choosen_template
|
||||||
|
|
||||||
assert_equal "Test123", Template.parse("{% include template %}").render("template" => 'Test123')
|
assert_equal "Test123", Template.parse("{% include template %}").render("template" => 'Test123')
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class RawTest < Test::Unit::TestCase
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def test_tag_in_raw
|
||||||
|
assert_template_result '{% comment %} test {% endcomment %}',
|
||||||
|
'{% raw %}{% comment %} test {% endcomment %}{% endraw %}'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_output_in_raw
|
||||||
|
assert_template_result '{{ test }}',
|
||||||
|
'{% raw %}{{ test }}{% endraw %}'
|
||||||
|
end
|
||||||
|
end
|
||||||
+5
-1
@@ -7,7 +7,11 @@ require 'test/unit'
|
|||||||
require 'test/unit/assertions'
|
require 'test/unit/assertions'
|
||||||
require 'caller'
|
require 'caller'
|
||||||
require 'breakpoint'
|
require 'breakpoint'
|
||||||
require 'ruby-debug'
|
begin
|
||||||
|
require 'ruby-debug'
|
||||||
|
rescue LoadError
|
||||||
|
puts "Couldn't load ruby-debug. gem install ruby-debug if you need it."
|
||||||
|
end
|
||||||
require File.join(File.dirname(__FILE__), '..', 'lib', 'liquid')
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'liquid')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user