mirror of
https://github.com/semihalev/twig.git
synced 2026-03-14 13:55:46 +01:00
Update readme for new tags and filter
This commit is contained in:
parent
b44bad903b
commit
f5a2985a31
1 changed files with 62 additions and 0 deletions
62
README.md
62
README.md
|
|
@ -128,6 +128,9 @@ func main() {
|
|||
- Includes: `{% include %}`
|
||||
- Macros: `{% macro name(args) %}...{% endmacro %}`
|
||||
- Imports: `{% import "template.twig" as alias %}`
|
||||
- Selective imports: `{% from "template.twig" import macro1, macro2 as alias %}`
|
||||
- Apply filters to blocks: `{% apply filter %}...{% endapply %}`
|
||||
- Verbatim content: `{% verbatim %}...{% endverbatim %}`
|
||||
- Comments: `{# comment #}`
|
||||
- Array literals: `[1, 2, 3]`
|
||||
- Conditional expressions: `condition ? true_expr : false_expr`
|
||||
|
|
@ -170,6 +173,7 @@ This implementation supports many standard Twig filters:
|
|||
- `round`: Rounds a number
|
||||
- `striptags`: Strips HTML tags from a string
|
||||
- `nl2br`: Replaces newlines with HTML line breaks
|
||||
- `spaceless`: Removes whitespace between HTML tags
|
||||
|
||||
### Filter Usage Examples
|
||||
|
||||
|
|
@ -583,6 +587,64 @@ This is particularly useful in JavaScript blocks or when you need to include lit
|
|||
|
||||
Twig templates can have significant whitespace that affects the rendered output. This implementation supports the following mechanism for controlling whitespace:
|
||||
|
||||
### Apply Tag
|
||||
|
||||
The `apply` tag allows you to apply a filter to an entire block of template content:
|
||||
|
||||
```twig
|
||||
{% apply upper %}
|
||||
This text will be converted to uppercase.
|
||||
{{ variable }} will also be uppercase.
|
||||
{% endapply %}
|
||||
```
|
||||
|
||||
This is particularly useful for:
|
||||
- Applying transformations to both static and dynamic content
|
||||
- Handling complex HTML with the spaceless filter
|
||||
- Creating consistent text formatting across multiple lines
|
||||
- Processing content with custom filters
|
||||
|
||||
Example usages:
|
||||
|
||||
```twig
|
||||
{# Remove whitespace between HTML tags #}
|
||||
{% apply spaceless %}
|
||||
<div>
|
||||
<strong>Hello</strong>
|
||||
</div>
|
||||
{% endapply %}
|
||||
|
||||
{# Result: <div><strong>Hello</strong></div> #}
|
||||
|
||||
{# Convert content to uppercase #}
|
||||
{% apply upper %}
|
||||
Welcome, {{ user.name }}!
|
||||
{% endapply %}
|
||||
|
||||
{# Replace content #}
|
||||
{% apply replace('e', 'a') %}
|
||||
Hello there
|
||||
{% endapply %}
|
||||
{# Result: Hallo thara #}
|
||||
```
|
||||
|
||||
### Verbatim Tag
|
||||
|
||||
The `verbatim` tag allows you to output Twig syntax without it being processed:
|
||||
|
||||
```twig
|
||||
{% verbatim %}
|
||||
This {{ will not be processed }} as a variable.
|
||||
{% if statements won't be executed %}
|
||||
{% endverbatim %}
|
||||
```
|
||||
|
||||
This is useful when:
|
||||
- Displaying Twig syntax as part of documentation
|
||||
- Including template examples in your output
|
||||
- Working with JavaScript frameworks that use similar syntax (Vue.js, Angular, etc.)
|
||||
- Creating code examples that include Twig syntax
|
||||
|
||||
### Whitespace Control
|
||||
|
||||
The whitespace control modifiers (`-` character) allow you to trim whitespace around tags:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue