diff --git a/README.md b/README.md index 93a726a9..da24e54f 100644 --- a/README.md +++ b/README.md @@ -99,15 +99,14 @@ Setting the error mode of Liquid lets you specify how strictly you want your tem Normally the parser is very lax and will accept almost anything without error. Unfortunately this can make it very hard to debug and can lead to unexpected behaviour. -Liquid also comes with a stricter parser that can be used when editing templates to give better error messages +Liquid also comes with a parser that can be used when editing templates to give better error messages when templates are invalid. You can enable this new parser like this: ```ruby -Liquid::Environment.default.error_mode = :strict -Liquid::Environment.default.error_mode = :strict # Raises a SyntaxError when invalid syntax is used -Liquid::Environment.default.error_mode = :warn # Adds strict errors to template.errors but continues as normal -Liquid::Environment.default.error_mode = :lax # The default mode, accepts almost anything. -Liquid::Environment.default.error_mode = :rigid # Uses Parser.new instead of Expression.parse for stricter parsing +Liquid::Environment.default.error_mode = :rigid # Raises a SyntaxError when invalid syntax is used in all tags +Liquid::Environment.default.error_mode = :strict # Raises a SyntaxError when invalid syntax is used in some tags +Liquid::Environment.default.error_mode = :warn # Adds strict errors to template.errors but continues as normal +Liquid::Environment.default.error_mode = :lax # The default mode, accepts almost anything. ``` If you want to set the error mode only on specific templates you can pass `:error_mode` as an option to `parse`: diff --git a/bin/example.liquid b/bin/example.liquid deleted file mode 100644 index c4a93aa3..00000000 --- a/bin/example.liquid +++ /dev/null @@ -1,5 +0,0 @@ - - {% tablerow i in (1..10) limit: foo=>bar %} - {{ i }} - {% endtablerow %} -
diff --git a/bin/render b/bin/render index 004c103f..26d9b53d 100755 --- a/bin/render +++ b/bin/render @@ -6,12 +6,20 @@ require 'liquid' class VirtualFileSystem def initialize - snippet_1 = '

{{ greating | default: "Hello" }}, {{ name | default: "world" }}!

' - snippet_2 = '{% for i in (1..5) %} > {{ i }}{% endfor %}' + snippet_1 = <<~LIQUID +

+ {{- greating | default: 'Hello' }}, {{ name | default: 'world' -}}! +

+ LIQUID + snippet_2 = <<~LIQUID + {%- for i in (1..5) -%} + > {{ i }} + {%- endfor -%} + LIQUID @templates = { - 'snippet_1' => snippet_1, - 'snippet_2' => snippet_2, + 'snippet-1' => snippet_1, + 'snippet-2' => snippet_2, } end @@ -20,11 +28,19 @@ class VirtualFileSystem end end -error_mode = :strict -# error_mode = :rigid -file = File.read(ARGV[0]) -template = Liquid::Template.parse(file, error_mode: error_mode) +def source + File.read(ARGV[0]) +rescue StandardError + 'Usage: bin/render example/server/templates/index.liquid' +end -template.registers[:file_system] = VirtualFileSystem.new +def assigns + { + 'date' => Time.now, + } +end -puts template.render +puts Liquid::Template + .parse(source, error_mode: :rigid) + .tap { |t| t.registers[:file_system] = VirtualFileSystem.new } + .render(assigns)