Date
May. 19th, 2024
 
2024年 4月 12日

Post: Concatenate arrays in liquid

Concatenate arrays in liquid

Published 12:12 Dec 06, 2015.

Created by @ezra. Categorized in #Programming, and tagged as #Jekyll.

Source format: Markdown

Table of Content

今天在使用 Jekyll 时遇到一个问题: 数组合并。

第一反应是使用 concat 过滤, 但是实际操作后并没有效果。查阅了一些 资料 发现, 虽然 Liquid 本身已经支持 concat, 但并不包括在 Liquid 核心部分中, 因此在目前 Jekyll 的稳定版本中尚未实现。

关于 concat 过滤器的示例请参见: https://help.shopify.com/themes/liquid/filters/array-filters#concat

与此同时, 解决方案至关重要:

=begin
  Jekyll filter to concatenate arrays
  Usage:
    {% raw %}{% assign result = array-1 | concatArray: array-2 %}{% endraw %}
=end
module Jekyll
  module ConcatArrays

    # copied from https://github.com/Shopify/liquid/blob/v4.0.0.rc3/lib/liquid/standardfilters.rb
    def concat(input, array)
      unless array.respond_to?(:to_ary)
        raise ArgumentError.new("concat filter requires an array argument")
      end
      InputIterator.new(input).concat(array)
    end

   class InputIterator
      include Enumerable

      def initialize(input)
        @input = if input.is_a?(Array)
          input.flatten
        elsif input.is_a?(Hash)
          [input]
        elsif input.is_a?(Enumerable)
          input
        else
          Array(input)
        end
      end

      def concat(args)
        to_a.concat(args)
      end

      def each
        @input.each do |e|
          yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
        end
      end
    end

  end
end

Liquid::Template.register_filter(Jekyll::ConcatArrays)

更多插件的相关知识请参考 官方文档

不过, 由于 Github Pages 并不支持扩展插件, 目前还没有找到太好的解决方案。

{% raw %}{% assign all_hosts = "" | split: "" %}{% endraw %}
{% raw %}{% for host in site.data.shared_hosts %}{% endraw %}
  {% raw %}{% assign all_hosts = all_hosts | push: host %}{% endraw %}
{% raw %}{% endfor %}{% endraw %}
{% raw %}{% for host in site.data.paas_hosts %}{% endraw %}
  {% raw %}{% assign all_hosts = all_hosts | push: host %}{% endraw %}
{% raw %}{% endfor %}{% endraw %}
Pinned Message
HOTODOGO
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF