Go to page content

Defining dispatch rules

Where and how to define dispatch rules.

Dispatch rules are defined in a dispatch file. The dispatch file must be placed inside the dispatch directory of a module or the site.

A module or site can have multiple dispatch files, and they can have any filename.  Nota bene: the file may not have the extension “.erl”. Otherwise it will be compiled by the Emakefile, which will result in errors as a dispatch file is not a valid Erlang module.

The content of a dispatch file is an Erlang list of dispatch rules.

Example (from this site):

%% Dispatch rules for mod_zotonic.

[
    {home,      [],                         resource_page,  [ {template, "home.tpl"}, {id, page_home} ]},
    {features,  ["features"],               resource_page,  [ {template, "features.tpl"}, {id, page_features} ]},
    {collection,["collection", id],         resource_page,  [ {template, "collection.tpl"} ]},
    {collection,["collection", id, slug],   resource_page,  [ {template, "collection.tpl"} ]},
    {category,  ["category", id],           resource_page,  [ {template, "category.tpl"} ]},
    {category,  ["category", id, slug],     resource_page,  [ {template, "category.tpl"} ]},
    {documentation, ["documentation", id],       resource_page, [ {template, "documentation.tpl"} ]},
    {documentation, ["documentation", id, slug], resource_page, [ {template, "documentation.tpl"} ]}
].

The module indexer will load all dispatch files. They can be reloaded with the “rescan” button in the admin modules page. Illegal dispatch files are skipped, whilst showing an error during debug mode.

When your dispatch rules don't load then check first if there are any typos, then check if your dispatch rules are not overruled by a module that loads earlier. Modules are loaded on priority first, then on module name.

This page is part of the Zotonic documentation, which is licensed under the Apache License 2.0.