【问题标题】:Attempting to write breadcrumbs for octopress and failing尝试为 octopress 编写面包屑但失败了
【发布时间】:2012-03-29 21:18:10
【问题描述】:

到目前为止,这是我的尝试。我意识到可能还有其他方法可以做到这一点。

但是为什么我的拆分失败了?

{% capture path %}
  {{ page.url | remove: ".html" | remove_first:"/" }}
{% endcapture %}
{% capture breadcrumbs %}
  {{ path | split:'/' }}
{% endcapture %}
{% for crumb in breadcrumbs %}
  {{ crumb }} » 
{% endfor %} 

我正在将它上传到 github,却一无所获。

【问题讨论】:

    标签: github split breadcrumbs liquid


    【解决方案1】:

    我也无法让split 正常工作,但breadcrumbs are still possible。以下内容改编自我在该答案中的代码(请注意应在if 语句中修改的部分,这是可读版本,不能按预期精确工作)。

    {% capture url_parts %} {{ page.url | remove: "/index.html" | replace:'/'," " }}{% endcapture %}
    {% capture num_parts %}{{ url_parts | number_of_words }}{% endcapture %}
    {% assign previous="" %}
    {% if num_parts == "0" %}
      <Handle being at the top of the site (i.e. "site.com/") here>
    {% else %}
     {% for unused in page.content limit:num_parts %}
      {% capture first_word %}{{ url_parts | truncatewords:1 | remove:"&hellip;"}}{% endcapture %}
      {{ first_word }} &#187;
      {% capture url_parts %}{{ url_parts | remove_first:first_word }}{% endcapture %}
     {% endfor %}
    {% endif %}
    

    如果用户在/a/b/c.html,这将打印a » b » c »,如果他们在/a/b/(或等效的/a/b/index.html),它将只打印a » b »

    至少,它将接近于:对于 Markdown 文件,每次打印 first_word 之间有太多换行符,因此它们被视为单独的段落,输出将是(这在HTML 文件,但需要更多标签才能使其正常工作):

    a »
    
    b »
    
    c »
    

    这可以通过将整个for循环放在一行来解决(这是应该使用的代码):

    {% capture url_parts %} {{ page.url | remove: "/index.html" | replace:'/'," " }}{% endcapture %}
    {% capture num_parts %}{{ url_parts | number_of_words }}{% endcapture %}
    {% assign previous="" %}
    {% if num_parts == "0" %}
      <Handle being at the top of the site (i.e. "site.com/") here>
    {% else %}
     {% for unused in page.content limit:num_parts %}{% capture first_word %}{{ url_parts | truncatewords:1 | remove:"..."}}{% endcapture %}{{ first_word }} &#187;{% capture url_parts %}{{ url_parts | remove_first:first_word }}{% endcapture %}{% endfor %}
    {% endif %}
    

    (注意。for 循环中的page.content 只是为了提供一些东西来迭代,魔术是由limit:num_parts 完成的。但是,这意味着如果page.content 的段落比num_parts 少,则不会所有面包屑都会出现,如果这很可能在_config.yml 中定义一个站点变量,如breadcrumb_list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 并使用site.breadcrumb_list 作为占位符而不是page.content。(从我的其他答案中提取。))

    Here is an example(它没有使用与上面完全相同的代码,但只是做了一些小修改,并且它在HTML文件中,因此不存在新行创建段落的问题)。

    【讨论】:

    • 实际上是你在另一个线程上的回答让我首先想到了这个问题。感谢您对您的方法的进一步解释。我希望有人能解释一下 split 方法有什么问题,因为这似乎是解决问题的更自然的方法。
    • 我想你在网上没有一个例子可以看到它的实际效果吗?
    • @Toby,是的,我现在知道了,请看问题的结尾 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2014-09-21
    • 1970-01-01
    • 2019-06-24
    • 2012-01-03
    • 2021-04-09
    相关资源
    最近更新 更多