我無法在我的降價文件中重複使用模板代碼。 例如,我想拉入vimeo鏈接的嵌入代碼,並將vimeo id傳遞給調用。如何使用Nunjucks的partials/macros/includes with metalsmith-in-place?
一個例子宏:
{% macro vimeoEmbed(id) %}
<iframe src="https://player.vimeo.com/video/{{ id }}?title=0&byline=0&portrait=0" width="300" height="169" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
{% endmacro %}
要這樣來使用:
{{ vimeoEmbed(120394634) }}
這工作,如果我在降價文件直接定義宏。但當然我想要一個帶有宏的全局文件以方便維護。
我試過用Nunjucks的{% import "macros.njk" as macros %}
。 macros.njk
將包含vimeoEmbed
宏。
但不幸的是,我不斷收到Error: template names must be a string: undefined
。
作爲替代我嘗試使用{% include "vimeoEmbed.njk" %}
但我得到了相同的Error: template names must be a string: undefined
。
這似乎是特定於metalsmith-in-place
爲Nujucks' include
和import
工作只是metalsmith-layouts
罰款。
任何其他解決方案可以在Markdown文件和Nunjucks中重複使用代碼。謝謝!
您好!你是如何設法制造金屬匠和女士們一起工作的?我已經嘗試過'layouts'和'in-place',但他們在我嘗試過的大多數配置中根本沒有處理'.nj'文件。你能分享你的配置嗎?謝謝! –
確保使用'.njk'或'.nunjucks'作爲Nunjucks的擴展名。 JSTransformer根據文件擴展名自動匹配變換器。看看[這個列表](https://github.com/jstransformers/inputformat-to-jstransformer/blob/master/dictionary.json)作爲參考。 –
是的,謝謝。我通過查看源代碼瞭解了這一點。我已經寫了一篇關於這個主題的文章:https://medium.com/@slavafomin/making-metalsmith-to-work-with-nunjucks-a67a74c3e73d –