Enhanced Tree-sitter grammar for Smarty 3 template engine.
✅ Full Smarty 3 Support: All major control structures and built-in functions
✅ Dual Delimiter Support: Both {} and {{}} styles work simultaneously
✅ Smarty 2 & 3 Syntax: Supports both foreach syntaxes
✅ Complete Function Coverage: {include}, {block}, {literal}, {function}, {call}, {capture}, {nocache}
This is a fork and enhancement of Kibadda/tree-sitter-smarty with the following additions:
- {for} - For loops with step support
- {while} - While loops
- {foreachelse} - Alternative for empty foreach loops
- {literal} - Literal content (no parsing)
- {function} - Template function definitions
- {call} - Call template functions
- {capture} - Capture output to variables
- {nocache} - Disable caching for blocks
- Dual delimiter support via regex patterns (
/\{+/and/\\}+/) - Smarty 2 foreach syntax:
{foreach from=$items item=item} - Smarty 3 foreach syntax:
{foreach $array as $item} - Improved comment parsing for
{* *}and{{* *}}
local parsers = require("nvim-treesitter.parsers").get_parser_configs()
parsers.smarty = {
install_info = {
url = "https://github.com/alsur/tree-sitter-smarty",
files = { "src/parser.c" },
branch = "main",
},
filetype = "smarty", -- Optional: Associate with .tpl files
}Then follow adding queries.
Add to ~/.config/helix/languages.toml:
[language-server.smarty-language-server]
command = "smarty-language-server"
args = ["--stdio"]
[[grammar]]
name = "smarty"
source = { git = "https://github.com/alsur/tree-sitter-smarty", rev = "d3e66413061437e59788ee9d3cd8f0376497bd38" }Check your editor's Tree-sitter configuration documentation.
{{if $condition}}
{{if $nested}}
Nested content
{{elseif $other}}
ElseIf content
{{else}}
Else content
{{/if}}
{{/if}}
{{foreach $items as $item}}
{{$item.name}}
{{foreachelse}}
No items
{{/foreach}}
{{foreach from=$items item=item}}
{{item.name}}
{{/foreach}}
{{for $i=0 to 10 step 2}}
{{$i}}
{{/for}}
{{while $condition}}
Content
{{/while}}{{include file="header.tpl" assign="header"}}
{{block name="content"}}
Default content
{{/block}}
{{literal}}
<script>{$var}</script>
{{/literal}}
{{function name="link" url="#"}}
<a href="{{$url}}">{{block name="content"}}Link{{/block}}</a>
{{/function}}
{{call name="link" url="https://example.com"}}
{{block name="content"}}Visit{{/block}}
{{/call}}
{{capture name="mycap"}}
Content to capture
{{/capture}}
{{$mycap}}
{{nocache}}
{$timestamp|date_format:"%Y-%m-%d %H:%M:%S"}
{{/nocache}}{{$variable}}
{{$array.key}}
{{$variable|upper}}
{{$variable|truncate:30:"..."}}
{{$variable|default:"default value"}}{* This is a Smarty comment *}
{{* This works with double delimiters too *}}This grammar is used by the zed-smarty extension for the Zed editor.
# Parse a test file
tree-sitter parse tests/smarty/basic.tpl
# Generate parser from grammar.js
tree-sitter generate
# Run tests
tree-sitter test- Edit
grammar.js - Generate:
tree-sitter generate - Test:
tree-sitter test - Commit changes
Important: If you're using this with zed-smarty, you must update the commit SHA in zed-smarty/extension.toml after making changes.
Current version: d3e6641 (2025-01-05)
This grammar is actively maintained for use with zed-smarty. The main branch is always stable and ready to use.
- Original: Kibadda/tree-sitter-smarty
- Enhanced by: alsur
MIT
- Zed Editor - The hackable code editor
- Smarty 3 Documentation - Official documentation
- Tree-sitter - Parser generator tool