如何使用 Liquid 对 Jekyll 的数据项进行分页

2024-03-23

您好,我正在尝试访问上一个和下一个数组中的元素。确切地说,我想访问页面的上一个和下一个 url。这就是我到目前为止所做的。谢谢!

{% assign page_venue = site.data.venues-array | where: "venueID",   page.venue | first %
//This outputs the current url
{{venue.url}}

这是 yml 文件的一部分:

venueID: Red-Radish
name: Red Radish
url: redradish
building: 65
neighborhood: University Union

venueID: Poly-Deli
name: Poly Deli
url: polydeli
building: 19
neighborhood: University Union

venueID: Myrons
name: Myron's
url: myrons
previous: MustangStation
building: 19
neighborhood: University Union

假设我是第二个地点(Poly-Deli),我想看到这个:
当前网址:polydeli
上一个网址: 红
下一个网址: 迈伦斯
我尝试使用以下命令输出上一个和下一个 url,但它不起作用:

<p>{{page.next.url}}</p>
<p>{{venue.next.url}}</p>
<p>{{paginate.next.url}}</p>
<p>{{paginator.next_page}}</p>

有人帮助了我,它确实有效,但它输出了整个列表。我只想输出这样的东西,因为我有 30 个数组(30 个不同的场地):
当前网址:polydeli
上一个网址: 红
下一个网址: 迈伦斯

这是输出整个列表的代码:

{% for venue in site.data.venues-array %}

  {% assign next = forloop.index0 | plus: 1 %}
  {% assign previous = forloop.index0 | minus: 1 %}
    <div>Name: {{ venue.name }}</div>
    <div>Current URL: {{ venue.url }}</div>

    <div>Previous url:{{ site.data.venues-array[previous].url }}</div>
    <div>Next URL is:{{ site.data.venues-array[next].url }}</div>
    <hr>

{% endfor %} 

正如我在之前的评论中提到的,我无法找到合适的插件来分页_data收藏。有一些可用,但都需要大量的黑客攻击,而且对于如此简单的要求来说,它们相当臃肿。

您可以将以下 HTML 和 JS 添加到场地页面的内容部分。 (即在前面的内容下面)。

HTML:

---
---
<div class="venue">

</div>

JavaScript:

<script type="text/javascript">

  /**
   * Setting for whether to keep looping or not.
   * If set to true, the first venue will show the URL of the last venue as
   * the previous venue, while the last venue will show the URL of the first
   * venue as the next one.
   *
   * If set to false, the first venue will not include a previous URL, while the last venue * * won't display the next URL.
   */
  var infinite = true

  // Gets all the venues adta and parses it as JSON.
  var venues = '{{ site.data.venues-array | jsonify }}'
  venues = JSON.parse(venues)


  // Inits the html content.
  var html = ''

  // The array index of the current venue.
  var currentVenueIndex = 0

  /**
   * Displays the current venue. Includes the previous and next links.
   *
   */
  var generateHtmlForCurrentVenue = function (venueName) {
    var venueContent = '<p>Current Venue Name: ' + venueName + '</p>' +

    getPreviousLink() + getNextLink()

    html = venueContent
  }

  /**
   * Gets the previous URL link unless we're not infinitely looping and the
   * current Venue is the first item in the array.
   */
  var getPreviousLink = function () {
    link = ''
    if (!infinite&& currentVenueIndex == 0) {
      return link
    }
    previousIndex = 0
    if (currentVenueIndex == 0) {
      previousIndex = (venues.length - 1)
    } else {
      previousIndex = (currentVenueIndex - 1)
    }

    return '<p>Previous: <a href="#" class="previous-venue">' +
      venues[previousIndex].url + '</a></p>'

  }

  /**
   * Gets the next URL link unless we're not infnitely looping and the
   * current Venue is the last item in the array.
   */
  var getNextLink = function () {
    link = ''
    if (!infinite&& currentVenueIndex >= (venues.length -1)) {
      return link
    }
    nextIndex = 0
    if (!(currentVenueIndex >= (venues.length - 1))) {
      nextIndex = (currentVenueIndex + 1)
    }
    return '<p>Next: <a href="#" class="next-venue">' +
      venues[nextIndex].url + '</a></p>'
  }


  /**
   * Shows the Previous Venue.
   */
  var showPreviousVenue = function () {
    if (currentVenueIndex == 0) {
      currentVenueIndex = (venues.length -1)
    } else {
      currentVenueIndex --
    }
    $('.venue').html('')
    generateHtmlForCurrentVenue(venues[currentVenueIndex].name)
    $('.venue').html(html)
  }

  /**
   * Shows the Next Venue.
   */
  var showNextVenue = function () {
    if (currentVenueIndex == (venues.length -1)) {
      currentVenueIndex = 0
    } else {
      currentVenueIndex ++
    }
    $('.venue').html('')
    generateHtmlForCurrentVenue(venues[currentVenueIndex].name)
    $('.venue').html(html)
  }

  /**
   * Previous venue link click event handler.
   */
  $(document.body).on('click', '.previous-venue', function (event) {
    event.preventDefault()
    showPreviousVenue()
  })


  /**
   * Next venue link click event handler.
   */
  $(document.body).on('click', '.next-venue', function (event){
    event.preventDefault()
    showNextVenue()
  })

  generateHtmlForCurrentVenue(venues[currentVenueIndex].name)

  $('.venue').html(html)

</script>

您可以通过切换来更改是否继续循环infinite变量如代码注释中所述。

请注意:

I have an older version of Jekyll on my system (v3.0.2) and thus the jsonify filter breaks when there are single quotes in the text values of the venues-array.yml. I.e. Myron's breaks and I could not escape it as shown below: enter image description here

如果你有杰基尔>= 3.2那么我相信你不会遇到这个问题,因为 Jekyll 会自动使用UTF-8在运行过滤器之前进行编码。由于客户站点需要此版本并且没有 Docker 容器,因此我无法升级我的计算机。如果您确实遇到此问题,请尝试:

1) 在 yml 文件上强制使用 UTF-8。或者 2) 清除过滤器之前的单引号或 3)不要使用单引号:)

逃避对我来说没有用。

除此之外,一切都完美,如下所示:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Liquid 对 Jekyll 的数据项进行分页 的相关文章

随机推荐