捕获一个液体变量然后分配给nil有什么作用? [英] What does capturing a liquid variable then assigning to nil do?

查看:101
本文介绍了捕获一个液体变量然后分配给nil有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jekyll引导程序包含文件_includes/JB/setup:

Jekyll bootstrap includes the file _includes/JB/setup:

{% capture jbcache %}
  <!--
  - Dynamically set liquid variables for working with URLs/paths
  -->
  {% if site.JB.setup.provider == "custom" %}
    {% include custom/setup %}
  {% else %}
    {% if site.safe and site.JB.BASE_PATH and site.JB.BASE_PATH != '' %}
      {% assign BASE_PATH = site.JB.BASE_PATH %}
      {% assign HOME_PATH = site.JB.BASE_PATH %}
    {% else %}
      {% assign BASE_PATH = nil %}
      {% assign HOME_PATH = "/" %}
    {% endif %}

    {% if site.JB.ASSET_PATH %}
      {% assign ASSET_PATH = site.JB.ASSET_PATH %}
    {% else %}
      {% capture ASSET_PATH %}{{ BASE_PATH }}/assets/themes/{{ page.theme.name }}{% endcapture %}
    {% endif %}  
  {% endif %}
{% endcapture %}{% assign jbcache = nil %}

我知道这1)将文本捕获为变量,然后2)立即将其赋值为nil,有效地将其丢弃.那这是做什么的呢?

I understand that this 1) captures the text as a variable then 2) assigns it to nil immediately, effectively throwing it away. So what does this do?

推荐答案

因为您想要渲染的副作用,但又不想渲染的输出.如果您捕获,则输出渲染的内容.但是您实际上并不需要输出,因此在完成后将其丢弃.这是一个小小的黑客.

Because you want the side-effects of rendering but don't want the rendered output. If you don't capture, the rendered content is output. But you don't actually want the output, so you throw it away when you're done. It's a slight hack.

因此,如果要在不输出结果的情况下进行计算,则捕获变量是合理的做法. 然后分配为零" hack是一种表示我们对渲染计算(而不是输出)的副作用感兴趣的方式.其他assign保留的持久性即使在抛出该变量时也具有持久的效果.

So if you want to compute without outputting the result, capturing in a variable is a reasonable thing to do. The "then assign to nil" hack is a way of saying we are interested in the side-effects of the rendering computation, not the output. Those other assignments persist have effects that persist even when the variable is thrown out.

{%include custom/setup %}的输出将类似地被丢弃,但是其副作用可能很重要.

The {%include custom/setup %}'s output will similarly be thrown away, but its side-effects may be important.

这篇关于捕获一个液体变量然后分配给nil有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆