When you build an extension app, you often need to add something to every page without editing core templates. Examples are a support chat widget, an analytics snippet, a global CSS variable block or a keyboard shortcut handler. Editing templates/index.html would make your change a fork. Horilla‘s asset registry avoids that by letting your app register JS files and HTML fragments from its own code.

How it works

The base layout, templates/index.html, has four named slots that render whatever apps have registered:

SlotWhere it rendersTypical use
head_startFirst thing in <head>, before theme initializationBlocking scripts that must run early
head_endEnd of <head>Stylesheets, meta tags, config blocks
body_startRight after <body> opensEarly body initialization
body_endAfter all scripts, before </body>Widgets and deferred scripts

Registered JS files are separate. They are loaded as <script> tags near the end of the body, after Horilla’s own scripts, in the order they were registered.

The code lives in horilla/registry/asset_registry.py and has two public functions:

  • register_js(static_paths) registers one or more static JavaScript files.
  • register_html(template_path, slot, priority=50, page=”base”) registers a template fragment for a slot.

Part 1: Register a JavaScript file

Say your extension app is called my_extension, and you have this file:

my_extension/
└── static/
    └── my_extension/
        └── assets/
            └── js/
                └── hello.js
// hello.js
document.addEventListener("DOMContentLoaded", () => {
    console.log("my_extension loaded");
});

There are two ways to register it.

Horilla apps extend AppLauncher. Set js_files on it, and the file is registered when the app starts. The built-in keyboard shortcuts app (horilla.contrib.keys) does exactly this:

# my_extension/apps.py
from horilla.apps import AppLauncher




class MyExtensionConfig(AppLauncher):
    default = True
    name = "my_extension"
    label = "my_extension"


    js_files = "my_extension/assets/js/hello.js"

js_files accepts a single string or a list of strings. The path is relative to your static directory.

Option B: Call register_js yourself

# my_extension/registration.py
from horilla.registry.asset_registry import register_js


register_js([
    "my_extension/assets/js/hello.js",
    "my_extension/assets/js/widget.js",
])

Duplicate paths are ignored, so registering twice is safe.

A registration.py file only runs if the app lists it in auto_import_modules:

auto_import_modules = [“registration”]

Part 2: Register an HTML fragment

Create a template that contains only the fragment, not a full page:

my_extension/templates/inject_html/support_widget.html

{% load static %}
<script>
    window.SUPPORT_CONFIG = { userId: "{{ request.user.id|default:0 }}" };
</script>
<script src="{% static 'my_extension/assets/js/support_widget.js' %}"></script>

Then register it in registration.py:

# my_extension/registration.py

register_html(
    "inject_html/support_widget.html",
    slot="body_end",
    priority=60,
)

Make sure registration is in auto_import_modules on your AppLauncher, as shown above. The fragment is rendered with a plain {% include %}, so it has the page’s normal template context, including request.

Controlling the order with priority

Fragments in the same slot render in ascending priority. The default is 50, and lower numbers render first. Core apps use it like this:

FragmentSlotPriority
Login Tailwind confighead_end50
RTL assets (core)head_end90
Jalali assets (horilla_jalali)head_end95
Tailwind dynamic config (theme)head_end100

If your fragment depends on another one, give it a higher number so it renders after.

Targeting the login page

By default, a fragment goes on the main layout (page=”base”). The login page has its own template and asks for page=”login” fragments instead. The theme app uses this to give the login page its own Tailwind config:

register_html(
    "inject_html/tailwind_dynamic_config_login.html",
    slot="head_end",
    priority=50,
    page="login",
)

To show a fragment on both pages, register it once for each page, as the core app does for its RTL assets. Passing page=”*” is also accepted by the lookup, but the explicit double registration is the pattern core uses.

Note that registered JS files (Part 1) only load on the main layout, not on the login page. If you need a script on the login page, put it in a fragment registered with page=”login”.

Part 3: Make it conditional

A fragment is an ordinary Django template, so you can wrap it in {% if %}. The Jalali calendar extension (horilla_jalali) only loads its scripts when the user’s company uses that calendar:

{% load static horilla_jalali_tags %}
{% uses_jalali_calendar as jalali %}
{% if jalali %}
    <script>document.body.dataset.useJalaliCalendar = "true";</script>
    <script src="{% static 'horilla_jalali/js/jalalidatepicker.min.js' %}"></script>
{% endif %}

This is a good pattern. The registration is global and static, and the template decides at render time whether anything is output.

Common mistakes

  • Invalid slot. register_html raises a ValueError for anything other than head_start, head_end, body_start or body_end.
  • Fragment is never rendered. Check that registration is in auto_import_modules, and that your app is in INSTALLED_APPS. If the module can’t be imported, AppLauncher only logs a warning, so nothing visibly fails.
  • Wrong template path. The path is resolved by Django’s template loader, so it’s relative to a templates folder. Use a unique subfolder such as inject_html/ or my_extension/inject_html/, so it can’t collide with another app’s file.
  • Static path with a leading slash. Use my_extension/assets/js/hello.js, not /static/….
  • Script runs too early. Fragments in head_start and head_end run before the body exists. Wrap DOM work in DOMContentLoaded, or use body_end.

Conclusion

  1. Put your JS under static/<app>/… and set js_files on your AppLauncher.
  2. Put HTML fragments under templates/inject_html/ and register them with register_html(template, slot, priority, page).
  3. Add registration to auto_import_modules.
  4. Use priority to order fragments and page=”login” to target the login screen.
  5. Use template conditions to switch a fragment on or off.

You’ve now seen how to add behaviour to every page without editing a core template. Together with the other extension posts in this series, that covers a lot of what a Horilla extension app needs.

Share this article