### Development Setup
Source: https://github.com/moetelo/twiggy/blob/master/packages/vscode/README.md
Steps to set up the development environment for Twiggy.
```shell
bun install
```
--------------------------------
### Install Twiggy Extension
Source: https://github.com/moetelo/twiggy/blob/master/packages/vscode/README.md
Instructions for installing the Twiggy extension in VS Code.
```shell
ext install moetelo.twiggy
```
--------------------------------
### Issue #18 example
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/tests.txt
Example demonstrating a comparison using 'same as' for an email UUID, likely related to issue #18.
```twig
{% if app.request.query.get('email') is same as email.uuid %}open{% endif %}
```
```tree-sitter-twig
(template
(if
(binary_expression
(call_expression
(member_expression
(member_expression
(member_expression
(variable)
(property))
(property))
(property))
(arguments
(string)))
(member_expression
(variable)
(property)))
(source_elements
(content))))
```
--------------------------------
### VS Code Setup for Symfony
Source: https://github.com/moetelo/twiggy/blob/master/README.md
Configuration steps for Symfony projects within VS Code.
```json
{
"twiggy.phpExecutable": "/path/to/php",
"twiggy.symfonyConsolePath": "/path/to/bin/console"
}
```
--------------------------------
### For loop with key and value
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Illustrates iterating over an iterable to get both the key and the value.
```twig
{% for key, user in users %}
{{ key }}: {{ user.username|e }}
{% endfor %}
```
```tree-sitter-twig
(template
(for
(variable)
(variable)
(variable)
(source_elements
(content)
(output
(variable))
(content)
(output
(filter_expression
(member_expression
(variable)
(property))
(function))) (content))))
```
--------------------------------
### Same as parenthesized filter
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/tests.txt
Example of using the 'same as' filter with parentheses in a Twig template.
```twig
{% if foo.attribute is same as(false) %}
...
{% endif %}
```
```tree-sitter-twig
(template
(if
(binary_expression
(member_expression
(variable)
(property))
(parenthesized_expression
(boolean)))
(source_elements
(content))))
```
--------------------------------
### Same as filter without parentheses
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/tests.txt
Example of using the 'same as' filter without parentheses in a Twig template.
```twig
{% if foo is same as false %}
...
{% endif %}
```
```tree-sitter-twig
(template
(if
(binary_expression
(variable)
(boolean))
(source_elements
(content))))
```
--------------------------------
### var declaration: qualified_name
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/var-comments.txt
Example of a variable declaration with a qualified name.
```twig
{# @var someVar \Foo\Bar\Baz #}
```
```tree-sitter-twig
(template
(var_declaration
(variable)
(qualified_name
(namespace
(php_identifier)
(php_identifier))
(php_identifier))))
```
--------------------------------
### var declaration: array_type
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/var-comments.txt
Example of a variable declaration with an array type.
```twig
{# @var items \Foo\Bar[] #}
```
```tree-sitter-twig
(template
(var_declaration
(variable)
(array_type
(qualified_name
(namespace
(php_identifier))
(php_identifier)))))
```
--------------------------------
### var declaration: int
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/var-comments.txt
Example of a variable declaration with a primitive type.
```twig
{# @var someVar int #}
```
```tree-sitter-twig
(template
(var_declaration
(variable)
(primitive_type)))
```
--------------------------------
### var declaration: union
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/var-comments.txt
Example of a variable declaration with a union type.
```twig
{# @var someVar int | \Foo\Bar #}
```
```tree-sitter-twig
(template
(var_declaration
(variable)
(union_type
(primitive_type)
(qualified_name
(namespace
(php_identifier))
(php_identifier)))))
```
--------------------------------
### Divisible by filter
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/tests.txt
Example of using the 'divisible by' filter in a Twig template.
```twig
{% if loop.index is divisible by(3) %}
...
{% endif %}
```
```tree-sitter-twig
(template
(if
(binary_expression
(member_expression
(variable)
(property))
(parenthesized_expression
(number)))
(source_elements
(content))))
```
--------------------------------
### var declaration: incomplete_qualified_name
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/var-comments.txt
Example of a variable declaration with an incomplete qualified name.
```twig
{# @var someVar \App\Something\ #}
```
```tree-sitter-twig
(template
(var_declaration
(variable)
(incomplete_type
(namespace
(php_identifier)
(php_identifier)))))
```
--------------------------------
### var declaration: incomplete primitive
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/var-comments.txt
Example of a variable declaration with an incomplete primitive type.
```twig
{# @var someVar str #}
```
```tree-sitter-twig
(template
(var_declaration
(variable)
(incomplete_type)))
```
--------------------------------
### var declaration: intersection
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/var-comments.txt
Example of a variable declaration with an intersection type.
```twig
{# @var someVar int & string #}
```
```tree-sitter-twig
(template
(var_declaration
(variable)
(intersection_type
(primitive_type)
(primitive_type))))
```
--------------------------------
### var declaration: incomplete_qualified_name - backslash \
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/var-comments.txt
Example of a variable declaration with an incomplete qualified name ending in a backslash.
```twig
{# @var someVar \ #}
```
```tree-sitter-twig
(template
(var_declaration
(variable)
(incomplete_type
(namespace))))
```
--------------------------------
### var declaration: incomplete_qualified_name in union
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/var-comments.txt
Example of a variable declaration with an incomplete qualified name within a union type.
```twig
{# @var someVar \App\SomeClass | \App\ #}
```
```tree-sitter-twig
(template
(var_declaration
(variable)
(union_type
(qualified_name
(namespace
(php_identifier))
(php_identifier))
(incomplete_type
(namespace
(php_identifier)))
)))
```
--------------------------------
### Importing multiple macros from a file
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Demonstrates importing multiple macros from the same Twig file.
```twig
{% from "macros.twig" import hello, world %}
```
```tree-sitter-twig
(template
(from
(string)
(variable)
(variable)))
```
--------------------------------
### Importing a macro from a file
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Shows how to import a single macro from another Twig file.
```twig
{% from "macros.twig" import hello %}
```
```tree-sitter-twig
(template
(from
(string)
(variable)))
```
--------------------------------
### Import statement
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Shows how to import all macros from a file and assign them to a namespace.
```twig
{% import "macros.twig" as macros %}
```
```tree-sitter-twig
(template
(import
(string)
(variable)))
```
--------------------------------
### Importing a macro with an alias
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Shows how to import a macro and give it a different name using the 'as' operator.
```twig
{% from "macros.twig" import input as input_field %}
```
```tree-sitter-twig
(template
(from
(string)
(as_operator
(variable)
(variable))))
```
--------------------------------
### Use
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Includes another template file.
```twig
{% use "blocks.html" %}
```
--------------------------------
### String Interpolation
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates string interpolation with variables and expressions.
```twig
{{ "foo #{ 43 } #{ bar } baz" }}
```
```tree-sitter-twig
(template
(output
(interpolated_string
(string)
(number)
(string)
(variable)
(string))))
```
--------------------------------
### Include with variables
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Shows how to pass variables to an included template.
```twig
{% include 'template.html' with {'foo': 'bar'} %}
```
```tree-sitter-twig
(template
(include
(string)
(object
(pair
(string)
(string)))))
```
--------------------------------
### Include statement
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Demonstrates how to include the content of another template file.
```twig
{% include 'header.html' %}
```
```tree-sitter-twig
(template
(include
(string)))
```
--------------------------------
### Lorem {# comment #} ipsum
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/directives.txt
A Twig template with content interspersed with a comment.
```twig
(template
(content)
(comment)
(content))
```
--------------------------------
### {# comment #} Lorem {# comment #}
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/directives.txt
A Twig template with content and comments at the beginning and end.
```twig
(template
(comment)
(content)
(comment))
```
--------------------------------
### Set several variables
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Demonstrates setting multiple variables in a single statement.
```twig
{% set foo, bar = 'foo', 'bar' %}
```
--------------------------------
### For loop with a sequence of numbers
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Demonstrates a for loop iterating from 0 to 10.
```twig
{% for i in 0..10 %}
{{ i }}
{% endfor %}
```
```tree-sitter-twig
(template
(for
(variable)
(binary_expression
(number)
(number))
(source_elements
(output
(variable)))))
```
--------------------------------
### With variables
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Provides a context with specific variables for a block of template content.
```twig
{% with { foo: 42 } %}
...
{% endwith %}
```
--------------------------------
### With
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Provides a context for a block of template content.
```twig
{% with %}
...
{% endwith %}
```
--------------------------------
### for
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Iterates over a sequence (e.g., a list of users) and outputs properties of each item.
```twig
{% for user in users %}
{{ user.username }}
{% endfor %}
```
```tree-sitter-twig
(template
(for
(variable)
(variable)
(source_elements
(output
(member_expression
(variable)
(property))))))
```
--------------------------------
### Output directive
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/directives.txt
A Twig template demonstrating the output directive with a variable.
```twig
(template
(output
(variable)))
```
--------------------------------
### Binary and unary expressions
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Illustrates a complex expression combining unary negation, addition, and multiplication.
```twig
{{ -42 + a * 'b' }}
```
```tree-sitter
(template
(output
(binary_expression
(unary_expression
(number))
(binary_expression
(variable)
(string)))))
```
--------------------------------
### Array with Spread Element
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates using the spread syntax within an array.
```twig
{{ [1, 2, ...moreNumbers] }}
```
```tree-sitter-twig
(template
(output
(array
(number)
(number)
(spread_element
(variable)))))
```
--------------------------------
### Object Property Keys as Integers
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates object properties with integer keys.
```twig
{{ { 2: 'foo', 4: 'bar' } }}
```
```tree-sitter-twig
(template
(output
(object
(pair
(number)
(string))
(pair
(number)
(string)))))
```
--------------------------------
### Set block
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Shows how to set a block of content to a variable, including outputting variables within the block.
```twig
{% set foo %}
Hello {{ username }}!
{% endset %}
```
--------------------------------
### Object with Spread Element
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates using the spread syntax within an object.
```twig
{{ { 'foo': 10, ...moreRatings } }}
```
```tree-sitter-twig
(template
(output
(object
(pair
(string)
(number))
(spread_element
(variable)))))
```
--------------------------------
### Macro definition
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Defines a reusable macro named 'input' that takes a 'name' argument.
```twig
{% macro input(name) %}
{% endmacro %}
```
```tree-sitter-twig
(template
(macro
(identifier)
(arguments
(variable))
(source_elements
(content)
(output
(variable))
(content))))
```
--------------------------------
### Include with variables and 'only' modifier
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Demonstrates including a template and passing specific variables, while ignoring others.
```twig
{% include 'template.html' with {'foo': 'bar'} only %}
```
```tree-sitter-twig
(template
(include
(string)
(object
(pair
(string)
(string)))))
```
--------------------------------
### Include with 'ignore missing' modifier
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Shows how to include a template and prevent an error if the template does not exist.
```twig
{% include 'sidebar.html' ignore missing %}
```
```tree-sitter-twig
(template
(include
(string)))
```
--------------------------------
### Call expression with arguments and named arguments
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Illustrates a function call with both positional and named arguments, including a filter.
```twig
{{ "now"|date(format: 'd/m/Y H:i', timezone="Europe/Paris") }}
```
```tree-sitter
(template
(output
(filter_expression
(string)
(function)
(arguments
(named_argument
(variable)
(string))
(named_argument
(variable)
(string))))))
```
--------------------------------
### for element in |
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Iterates over a sequence, outputting the element itself.
```twig
{% for user in %}
{{ user }}
{% endfor %}
```
```tree-sitter-twig
(template
(for
(variable)
(source_elements
(output
(variable)))))
```
--------------------------------
### Add snippets
Source: https://github.com/moetelo/twiggy/blob/master/CHANGELOG.md
Adds snippets for 'set block', 'if else', and 'block with body'.
```twig
set block
```
```twig
if else
```
```twig
block with body
```
--------------------------------
### Ternary operator
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates the standard ternary operator with a variable and two string literals.
```twig
{{ foo ? 'yes' : 'no' }}
```
```tree-sitter
(template
(output
(ternary_expression
(variable)
(string)
(string))))
```
--------------------------------
### Block shortcut
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
A shorthand for defining a block that immediately applies a filter to a variable.
```twig
{% block title page_title|title %}
```
```tree-sitter-twig
(template
(block
(identifier)
(filter_expression
(variable)
(function))))
```
--------------------------------
### Cache with key and expiration
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Defines a cache block with a key and a time-to-live (TTL) in seconds.
```twig
{% cache "cache key" ttl(300) %}
Cached for 300 seconds
{% endcache %}
```
```tree-sitter-twig
(template
(cache
(string)
(call_expression
(function)
(arguments
(number)))
(source_elements
(content))))
```
--------------------------------
### Subscript Expression with Slice
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates slicing strings and arrays using bracket notation.
```twig
{{ '123456'[1:2] }}
{{ '123456'[:-2] }}
{{ [1,2,3][2:] }}
{{ [1,2,3][:] }}
```
```tree-sitter-twig
(template
(output
(subscript_expression
(string)
(slice
(number)
(number))))
(output
(subscript_expression
(string)
(slice
(number)
(number))))
(output
(subscript_expression
(array)
(slice
(number)
null)))
(output
(subscript_expression
(array)
(slice
null
null))))
```
--------------------------------
### Use with variable
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Includes another template file and aliases its blocks.
```twig
{% use "blocks.html" with sidebar as base_sidebar, title as base_title %}
```
--------------------------------
### Do
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Executes a statement that does not return a value, like an expression.
```twig
{% do 1 + 2 %}
```
```tree-sitter-twig
(template
(do
(binary_expression
(number)
(number))))
```
--------------------------------
### Empty template
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/directives.txt
An empty Twig template.
```twig
(template)
```
--------------------------------
### With variables only
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Provides a context with specific variables for a block of template content, and only uses those variables.
```twig
{% with { foo: 42 } only %}
...
{% endwith %}
```
--------------------------------
### If-elseif-else statement
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
A comprehensive if statement with elseif and else clauses.
```twig
{% if product.stock > 10 %}
Available
{% elseif product.stock > 0 %}
Only {{ product.stock }} left!
{% else %}
Sold-out!
{% endif %}
```
```tree-sitter-twig
(template
(if
(binary_expression
(member_expression
(variable)
(property))
(number))
(source_elements
(content))
(elseif
(binary_expression
(member_expression
(variable)
(property))
(number))
(source_elements
(content)
(output
(member_expression
(variable)
(property))) (content)))
(source_elements
(content))))
```
--------------------------------
### Call expression with one argument
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Illustrates a function call with a single string argument.
```twig
{{ join(',') }}
```
```tree-sitter
(template
(output
(call_expression
(function)
(arguments
(string)))))
```
--------------------------------
### Filter multiple
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Illustrates applying multiple filters sequentially to a variable.
```twig
{{ name|striptags|title }}
```
```tree-sitter
(template
(output
(filter_expression
(filter_expression
(variable)
(function))
(function))))
```
--------------------------------
### Call expression without arguments
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Illustrates a function call with no arguments.
```twig
{{ range() }}
```
```tree-sitter
(template
(output
(call_expression
(function)
(arguments))))
```
--------------------------------
### Content one line
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/directives.txt
A Twig template with a single line of content.
```twig
(template
(content))
```
--------------------------------
### Call expression with one named argument
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates a function call with a single named argument.
```twig
{{ join(splitter=',') }}
```
```tree-sitter
(template
(output
(call_expression
(function)
(arguments
(named_argument
(variable)
(string))))))
```
--------------------------------
### Flush
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Flushes output buffer content.
```twig
{% flush %}
```
```tree-sitter-twig
(template
(flush))
```
--------------------------------
### If-elseif statement
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
An if statement with an elseif clause for multiple conditions.
```twig
{% if product.stock > 10 %}
Available
{% elseif product.stock > 0 %}
Only {{ product.stock }} left!
{% endif %}
```
```tree-sitter-twig
(template
(if
(binary_expression
(member_expression
(variable)
(property))
(number))
(source_elements
(content))
(elseif
(binary_expression
(member_expression
(variable)
(property))
(number))
(source_elements
(content)
(output
(member_expression
(variable)
(property))) (content)))))
```
--------------------------------
### If statement
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
A basic if statement to conditionally render content.
```twig
{% if online == false %}
Our website is in maintenance mode
{% endif %}
```
```tree-sitter-twig
(template
(if
(binary_expression
(variable)
(boolean))
(source_elements
(content))))
```
--------------------------------
### Empty output
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/directives.txt
Twig templates with empty output directives.
```twig
(template
(output)
(output)
(output))
```
--------------------------------
### Call expression with arguments
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows a function call with multiple numeric arguments.
```twig
{{ range(0, 3) }}
```
```tree-sitter
(template
(output
(call_expression
(function)
(arguments
(number)
(number)))))
```
--------------------------------
### Arrow Function with Arguments
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Defines an arrow function with multiple parameters.
```twig
{{ (a, b) => 42 }}
```
```tree-sitter-twig
(template
(output
(arrow_function
(parameter)
(parameter)
(number))))
```
--------------------------------
### Call expression with named arguments
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows a function call with multiple named arguments.
```twig
{{ range(low=1, high=10, step=2) }}
```
```tree-sitter
(template
(output
(call_expression
(function)
(arguments
(named_argument
(variable)
(number))
(named_argument
(variable)
(number))
(named_argument
(variable)
(number))))))
```
--------------------------------
### Tag with expressions
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
A tag with multiple arguments, including a variable and a binary expression.
```twig
{% unknown_tag arg1 2 + 3 %}
```
```tree-sitter-twig
(template
(tag
(variable)
(binary_expression
(number)
(number))))
```
--------------------------------
### Object with Computed Property Name
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Illustrates using parentheses for computed property names in objects.
```twig
{{ { (foo): 'foo' } }}
```
```tree-sitter-twig
(template
(output
(object
(pair
(computed_property_name
(variable))
(string)))))
```
--------------------------------
### Verbatim
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Outputs content literally without parsing Twig tags.
```twig
{% verbatim %}
...
{% endverbatim %}
```
--------------------------------
### None Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents the none value, which is equivalent to null.
```twig
{{ none }}
```
```tree-sitter-twig
(template
(output
(null)))
```
--------------------------------
### Variable
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents a simple variable.
```twig
{{ user }}
```
```tree-sitter-twig
(template
(output
(variable)))
```
--------------------------------
### Binary expression
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows a simple binary expression with two variables.
```twig
{{ a + b }}
```
```tree-sitter
(template
(output
(binary_expression
(variable)
(variable))))
```
--------------------------------
### For loop with an else clause
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Shows a for loop that includes an else block to execute when the iterable is empty.
```twig
{% for user in users %}
{{ user.username }}
{% else %}
no user found
{% endfor %}
```
```tree-sitter-twig
(template
(for
(variable)
(variable)
(source_elements
(output
(member_expression
(variable)
(property))))
(source_elements
(content))))
```
--------------------------------
### Filter without arguments
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates a filter applied to a variable without any arguments.
```twig
{{ name|title }}
```
```tree-sitter
(template
(output
(filter_expression
(variable)
(function))))
```
--------------------------------
### Object Property Keys as Identifiers
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows object properties where keys are identifiers (unquoted strings).
```twig
{{ { foo: 'foo', bar: 'bar' } }}
```
```tree-sitter-twig
(template
(output
(object
(pair
(identifier)
(string))
(pair
(identifier)
(string)))))
```
--------------------------------
### String Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents a string literal.
```twig
{{ 'hello' }}
```
```tree-sitter-twig
(template
(output
(string)))
```
--------------------------------
### Apply multiple filters
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Applies multiple filters sequentially to the block content, including arguments for filters.
```twig
{% apply lower|escape('html') %}
SOME TEXT
{% endapply %}
```
```tree-sitter-twig
(template
(apply
(filter_expression
(variable)
(function)
(arguments
(string)))
(source_elements
(content))))
```
--------------------------------
### Empty comment
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/directives.txt
An empty Twig comment.
```twig
(template (comment))
```
--------------------------------
### Tag empty
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
An empty tag with no arguments or content.
```twig
{% unknown_tag %}
```
```tree-sitter-twig
(template
(tag))
```
--------------------------------
### If-else statement
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
An if statement with an else block for alternative content.
```twig
{% if product.stock > 10 %}
Available
{% else %}
Sold-out!
{% endif %}
```
```tree-sitter-twig
(template
(if
(binary_expression
(member_expression
(variable)
(property))
(number))
(source_elements
(content))
(source_elements
(content))))
```
--------------------------------
### Ternary operator short
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows the short form of the ternary operator, using a variable and a string literal.
```twig
{{ foo ?: 'no' }}
```
```tree-sitter
(template
(output
(ternary_expression
(variable)
(string))))
```
--------------------------------
### Apply
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Applies a filter (e.g., 'upper') to the content within the block.
```twig
{% apply upper %}
his text becomes uppercase
{% endapply %}
```
```tree-sitter-twig
(template
(apply
(function)
(source_elements
(content))))
```
--------------------------------
### Set statement
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Assigns a value to a variable.
```twig
{% set foo = [1, 2] %}
```
```tree-sitter-twig
(template
(set
(variable)
(array
(number)
(number))))
```
--------------------------------
### Call call expression
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates a chained function call, where the result of one call is immediately called.
```twig
{{ range()() }}
```
```tree-sitter
(template
(output
(call_expression
(call_expression
(function)
(arguments))
(arguments))))
```
--------------------------------
### Comment single line
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/directives.txt
A Twig template with a single-line comment.
```twig
(template
(comment))
```
--------------------------------
### Extends
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Specifies that the current template extends another base template.
```twig
{% extends "base.twig" %}
```
```tree-sitter-twig
(template
(extends
(string)))
```
--------------------------------
### Types several declarations
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Declares types for multiple variables, including optional ones.
```twig
{% types
foo: 'string|bool',
score?: 'int',
%}
```
--------------------------------
### Cache
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Defines a cache block with a key, caching content indefinitely.
```twig
{% cache "cache key" %}
Cached forever (depending on the cache implementation)
{% endcache %}
```
```tree-sitter-twig
(template
(cache
(string)
(source_elements
(content))))
```
--------------------------------
### if empty condition (completion)
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
An empty if condition, likely for completion or syntax checking.
```twig
{% if %}{% endif %}
```
--------------------------------
### Unary expression
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates a unary expression negating a number.
```twig
{{ -42 }}
```
```tree-sitter
(template
(output
(unary_expression
(number))))
```
--------------------------------
### Extends with ternary operator
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Extends a template based on a condition using a ternary operator.
```twig
{% extends standalone ? "minimum.html" : "base.html" %}
```
```tree-sitter-twig
(template
(extends
(ternary_expression
(variable)
(string)
(string))))
```
--------------------------------
### Embed ignore missing
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Embeds a template, ignoring it if it does not exist.
```twig
{% embed "base" ignore missing %}
...
{% endembed %}
```
```tree-sitter-twig
(template
(embed
(string)
(source_elements
(content))))
```
--------------------------------
### Apply call expression
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Applies a function with arguments to the block content.
```twig
{% apply niceComicSans('issue 51') %}
{% endapply %}
```
```tree-sitter-twig
(template
(apply
(call_expression
(function)
(arguments
(string)))))
```
--------------------------------
### Tag with expression
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
A tag that includes a simple binary expression.
```twig
{% unknown_tag 2 + 3 %}
```
```tree-sitter-twig
(template
(tag
(binary_expression
(number)
(number))))
```
--------------------------------
### Autoescape string argument
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Specifies the escaping strategy ('html') as a string argument.
```twig
{% autoescape 'html' %}
Everything will be automatically escaped in this block
using the HTML strategy
{% endautoescape %}
```
```tree-sitter-twig
(template
(autoescape
(string)
(source_elements
(content))))
```
--------------------------------
### Object Property as Variable Name
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows shorthand object property definition where the key is the variable name.
```twig
{{ { foo } }}
```
```tree-sitter-twig
(template
(output
(object
(variable))))
```
--------------------------------
### Embed with variable
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Embeds a template and passes variables to it.
```twig
{% embed "base" with {'foo': 'bar'} %}
...
{% endembed %}
```
```tree-sitter-twig
(template
(embed
(string)
(object
(pair
(string)
(string)))
(source_elements
(content))))
```
--------------------------------
### Block
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Defines a named block with content.
```twig
{% block title %}Index{% endblock %}
```
```tree-sitter-twig
(template
(block
(identifier)
(source_elements
(content))))
```
--------------------------------
### Object Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents an object with string keys and values.
```twig
{{ {'foo': 'bar'} }}
```
```tree-sitter-twig
(template
(output
(object
(pair
(string)
(string)))))
```
--------------------------------
### Filter with arguments
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows a filter applied to a variable with a single string argument.
```twig
{{ name|join(', ') }}
```
```tree-sitter
(template
(output
(filter_expression
(variable)
(function)
(arguments
(string)))))
```
--------------------------------
### Empty Coalesce
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Illustrates chained empty coalescing operators with multiple variables.
```twig
{% set choice = thingOne ??? thingTwo ??? thingThree ??? thingFour %}
```
```tree-sitter
(template
(set
(variable)
(binary_expression
(variable)
(binary_expression
(variable)
(binary_expression
(variable)
(variable))))))
```
--------------------------------
### Arrow Function without Arguments
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Defines an arrow function with no parameters.
```twig
{{ () => 42 }}
```
```tree-sitter-twig
(template
(output
(arrow_function
(number))))
```
--------------------------------
### Embed
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Embeds another template, inheriting its blocks.
```twig
{% embed "teasers_skeleton.twig" %}{% endembed %}
```
```tree-sitter-twig
(template
(embed
(string)))
```
--------------------------------
### String Interpolation with Escaped Characters
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Illustrates handling of escaped characters within interpolated strings.
```twig
{{ "foo \#{} \" \n" }}
```
```tree-sitter-twig
(template
(output
(string)))
```
--------------------------------
### Fix brackets coloring
Source: https://github.com/moetelo/twiggy/blob/master/CHANGELOG.md
Fixes brackets coloring in a Twig template string.
```twig
```
--------------------------------
### Subscript expression
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Demonstrates a subscript expression accessing an element within an array using a slice.
```twig
{{ '123456'[-2:] }}
```
```tree-sitter
(template
(output
(subscript_expression
(string)
(slice
(unary_expression
(number))))))
```
--------------------------------
### if empty condition (diagnostics)
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
An empty if condition with content, likely for diagnostics.
```twig
{% if %}{% endif %}
```
--------------------------------
### Sandbox block
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Encloses code within a sandbox environment, restricting its capabilities.
```twig
{% sandbox %}
...
{% endsandbox %}
```
```tree-sitter-twig
(template
(sandbox
(source_elements
(content))))
```
--------------------------------
### Macro definition with explicit end name
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Defines a macro with an explicit closing tag that repeats the macro name.
```twig
{% macro input() %}
...
{% endmacro input %}
```
```tree-sitter-twig
(template
(macro
(identifier)
(arguments)
(source_elements
(content))
(identifier)))
```
--------------------------------
### Deprecated
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Marks a template element as deprecated with a warning message.
```twig
{% deprecated 'The "base.twig" template is deprecated, use "layout.twig" instead.' %}
```
```tree-sitter-twig
(template
(deprecated
(string)))
```
--------------------------------
### Empty Object Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents an empty object.
```twig
{{ {} }}
```
```tree-sitter-twig
(template
(output
(object)))
```
--------------------------------
### Types declaration
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Declares the type for a variable.
```twig
{% types foo: 'string|bool' %}
```
--------------------------------
### Types enclosed with braces
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Declares types for multiple variables using braces, supporting union and intersection types.
```twig
{% types {
foo: 'string|bool',
baz?: '\Iterator&\Countable',
} %}
```
--------------------------------
### Autoescape
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Enables automatic HTML escaping for all content within the block.
```twig
{% autoescape %}
Everything will be automatically escaped in this block
using the HTML strategy
{% endautoescape %}
```
```tree-sitter-twig
(template
(autoescape
(source_elements
(content))))
```
--------------------------------
### Arrow Function with Single Argument
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Defines an arrow function with a single parameter (shorthand).
```twig
{{ v => 42 }}
```
```tree-sitter-twig
(template
(output
(arrow_function
(parameter)
(number))))
```
--------------------------------
### Empty Array Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents an empty array.
```twig
{{ [] }}
```
```tree-sitter-twig
(template
(output
(array)))
```
--------------------------------
### Block with end-tag
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Defines a block with an explicit end-tag that includes the block name.
```twig
{% block sidebar %}{% endblock sidebar %}
```
```tree-sitter-twig
(template
(block
(identifier)
(identifier)))
```
--------------------------------
### Member Expression with Index
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Accessing an element of an array or object using a numeric index via dot notation.
```twig
{{ arr.0 }}
```
```tree-sitter-twig
(template
(output
(member_expression
(variable)
(property))))
```
--------------------------------
### Float Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents a floating-point number.
```twig
{{ 42.35 }}
```
```tree-sitter-twig
(template
(output
(number)))
```
--------------------------------
### Embed with variable only
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Embeds a template and passes variables, using the 'only' keyword to prevent inheritance of blocks.
```twig
{% embed "base" with {'foo': 'bar'} only %}
...
{% endembed %}
```
```tree-sitter-twig
(template
(embed
(string)
(object
(pair
(string)
(string)))
(source_elements
(content))))
```
--------------------------------
### Parenthesized Expression
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows an expression enclosed in parentheses.
```twig
{{ (42) }}
```
```tree-sitter-twig
(template
(output
(parenthesized_expression
(number))))
```
--------------------------------
### Subscript Expression
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Accessing an object property using bracket notation with a string key.
```twig
{{ user['name'] }}
```
```tree-sitter-twig
(template
(output
(subscript_expression
(variable)
(string))))
```
--------------------------------
### Null Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents the null value.
```twig
{{ null }}
```
```tree-sitter-twig
(template
(output
(null)))
```
--------------------------------
### Array with Trailing Comma
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows an array definition with a trailing comma.
```twig
{{ [1, 2,] }}
```
```tree-sitter-twig
(template
(output
(array
(number)
(number))))
```
--------------------------------
### Deeper String Interpolation
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows nested string interpolation.
```twig
{{ "foo #{"foo #{bar} baz"} baz" }}
```
```tree-sitter-twig
(template
(output
(interpolated_string
(string)
(interpolated_string
(string)
(variable)
(string))
(string))))
```
--------------------------------
### Member Expression
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Accessing a property of an object using dot notation.
```twig
{{ user.name }}
```
```tree-sitter-twig
(template
(output
(member_expression
(variable)
(property))))
```
--------------------------------
### Array Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents an array with various elements.
```twig
{{ [1, 2, 'foo', "bar"] }}
```
```tree-sitter-twig
(template
(output
(array
(number)
(number)
(string)
(string))))
```
--------------------------------
### Trans
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Marks a block of text for translation.
```twig
{% if display_submitted %}
{% trans %}
Hello
{% endtrans %}
{% endif %}
```
--------------------------------
### Deeper Member Expression
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Chaining member expressions to access nested properties.
```twig
{{ user.name.first }}
```
```tree-sitter-twig
(template
(output
(member_expression
(member_expression
(variable)
(property))
(property))))
```
--------------------------------
### Subscript expression deeper
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Shows a nested subscript expression, first accessing an element by string key, then by variable.
```twig
{{ user['name'][prop] }}
```
```tree-sitter
(template
(output
(subscript_expression
(subscript_expression
(variable)
(string))
(variable))))
```
--------------------------------
### Integer Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents an integer number.
```twig
{{ 42 }}
```
```tree-sitter-twig
(template
(output
(number)))
```
--------------------------------
### Boolean True Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents the boolean true value.
```twig
{{ true }}
```
```tree-sitter-twig
(template
(output
(boolean)))
```
--------------------------------
### Boolean False Literal
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/expressions.txt
Represents the boolean false value.
```twig
{{ false }}
```
```tree-sitter-twig
(template
(output
(boolean)))
```
--------------------------------
### Autoescape boolean argument
Source: https://github.com/moetelo/twiggy/blob/master/packages/tree-sitter-twig/corpus/statements.txt
Disables automatic escaping using a boolean argument (false).
```twig
{% autoescape false %}
Everything will be automatically escaped in this block
using the HTML strategy
{% endautoescape %}
```
```tree-sitter-twig
(template
(autoescape
(boolean)
(source_elements
(content))))
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.