* Update bin/render script to present an error when no template is passed

* Remove `bin/example.liquid` as it's not executable
This commit is contained in:
Guilherme Carreiro
2025-10-27 16:33:31 +01:00
committed by Guilherme Carreiro
parent 34ab3bdc8e
commit 4cd367d971
3 changed files with 31 additions and 21 deletions
+5 -6
View File
@@ -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 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. 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: when templates are invalid. You can enable this new parser like this:
```ruby ```ruby
Liquid::Environment.default.error_mode = :strict 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 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 = :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 = :lax # The default mode, accepts almost anything.
Liquid::Environment.default.error_mode = :rigid # Uses Parser.new instead of Expression.parse for stricter parsing
``` ```
If you want to set the error mode only on specific templates you can pass `:error_mode` as an option to `parse`: If you want to set the error mode only on specific templates you can pass `:error_mode` as an option to `parse`:
-5
View File
@@ -1,5 +0,0 @@
<table>
{% tablerow i in (1..10) limit: foo=>bar %}
{{ i }}
{% endtablerow %}
</table>
+26 -10
View File
@@ -6,12 +6,20 @@ require 'liquid'
class VirtualFileSystem class VirtualFileSystem
def initialize def initialize
snippet_1 = '<h1>{{ greating | default: "Hello" }}, {{ name | default: "world" }}!</h1>' snippet_1 = <<~LIQUID
snippet_2 = '{% for i in (1..5) %} > {{ i }}{% endfor %}' <h1>
{{- greating | default: 'Hello' }}, {{ name | default: 'world' -}}!
</h1>
LIQUID
snippet_2 = <<~LIQUID
{%- for i in (1..5) -%}
> {{ i }}
{%- endfor -%}
LIQUID
@templates = { @templates = {
'snippet_1' => snippet_1, 'snippet-1' => snippet_1,
'snippet_2' => snippet_2, 'snippet-2' => snippet_2,
} }
end end
@@ -20,11 +28,19 @@ class VirtualFileSystem
end end
end end
error_mode = :strict def source
# error_mode = :rigid File.read(ARGV[0])
file = File.read(ARGV[0]) rescue StandardError
template = Liquid::Template.parse(file, error_mode: error_mode) '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)