mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Condense and improve examples
This commit is contained in:
@@ -22,7 +22,7 @@ Liquid code can be categorized into [**objects**](#objects), [**tags**](#tags),
|
||||
{{ page.title }}
|
||||
```
|
||||
|
||||
In this case, Liquid is rendering the content of an object called `page.title`, and that object contains the text `Introduction`.
|
||||
In this case, Liquid is rendering the content of the `title` property of the `page` object, which contains the text `{{ page.title }}`.
|
||||
|
||||
## Tags
|
||||
|
||||
@@ -41,7 +41,7 @@ The markup used in tags does not produce any visible text. This means that you c
|
||||
|
||||
<p class="code-label">Output</p>
|
||||
```text
|
||||
Hello Adam!
|
||||
Hello Adam!
|
||||
```
|
||||
|
||||
Tags can be categorized into three types:
|
||||
|
||||
@@ -13,32 +13,32 @@ In programming, anything that returns `true` in a conditional is called **truthy
|
||||
|
||||
All values in Liquid are truthy except `nil` and `false`.
|
||||
|
||||
In the example below, the string "Tobi" is not a boolean, but it is truthy in a conditional:
|
||||
In the example below, the text "Tobi" is not a boolean, but it is truthy in a conditional:
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{% assign tobi = "Tobi" %}
|
||||
{% assign name = "Tobi" %}
|
||||
|
||||
{% if tobi %}
|
||||
This condition will always be true.
|
||||
{% if name %}
|
||||
This text will always appear since "name" is defined.
|
||||
{% endif %}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
[Strings]({{ "/basics/types/#string" | prepend: site.baseurl }}), even when empty, are truthy. The example below will result in empty HTML tags if `settings.fp_heading` is empty:
|
||||
[Strings]({{ "/basics/types/#string" | prepend: site.baseurl }}), even when empty, are truthy. The example below will create empty HTML tags if `page.category` exists but is empty:
|
||||
|
||||
<p class="code-label">Input</p>
|
||||
```liquid
|
||||
{% raw %}
|
||||
{% if settings.fp_heading %}
|
||||
<h1>{{ settings.fp_heading }}</h1>
|
||||
{% if page.category %}
|
||||
<h1>{{ page.category }}</h1>
|
||||
{% endif %}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
<p class="code-label">Output</p>
|
||||
```html
|
||||
<h1></h1>
|
||||
<h1></h1>
|
||||
```
|
||||
|
||||
## Falsy
|
||||
|
||||
+1
-1
@@ -95,7 +95,7 @@ To access all the items in an array, you can loop through each item in the array
|
||||
|
||||
<p class="code-label">Output</p>
|
||||
```text
|
||||
Tobi Laura Tetsuro Adam
|
||||
Tobi Laura Tetsuro Adam
|
||||
```
|
||||
|
||||
### Accessing specific items in arrays
|
||||
|
||||
+15
-9
@@ -23,19 +23,20 @@ Notice the blank line before "tomato" in the rendered template:
|
||||
{{ my_variable }}
|
||||
```
|
||||
|
||||
By including hyphens in your `assign` tag, you can strip the generated whitespace from the rendered template:
|
||||
By including a hyphen in your `assign` closing delimiter, you can strip the whitespace following it from the rendered template:
|
||||
|
||||
<p class="code-label">Input</p>
|
||||
```liquid
|
||||
{% raw %}
|
||||
{%- assign my_variable = "tomato" -%}
|
||||
{% assign my_variable = "tomato" -%}
|
||||
{{ my_variable }}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
<p class="code-label">Output</p>
|
||||
```text
|
||||
tomato
|
||||
{% assign my_variable = "tomato" -%}
|
||||
{{ my_variable }}
|
||||
```
|
||||
|
||||
If you don't want any of your tags to print whitespace, as a general rule you can add hyphens to both sides of all your tags (`{% raw %}{%-{% endraw %}` and `{% raw %}-%}{% endraw %}`):
|
||||
@@ -45,7 +46,7 @@ If you don't want any of your tags to print whitespace, as a general rule you ca
|
||||
{% raw %}
|
||||
{% assign username = "John G. Chalmers-Smith" %}
|
||||
{% if username and username.size > 10 %}
|
||||
Wow, {{ username }}, you have a long name!
|
||||
Wow, {{ username }} , you have a long name!
|
||||
{% else %}
|
||||
Hello there!
|
||||
{% endif %}
|
||||
@@ -56,7 +57,7 @@ If you don't want any of your tags to print whitespace, as a general rule you ca
|
||||
```text
|
||||
{% assign username = "John G. Chalmers-Smith" %}
|
||||
{% if username and username.size > 10 %}
|
||||
Wow, {{ username }}, you have a long name!
|
||||
Wow, {{ username }} , you have a long name!
|
||||
{% else %}
|
||||
Hello there!
|
||||
{% endif %}
|
||||
@@ -65,16 +66,21 @@ If you don't want any of your tags to print whitespace, as a general rule you ca
|
||||
<p class="code-label">Input</p>
|
||||
```liquid
|
||||
{% raw %}
|
||||
{%- assign username = "John G. Chalmers-Smith" -%}
|
||||
{% assign username = "John G. Chalmers-Smith" -%}
|
||||
{%- if username and username.size > 10 -%}
|
||||
Wow, {{ username }}, you have a long name!
|
||||
Wow, {{ username -}} , you have a long name!
|
||||
{%- else -%}
|
||||
Hello there!
|
||||
{%- endif -%}
|
||||
{%- endif %}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
<p class="code-label">Output with whitespace control</p>
|
||||
```text
|
||||
Wow, John G. Chalmers-Smith, you have a long name!
|
||||
{% assign username = "John G. Chalmers-Smith" -%}
|
||||
{%- if username and username.size > 10 -%}
|
||||
Wow, {{ username -}} , you have a long name!
|
||||
{%- else -%}
|
||||
Hello there!
|
||||
{%- endif %}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user