Updated to remove spacing from include variables
It turns out Liquid throws an error when you write `{% if {{ include.url }} %}` instead of `{% if {{include.url}} %}`. I updated the examples here to omit the spacing. To avoid inconsistency, I just omitted the spacing from all curly braces. Also added a note explaining the issue and put the blame on Liquid.
This commit is contained in:
parent
8289b61316
commit
56a7038a7e
|
@ -32,7 +32,7 @@ such as variables.
|
|||
|
||||
### Using variables names for the include file
|
||||
|
||||
The name of the file you want to embed can be specified as a variable instead of an actual file name. For example, suppose you defined a variable in your page's frontmatter like this:
|
||||
The name of the file you want to embed can be specified as a variable instead of an actual file name. For example, suppose you defined a variable in your page's front matter like this:
|
||||
|
||||
```yaml
|
||||
---
|
||||
|
@ -44,7 +44,7 @@ my_variable: footer_company_a.html
|
|||
You could then reference that variable in your include:
|
||||
|
||||
```liquid
|
||||
{% raw %}{% include {{ page.my_variable }} %}{% endraw %}
|
||||
{% raw %}{% include {{page.my_variable}} %}{% endraw %}
|
||||
```
|
||||
|
||||
In this example, the include would insert the file `footer_company_a.html` from the `_includes/footer_company_a.html` directory.
|
||||
|
@ -56,17 +56,17 @@ You can also pass parameters to an include. For example, suppose you have a file
|
|||
```liquid
|
||||
{% raw %}<div markdown="span" class="alert alert-info" role="alert">
|
||||
<i class="fa fa-info-circle"></i> <b>Note:</b>
|
||||
{{ include.content }}
|
||||
{{include.content}}
|
||||
</div>{% endraw %}
|
||||
```
|
||||
|
||||
The `{% raw %}{{ include.content }}{% endraw %}` is a parameter gets populated when you call the include and specify a value for that parameter, like this:
|
||||
The `{% raw %}{{include.content}}{% endraw %}` is a parameter that gets populated when you call the include and specify a value for that parameter, like this:
|
||||
|
||||
```liquid
|
||||
{% raw %}{% include note.html content="This is my sample note." %} {% endraw %}
|
||||
```
|
||||
|
||||
The value of `content` (which is `This is my sample note`) will be inserted into the {% raw %}`{{ include.content }}`{% endraw %} parameter.
|
||||
The value of `content` (which is `This is my sample note`) will be inserted into the {% raw %}`{{include.content}}`{% endraw %} parameter.
|
||||
|
||||
Passing parameters to includes is especially helpful when you want to hide away complex formatting from your Markdown content.
|
||||
|
||||
|
@ -85,10 +85,10 @@ You could templatize this content in your include and make each value available
|
|||
|
||||
```liquid
|
||||
{% raw %}<figure>
|
||||
<a href="{{ include.url }}">
|
||||
<img src="{{ include.file }}" style="max-width: {{ include.max-width }};"
|
||||
alt="{{ include.alt }}"/>
|
||||
<figcaption>{{ include.caption }}</figcaption>
|
||||
<a href="{{include.url}}">
|
||||
<img src="{{include.file}}" style="max-width: {{include.max-width}};"
|
||||
alt="{{include.alt}}"/>
|
||||
<figcaption>{{include.caption}}</figcaption>
|
||||
</figure>{% endraw %}
|
||||
```
|
||||
|
||||
|
@ -104,17 +104,17 @@ You could also use `if` tags to safeguard scenarios where either the parameter i
|
|||
|
||||
```liquid
|
||||
{% raw %}<figure>
|
||||
{% if {{ include.url }} %}<a href="{{ include.url }}">{% endif %}
|
||||
<img src="{{ include.file }}" {% if {{ include.max-width }} %}
|
||||
style="max-width: {{ include.max-width }}" {% endif %}
|
||||
alt="{{ include.alt }}" />{% if {{ include.url }} %}</a>{% endif %}
|
||||
{% if {{ include.caption }} %}
|
||||
<figcaption>{{ include.caption }}</figcaption>
|
||||
{% if {{include.url}} %}<a href="{{include.url}}">{% endif %}
|
||||
<img src="{{include.file}}" {% if {{include.max-width}} %}
|
||||
style="max-width: {{include.max-width}}" {% endif %}
|
||||
alt="{{include.alt}}" />{% if {{include.url}} %}</a>{% endif %}
|
||||
{% if {{include.caption}} %}
|
||||
<figcaption>{{include.caption}}</figcaption>
|
||||
{% endif %}
|
||||
</figure>{% endraw %}
|
||||
```
|
||||
|
||||
In this example, `{% raw %}if {{ include.url }}{% endraw %}` will include the `url` only if the `url` parameter is specified in the include. You could also create default values using [Liquid's default filter](https://help.shopify.com/themes/liquid/filters/additional-filters#default).
|
||||
In this example, `{% raw %}if {{include.url}}{% endraw %}` will include the `url` only if the `url` parameter is specified in the include. You could also create default values using [Liquid's default filter](https://help.shopify.com/themes/liquid/filters/additional-filters#default).
|
||||
|
||||
Here's an example that passes all the parameters to this include (the include file is named `image.html`):
|
||||
|
||||
|
@ -130,17 +130,19 @@ You can create includes that act as templates for a variety of uses — inse
|
|||
|
||||
However, note that you should avoid using too many includes, as this will slow down the build time of your site. For example, don't use includes every time you insert an image. (The above technique shows a use case for special images.)
|
||||
|
||||
Additionally, if you have a include variable inside an `if` statement, such as `{% raw %}{% if {{include.url}} %}{% endraw %}`, don't use spaces inside the curly braces. For example, avoid this syntax: `{% raw %}{% if {{ include.url }} %}{% endraw %}`. Liquid will consider this extra spacing an error. (Outside of `if` statements, the spacing for include variables doesn't matter.)
|
||||
|
||||
### Passing parameter variables to includes
|
||||
|
||||
Suppose the parameter you want to pass to the include is a variable rather than a string. For example, you might be using {% raw %}`{{ site.product_name }}`{% endraw %} to refer to every instance of your product rather than the actual hard-coded name. (In this case, your `_config.yml` file would have a key called `product_name` with a value of your product's name.)
|
||||
Suppose the parameter you want to pass to the include is a variable rather than a string. For example, you might be using {% raw %}`{{site.product_name}}`{% endraw %} to refer to every instance of your product rather than the actual hard-coded name. (In this case, your `_config.yml` file would have a key called `product_name` with a value of your product's name.)
|
||||
|
||||
The string you pass to your include parameter can't contain curly braces. For example, you can't pass a parameter that contains this: {% raw %}`"The latest version of {{ site.product_name }} is now available."`{% endraw %}
|
||||
The string you pass to your include parameter can't contain curly braces. For example, you can't pass a parameter that contains this: {% raw %}`"The latest version of {{site.product_name}} is now available."`{% endraw %}
|
||||
|
||||
If you want to include this variable in your parameter that you pass to an include, you need to store the entire parameter as a variable before passing it to the include. You can use `capture` tags to create the variable:
|
||||
|
||||
```liquid
|
||||
{% raw %}{% capture download_note %}The latest version of
|
||||
{{ site.product_name }} is now available.{% endcapture %}{% endraw %}
|
||||
{{site.product_name}} is now available.{% endcapture %}{% endraw %}
|
||||
```
|
||||
|
||||
Then pass this captured variable into the parameter for the include. Omit the quotation marks around the parameter content because it's no longer a string (it's a variable):
|
||||
|
@ -168,9 +170,9 @@ Here's an example. In the `_data` folder, suppose you have a YAML file called `p
|
|||
In the `_includes` folder, assume you have a file called `spotlight.html` with this code:
|
||||
|
||||
```liquid
|
||||
{% raw %}{% for person in {{ include.participants }} %}
|
||||
{% raw %}{% for person in {{include.participants}} %}
|
||||
{% if person.login_age == "new" %}
|
||||
{{ person.name }}
|
||||
{{person.name}}
|
||||
{% endif %}
|
||||
{% endfor %}{% endraw %}
|
||||
```
|
||||
|
@ -181,4 +183,4 @@ Now when you insert the `spotlight.html` include file, you can submit the YAML f
|
|||
{% raw %}{% include spotlight.html participants=site.data.profiles %}{% endraw %}
|
||||
```
|
||||
|
||||
In this instance, `site.data.profiles` gets inserted in place of {% raw %}`{{ include.participants }}`{% endraw %} in the include, and the Liquid logic processes. The result will be `Jane Doe`.
|
||||
In this instance, `site.data.profiles` gets inserted in place of {% raw %}`{{include.participants}}`{% endraw %} in the include, and the Liquid logic processes. The result will be `Jane Doe`.
|
||||
|
|
Loading…
Reference in New Issue