Replies: 2 comments 4 replies
-
Assuming you're talking about the Nunjucks Take this with the world's largest grain of salt, but my limited expertise tells me that they're pretty close, with a Here was my rough attempt at a Nunjucks ---
# src/index.njk
---
{% macro fieldMacro(name, value="", type="text") %}
<div class="field">
<input type="{{ type }}" name="{{ name }}" value="{{ value | escape }}" />
</div>
{% endmacro %}
<h2>Macro</h2>
{{ fieldMacro("macroUser") }}
{{ fieldMacro("macroPass", type="password") }}
<h2>Shortcode</h2>
{% fieldShortcode "shortcodeUser" %}
{% fieldShortcode "shortcodePass", type="password", value=("qwerty@?:&123" | escape) %} // .eleventy.js
module.exports = function (eleventyConfig) {
eleventyConfig.addShortcode("fieldShortcode", function (name, kwargs = {}) {
const defaultArgs = {
value: "",
type: "text",
};
const { value, type } = Object.assign({}, defaultArgs, kwargs);
return `
<div class="field">
<input type="${type}" name="${name}" value="${value}" />
</div>
`;
});
return {
dir: {
input: "src",
output: "www",
},
};
}; The shortcode was a bit trickier than I expected, because I had to refactor some things to support the shortcode named argument syntax (Nunjucks-only). The other trick is that the
Easy enough in a I vaguely recall a few issues and discussions around UPDATE: Pushed the example code to GitHub at https://github.com/pdehaan/11ty-nunjucks-macro-test |
Beta Was this translation helpful? Give feedback.
-
Good question! Some good discussion on this on Twitter this week via @jeromecoupe https://twitter.com/jeromecoupe/status/1419726991994068994 |
Beta Was this translation helpful? Give feedback.
-
These seem to provide fairly similar functionality, is there any situation where shortcodes would be more suited than a macro and vice versa?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions