Tags
outlet and add
Let components add styles and scripts to the page's head.
outlet marks a place in the page that any template can add to, and add to adds content there. Use them so a component can bring its own styles and scripts, and they end up in the page's <head>.
page.kiln
{% extends "layouts/base" %}
{% set title = "Home" %}
{% block content %}
{% include "components/card" with { title: "One" } %}
{% include "components/card" with { title: "Two" } %}
{% endblock %}
layouts/base.kiln
<html>
<head>
<title>{{ title }}</title>
{% outlet "head" %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
components/card.kiln
{% props title %}
{% add to "head" once %}
<link rel="stylesheet" href="/card.css">
{% endadd %}
<article class="card">{{ title }}</article>
Output
<html>
<head>
<title>Home</title>
<link rel="stylesheet" href="/card.css">
</head>
<body>
<article class="card">One</article>
<article class="card">Two</article>
</body>
</html>
Adding content
- Content goes to the outlet with the same name, in the order it was added. It doesn't matter whether
add toruns before or after the outlet. - The content can read the variables around it. Variables you
setinside it stay inside it. - Content for an outlet the page doesn't have is dropped. A component still works on a page without that outlet, and when you render one block.
Adding once
once adds the content only the first time that tag runs in a render, in the order of the page. A component used many times on a page adds its styles once.
{% add to "head" once %}
<script type="module" src="/tabs.js"></script>
{% endadd %}
From your code
A withRender function or a host component can add to an outlet with addTo. The HTML is added as it is, so escape anything it holds from your data. It follows the same rules as add to, where the call happens.
const datePicker = withRender((render) => {
render.addTo("head", '<script type="module" src="/date-picker.js"></script>', {
once: "date-picker",
});
return safe("<date-picker></date-picker>");
});
A once key is yours to choose. Keys from your code and add to once tags never clash.
Rules
- Each outlet name can be used once per render.
- Outlet names are plain strings, in both tags.
- An outlet can't be inside
cache,set,applyor a macro, because its content is only known when the render ends. - Content added inside
cacheis saved with the section, and added again when the saved copy is used. flushcloses the outlets above it. Adding content to a closed outlet is an error.