You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGES.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,10 @@
1
1
# Python Liquid Change Log
2
2
3
+
## Version 2.2.0 (unreleased)
4
+
5
+
- Added the `{% snippet %}` tag.
6
+
- Improved static analysis of partial templates. Previously we would visit a partial template only once, regardless of how many times it is rendered with `{% render %}`. Now we visit partial templates once for each distinct set of arguments passed to `{% render %}`, potentially reporting "global" variables that we'd previously missed.
A snippet is a reusable block of Liquid markup. Traditionally we'd save a snippet to a file and include it in a template with the `{% render %}` tag.
554
+
555
+
```liquid
556
+
{% render "some_snippet" %}
557
+
```
544
558
545
-
<!-- md:version 0.1.0 -->
546
-
<!-- md:shopify -->
559
+
With the `{% snippet %}` tag we can define blocks for reuse inside a single template, potentially reducing the number of snippet files we need.
560
+
561
+
```liquid
562
+
{% snippet div %}
563
+
<div>
564
+
{{ content }}
565
+
</div>
566
+
{% endsnippet %}
567
+
```
568
+
569
+
Defining a snippet does not render it. We use `{% render snippet_name %}` to render a snippet, where `snippet_name` is the name of your snippet without quotes (file-based snippet names must be quoted).
570
+
571
+
```liquid
572
+
{% snippet div %}
573
+
<div>
574
+
{{ content }}
575
+
</div>
576
+
{% endsnippet %}
577
+
578
+
{% render div, content: "Some content" %}
579
+
{% render div, content: "Other content" %}
580
+
```
581
+
582
+
```html title="output"
583
+
<div>Some content</div>
584
+
585
+
<div>Other content</div>
586
+
```
587
+
588
+
Inline snippets share the same namespace as variables defined with `{% assign %}` and `{% capture %}`, so be wary of accidentally overwriting snippets with non-snippet data.
589
+
590
+
```liquid
591
+
{% snippet foo %}Hello{% endsnippet %}
592
+
{% foo = 42 %}
593
+
{% render foo %} {% # error %}
594
+
```
595
+
596
+
Snippets can be nested and follow the same scoping rules as file-based snippets.
597
+
598
+
```liquid
599
+
{% snippet a %}
600
+
b
601
+
{% snippet c %}
602
+
d
603
+
{% endsnippet %}
604
+
{% render c %}
605
+
{% endsnippet %}
606
+
607
+
{% render a %}
608
+
{% render c %} {% # error, c is out of scope %}
609
+
```
610
+
611
+
Snippet blocks are bound to their names late. You can conditionally define multiple snippets with the same name and pick one at render time.
0 commit comments