Decorating views


Decoration vs extension

Typically when you extend template files, you’ll have something like base.html which you then extend with a file like home.html.

When you have many plugins it can be desirable to extend a file while at the same time preserving its filename. This is called template decoration and can be achieved with {% extends self %}.

In this case, you are essentially overriding the file using plugin hierarchy but at the same time extending it. As such, any fixes and enhancements that happen on the plugin level are preserved and used by your app-level code as well and not overwritten forever. Also, it allows you to combine the content from several plugins – this is useful when creating an admin menu for example (see below).

Helpful examples

Here’s an example where this could be useful:

No matter how many plugins we add to our project, each can contain another admin/main_menu.html, each will extend itself with its parent, each adding the menus it wants to add to the admin panel. All is super DRY!

After compilation, we end up with something like this (comments were removed):

Outlast Web & Mobile Development (c) 2023 | Privacy Policy |