[英]How do I create a Liquid tag with a for-loop?
所以,我想為 Jekyll 主題創建一個 .rb 插件,以便能夠在 .md 文件中使用以下 Liquid 語法:
{% tab caption %}
在從 .md 文件構建網頁時,應將其轉換為:
<p><b>Tab. X.</b> Caption</p>
其中 X 是文檔中每個特定{% tab caption %}
標簽的計數; caption 是預定義 hash 中鍵的值,其中鍵與標簽中的caption
相匹配。
比如說,我在 .md 中有以下代碼:
The table below summarizes diagram symbols.
{% tab diagram %}
The table below presents the configuration options.
{% tab config %}
哪個應該返回:
The table below summarizes diagram symbols.
<p><b>Tab. 1.</b> Diagram designations.</p>
The table below presents the configuration options.
<p><b>Tab. 2.</b> Configuration options.</p>
我已經很容易地從 hash 中找出價值檢索; 但是,我不知道如何進行編號。 我假設我可以循環遍歷這個特定標簽出現的數組; 然而,我一開始並沒有成功地通過谷歌搜索來制作這樣一個數組。
感謝您的關注!
我已經想出如何做我想做的事了。 畢竟它不需要任何循環:
module Jekyll
class TabHandler < Liquid::Tag
@@count = 1
def initialize(name, input, tokens)
super
@input = input
end
def render(context)
modulename = context["modulename"].to_s
dictionary = { "io " => "I/O specifications for " + modulename,
"se " => modulename + " signal exchange",
"diagram " => "Diagram designations" }
if dictionary.has_key?(@input)
output = "<p><b>Tab. " + @@count.to_s + ".</b> " + dictionary.fetch(@input) + ".</p>"
@@count += 1
else
output = "<p><b>Tab. " + @@count.to_s + ".</b> " + @input + ".</p>"
@@count += 1
end
return output
end
end
end
Liquid::Template.register_tag('tab', Jekyll::TabHandler)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.