Created
December 18, 2025 09:46
-
-
Save xavivars/7e43d8ad78520655ada941b0e14c3797 to your computer and use it in GitHub Desktop.
LiquidJS with only control flow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { Liquid } = require('liquidjs'); | |
| const engine = new Liquid(); | |
| // 1. Define the tags you want to KEEP (Control Flow only) | |
| // Note: You only need to list the opening tags. 'endif', 'endfor', etc. are handled automatically. | |
| const allowedTags = [ | |
| 'if', 'else', 'elsif', 'unless', | |
| 'for', 'break', 'continue', 'limit', 'offset', | |
| 'case', 'when', | |
| 'comment', 'raw' // 'raw' is useful to escape Liquid syntax, 'comment' is harmless | |
| ]; | |
| Object.keys(engine.tags).forEach(tagName => { | |
| if (!allowedTags.includes(tagName)) { | |
| // Overwrite with a tag that throws an error | |
| engine.registerTag(tagName, { | |
| parse: function(token) { | |
| // This could be configured like this in a validation step | |
| throw new Error(`Tag "{% ${token.name} %}" is not allowed.`); | |
| // This could be the behavior in render service | |
| return ''; | |
| } | |
| }); | |
| } | |
| }); | |
| const template = ` | |
| {% assign x = 10 %} {% if true %} | |
| This works! | |
| {% endif %} | |
| {% include 'secret.liquid' %} `; | |
| // Render | |
| engine.parseAndRender(template).then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment