Kamran224
Kamran224

Reputation: 1614

Append to global variable inside Jekyll includes

My Jekyll project has some include files that require specific scripts. I want to be able to have them handle including those scripts instead of doing it in the template or post level.

I have a global footer include which adds all scripts in a usemin block to eventually be minified into a single file

<!-- build:js(app) scripts{{page.id}}.js -->
  {% for script in page.scripts %}
    <script src="scripts/{{script}}.js"></script>
  {% endfor %}
<!-- endbuild -->

The post may or may not include a 'script' variable in it's YAML declaration. The issue I've having is appending to an array from inside my include file.

{% assign page.scripts = page.scripts | append: ["new-script"] %}

I'm not sure I'm using the right approach since I don't think YAML declared variables can be modified.

Upvotes: 2

Views: 681

Answers (1)

David Jacquel
David Jacquel

Reputation: 52809

First you copy page.scripts if it exists

{% if page.scripts %}
  {% assign pageScripts = page.scripts %}
{% endif %}

You can then append

{% assign pageScripts = pageScripts | push: "new-script" %}

Or prepend to your array

{% assign pageScripts = pageScripts | unshift: "new-script" %}

Upvotes: 1

Related Questions