Tags
macro and import
Define small reusable snippets you call like functions.
A macro is a small piece of markup you call like a function. Define macros in a template, then import them where you need them.
page.kiln
{% import "macros/forms" as forms %}
{{ forms.input("email", type: "email") }}
{{ forms.input("name", "Ada") }}
macros/forms.kiln
{% macro input(name, value = "", type = "text") -%}
<input type="{{ type }}" name="{{ name }}" value="{{ value }}">
{%- endmacro %}
Output
<input type="email" name="email" value="">
<input type="text" name="name" value="Ada">
Importing
import gives you all of a template's macros under one name. from imports chosen macros by name, optionally with an alias:
{% import "macros/forms" as forms %}
{% from "macros/forms" import input, textarea as area %}
Imports work everywhere in the template that declares them: at the top level, and inside every block, macro and fill. They are not passed on to layouts, included templates or components, which import what they need themselves.
Macros defined in the same template can be called by name, without importing.
Arguments
- Parameters can have defaults. Missing arguments without a default are null.
- Named arguments match parameter names, so you can skip ones in the middle:
input("email", type: "email"). - Passing more arguments than the macro has parameters is an error.
- A macro only sees its arguments and globals.
- A macro can call itself, for example to render a nested menu.
Macro or component?
Use a macro for a small snippet that only needs arguments. Use a component when the caller passes markup inside it.