检查变量是否为液体中的字符串或数组类型 [英] Check if variable is type of string or array in liquid

查看:82
本文介绍了检查变量是否为液体中的字符串或数组类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jekyll中,您可以使用液体模板,而我正在尝试编写一个包含所有链接的导航在网站上.

In Jekyll you can use liquid template and I am trying to write a nav that includes all links in the website.

sitemap:
  home: "/"
  demo:
    right: "/right"
    left: "/left"

我要实现的目标是创建一个包含所有这些链接的侧边栏.但是某些链接在一节中.输出应为以下

What I am trying to achieve is to create a sidebar that icludes all those links. But certains links are under a section. The output should be the following

<nav>
  <ul>
    <li>
      <a href="/">home</a>
    </li>
  </ul>

  <ul>
    <li>demo</li>
    <li>
      <a href="/right">right</a>
    </li>
    <li>
      <a href="/left">left</a>
    </li
  </ul>
</nav>

并非所有部分都必须具有标题.主页链接是独立链接. 演示链接都在演示"部分.

Not all sections must have a title. The home link is a standalone link. The demo links are all in the demo section.

在流动状态下,我可以通过以下方式遍历站点地图:

In liquid I can loop through the sitemap in this way:

{% for nav in site.sitemap %}
<ul>
  <li>{{ nav[0] }}</li>
</ul>
{% endfor %}

这样,液体将打印homedemo.

我需要检查是字符串还是数组,以便打印数组或单个链接!

What I need is to check if the value is a string or an array in order to print the array or a single link!

是否可以检查液体变量是字符串还是数组? 我在以前链接的文档中找不到它!

Is there a way to check if the liquid variable is a string or an array? I can't find it in the documentation i linked before!

推荐答案

是否可以检查液体变量是字符串还是数组?

Is there a way to check if the liquid variable is a string or an array?


要检查液体变量是字符串还是元素列表(数组或哈希),请使用数组过滤器

To check if a liquid variable is a string or a list of elements (array or hash), check if it has a first descendant, using the array filter first: {% var.first %} as follow :

{% if var.first %}
  // var is not a string
{% else %}
 // var is a string
{% endif %}


过滤器first返回数组的第一个元素或哈希(如果有),如果变量为空或元素列表不返回任何内容,并且由于nil

the filter first return the first element of an array or a hash if it has one, if the variable is empty or not a list of element it return nothing, and since nil is a falsy value in liquid, it works the same as false in the if condition above.

例如:

# var1 = "a"     // string
{% var1.first %} // return:   // nil -> falsy

# var2 = [a,b,c] // array
{% var2.first %} // return: a

# var3 = {k1: a, k2: b, k3: c} // hash
{% var1.first %} // return: k1a

# var4 = {k1, k2, k3: c} // hash, first element is a key without associated value
{% var1.first %} // return: k1


现在我们可以确定元素是否为字符串,我们可以这样做:

Now that we can determine if an element is a string or not, we can do that:

{% for nav in site.sitemap %}
<ul>
  <li>
     {{ nav[0] }} :
        {% if nav[1].first %}
        // loop through: nav[1]
        {% else %}
          {{ nav[1] }}
        {% endif %}        
  </li>
</ul>
{% endfor %}

但是我发现将sitemap用作元素列表(a)更方便*,而不是单个大哈希(实际上是)(b)(请参见 YAML语法)

but I found that's more convenient* to work the sitemap as list of elements (a), instead a single big hash (as it is actually) (b) (cf. YAML syntax)

因此,我通过在每个元素之前添加"- "(破折号和空格)来修改了它的结构:

so I modified it's structure, by adding a "- " (a dash and a space) before each element:

  - home: "/"
  - demo:
    - right: "/right"
    - left: "/left"  

这给了我们 a :

{"home"=>"/"}{"demo"=>[{"right"=>"/right"}, {"left"=>"/left"}]} 

而不是 b :

{"home"=>"/", "demo"=>{"right"=>"/right", "left"=>"/left"}} 

附带说明:您可以将站点地图放在其自己的文件中,该文件位于_data/sitmap.yml中,然后通过site.data.sitemap访问它,而不用在_config.yml中将其定义为属性.保持清洁,使其外观与上面完全相同.

Side Note: you can put the sitemap in it's own file in: _data/sitmap.yml and access it by site.data.sitemap, in lieu of defined it as an attribute in the _config.yml, to keep it cleaner, thereby it'll look exactly as the above.

因为我们将递归地将sitemap本身包括在内(对于每个带有子节点的节点),所以我们将该模板放置在_includes文件夹中,而不是_layouts

since we'll going to include the sitemap in itself recursively (for each node with children ) we'll put this template in _includes folder and not _layouts

还请注意,Jekyll允许将参数传递给包含对象.但是有一个陷阱,param应该是string,而不是数组或哈希.因此,我们必须从links数组中创建一个字符串.

Notice also that Jekyll allows passing parameters to includes. But there is a catch, the param should be a string not an array, or hash. So, we have to make a string out of the links array.

我们去:

<ul>
  <!--                  links is a param -->
  {%for link in include.links %}

    <li>

      {% for part in link %}
        {{part[0]}} : <!-- part[0] : link name  -->

        {% if part[1].first %} <!-- the element has children -->

          <!-- concatenate jekyll array into a string -->
          {% assign _links = "" | split: "" %}
          {% for _link in part[1] %}
            {% assign _links = _links | push: _link %}
          {% endfor %}
          <!-- pass the string as a param to sitemap, then do the recursive dance -->
          {% include sitemap.html links = _links %}

        {% else %} <!-- no children -->

          {{part[1]}} <!-- part[1] : link url -->

        {% endif %}

      {% endfor%}

    </li>

  {%endfor%}

</ul>

3.使用模板:)

<!-- include and init the param with this ↓, or `site.sitemap` if it's defined in `_config.yml`, or ...  -->
{% include sitemap.html links= site.data.sitemap %}

4.输出

用于:/_data/sitemap.yml:

- home : "/"
- about: "/about"
- archive:
  - left : "/left"
  - right: "/right"
  - other:
    - up: '/other/up'
    - down: '/other/down'

模板产生:

  • home:/
  • 关于:/about
  • 存档:
    • left:/left
    • right:/right
    • 其他:
      • 上:/other/上
      • 下:/other/下
      • home : /
      • about : /about
      • archive :
        • left : /left
        • right : /right
        • other :
          • up : /other/up
          • down : /other/down

          所以您必须将每个链接的part[0]part[1]放在<a> tag中,例如:

          so you have to put part[0] and part[1] of each link in an <a> tag like:

          <a href="{{part[1]}}"> {{part[0]}} </a>
          

          这篇关于检查变量是否为液体中的字符串或数组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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