jekyll调试或打印所有变量

tho*_*oth 27 ruby debugging jekyll

我想要进入Jekyll的大脑,看看发生了什么,在php中你有get_defined_vars,所以我尝试做类似的事情:

      {% for local_variable in local_variables %}
      <p> {{ local_variable }} </p><br>
      {% endfor %}
Run Code Online (Sandbox Code Playgroud)

哪个输出没什么.我努力了吗?在ruby或jekyll中有一些方法吗?我只是想四处寻找并确保一切设置正确,并可能找出我不知道的变量.

Dav*_*uel 39

使用Jekyll 2.x,您可以使用此插件.

它允许你做类似的事情{{ site | debug }}.

从Jekyll 3开始,你就拥有了{{ variable | inspect }}.


san*_*mai 23

inspect不会让你偷看变量,jsonify只有那样.

{{ variable | jsonify }}
Run Code Online (Sandbox Code Playgroud)

不需要插件.


Ale*_*ers 11

其他答案没有解决问题的“所有变量”部分。

虽然杰奇不提供get_defined_vars等效的,该文件确实申报所有可用的全局变量(它在这个时候是sitepagelayoutcontent,和paginator)。

因此,您可以使用一系列过滤器调用来调试/打印所有变量(也过滤,因为其中一些将包含 HTML):jsonify escape

<pre>
    site: {{ site | jsonify | escape }}
    page: {{ page | jsonify | escape }}
    layout: {{ layout | jsonify | escape }}
    content: {{ content | jsonify | escape }}
    paginator: {{ paginator | jsonify | escape }}
</pre>
Run Code Online (Sandbox Code Playgroud)


Flu*_*lux 5

要漂亮地打印变量,您可以将其插入到任何模板中:

<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>
Run Code Online (Sandbox Code Playgroud)

site将(in )更改{{ site | jsonify | uri_escape }}为您要打印的任何变量。

然后,您将能够在生成的页面中看到变量整齐排列。像这样的东西:

漂亮打印的 Jekyll 模板变量