jekyll debug or print all variables
Asked Answered
C

5

49

I want to peak into Jekyll's brain and see what's going on, in php you have get_defined_vars, so I tried to do something akin to that with:

      {% for local_variable in local_variables %}
      <p> {{ local_variable }} </p><br>
      {% endfor %}

Which output nothing. Am I trying too hard? Is there some method in ruby or jekyll for this? I'd just like to poke around and make sure everything is set correctly and possibly to find out about variables I don't know about.

Commendation answered 2/12, 2015 at 16:48 Comment(0)
R
53

With Jekyll 2.x, you can use this plugin.

It allows you to do something like {{ site | debug }}.

Since Jekyll 3, you have {{ variable | inspect }}.

Readjustment answered 2/12, 2015 at 17:45 Comment(0)
I
41

inspect doesn't let you peek inside variables, where jsonify does just that.

{{ variable | jsonify }}

No plugins needed.

Please be aware that jsonify will use as much memory it needs to do its thing without any specific limits. Say, if you have hundreds of posts or pages, and you would want to jsonify them all at once, this may not work as you expect. Worst case the system may go out of RAM and became unresponsive. Exercise due caution.

Illuviation answered 16/1, 2017 at 1:4 Comment(0)
P
11

The other answers don't address the “all variables” part of the question.

Although Jekyll doesn't offer a get_defined_vars equivalent, the documentation does declare all available global variables (which at this time are site, page, layout, content, and paginator).

You can therefore debug/print all variables with a series of jsonify filter calls (filtering also with escape since some of them will contain HTML):

<pre>
    site: {{ site | jsonify | escape }}
    page: {{ page | jsonify | escape }}
    layout: {{ layout | jsonify | escape }}
    content: {{ content | jsonify | escape }}
    paginator: {{ paginator | jsonify | escape }}
</pre>
Pristine answered 9/12, 2018 at 15:17 Comment(0)
C
5

To pretty-print variables, you can insert this in any template:

<pre id="jekyll-debug"></pre>
<script>
  var obj = JSON.parse(decodeURIComponent("{{ site | jsonify | uri_escape }}"));
  var prettyJson = JSON.stringify(obj, null, 4);  // Pretty-printed JSON (indented 4 spaces).
  document.getElementById("jekyll-debug").textContent = prettyJson;
</script>

Change site (in {{ site | jsonify | uri_escape }}) to whatever variable you want to print.

You will then be able to see your variables neatly arranged in the generated pages. Something like this:

Pretty-printed Jekyll template variables

Candra answered 4/4, 2020 at 9:9 Comment(0)
S
0

For future, who see this page.

I have wrote a Package to do this type of work. In your command line, install it,

gem install 'jekyll-liquid-debug'

Then you can debug your liquid file in your terminal without building your whole site again and again.

By using,

jekyll-liquid-debug -f YOUR_LIQUID_FILE.liquid

It also has other usages, please check it more~

Sheen answered 30/9, 2020 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.