Skip to content

Support custom markup delimiters#16

Merged
jg-rp merged 2 commits intomainfrom
custom-delimiters
Jun 2, 2025
Merged

Support custom markup delimiters#16
jg-rp merged 2 commits intomainfrom
custom-delimiters

Conversation

@jg-rp
Copy link
Copy Markdown
Owner

@jg-rp jg-rp commented Jun 1, 2025

This PR refactors Liquid2::Scanner and changes Liquid2::Environment to allow for custom markup delimiters, which default to {{ and }} for output statements, {% and %} for tags and {# and #} for comments.

For example, we can change all those curly brackets to be square brackets.

require "liquid2"

env = Liquid2::Environment.new(
  markup_out_start: "[[",
  markup_out_end: "]]",
  markup_tag_start: "[%",
  markup_tag_end: "%]",
  markup_comment_prefix: "[#",
  markup_comment_suffix: "]"
)

source = "[% if true %]Hello, [[ you ]]![% endif %]"
puts env.render(source, "you" => "World")

And configure multiple environments with different delimiters.

env = Liquid2::Environment.new(
  markup_out_start: "[[",
  markup_out_end: "]]"
)

source = "Hello, [[ you ]]!"

another_env = Liquid2::Environment.new(
  markup_out_start: "((",
  markup_out_end: "))"
)

another_source = "Hello, (( you ))!"

puts env.render(source, "you" => "World")
puts another_env.render(another_source, "you" => "Liquid")

This feature does mean the tokenizer is slightly slower now then when we had hard coded delimiters, but I'm inclined to go with it.

A word of warning though, both ]] and )) from the examples above can appear in standard Liquid expressions. So one would need to pick more elaborate delimiters that don't include existing punctuation, or accept that some syntax will not be available.

@jg-rp jg-rp marked this pull request as ready for review June 2, 2025 06:32
@jg-rp jg-rp merged commit d95c70a into main Jun 2, 2025
8 checks passed
@jg-rp jg-rp deleted the custom-delimiters branch June 2, 2025 06:51
@jg-rp jg-rp added the feature label Jun 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant