Go to page content

url

Generate the url for a named dispatch rule.

The {% url %} tag generates an absolute url (i.e. an absolute path without hostname) for a named dispatch rule and arguments.  In this way it is possible to automatically change the generated urls when the dispatch rules are modified.

For example to generate the url to the admin for editing a page (resource), use:

{% url admin_edit_rsc id=myid %}

Assuming myid is 42 then this will generate (on most Zotonic sites) the url “/admin/edit/42”.  The name “admin_edit_rsc” can be found in the dispatch rules of mod_admin.  Which dispatch rules are available depends on which modules are enabled.

When the dispatch rule named in the first argument is unknown then an empty string is returned.  There is no error message. This is to prevent breaking the web site when modules are enabled or disabled.

Arguments not named in the path of the dispatch rule are added to the query string of the returned url:

{% url admin_edit_rsc id=42 foo="bar" %}

Returns the url “/admin/edit/42?foo=bar”.

Please note that the best way to generate the url of a page (resource) is to use:

{{ m.rsc[myid].page_url }}

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

Comments

  • avatar

    Konstantin Nikiforov

    Posted 1 year, 4 months ago.

    If i have wildcart dispatch rule like:
    {arule, ["arule", p1, '*'], some_resource, []}
    How can i define a link to some_resource?
    Tag {% url arule p1="value" %} does nothing.

  • avatar

    Marc Worrell

    Posted 1 year, 3 months ago.

    You will also need to add 'star', like in:

    {% url arule p1="foo" star="bar" %}

    Otherwise the patterns don't match.