Sitemap
The sitemap file, SITEMAP(), the F.sitemap functions, the controller methods and @{sitemap}.
On this page
API
type
id : title --> url --> [parent]
The file that describes the pages of a website.
id : title --> url --> [parent]The file sitemap in the root of the application is loaded at start. Each line is an item: the ID, : (or a tab and :), the title, -->, the URL and optionally --> and the ID of the parent. Lines that start with # or // are comments. A title or URL in @(...) is translated with the resources. {0}…{5} are placeholders filled by sitemap_url(), sitemap_name() or sitemap_change(). A URL that ends with * covers all paths below it.
homepage : Homepage --> /
products : @(Products) --> /products/ --> homepage
detail : {0} --> /products/{0}/ --> products
docs : Docs --> /docs/* --> homepage
method
ROUTE('[METHOD] #id[/path]', [action], [flags])
Declares a route by a sitemap ID.
ROUTE('[METHOD] #id[/path]', [action], [flags])| Parameter | Type | Description |
|---|---|---|
id |
String |
The sitemap ID with #, optionally with an HTTP method before it. |
action? |
Function|String |
An action or a view name. |
flags? |
Array |
Route flags. |
Returns undefined
The URL comes from the sitemap item. A path after the ID is appended (#detail/{id}/), and a wildcard item gets *. Without an action, the view with the name of the ID is rendered (index for the URL /); with a string, that view. In both cases the breadcrumb and the page title are set from the sitemap. In your own action call this.sitemap() to do it. An item with a localized URL creates a route for each language. An unknown ID throws Sitemap item "id" not found.
exports.install = function() {
ROUTE('#homepage');
ROUTE('#products', 'products/list');
ROUTE('GET #detail', detail);
};
function detail(id) {
this.sitemap();
this.sitemap_change('detail', 'name', id);
this.sitemap_change('detail', 'url', id);
this.view('products/detail');
}
method
SITEMAP(id, [first], [language])
Returns the breadcrumb of a sitemap item, or the item itself.
SITEMAP(id, [first], [language])| Parameter | Type | Description |
|---|---|---|
id |
String |
The sitemap ID. |
first? |
Boolean |
Returns only the item. Default: false |
language? |
String |
Translates localized titles and URLs. |
Returns Array|Object The breadcrumb, or one item.
Without first, returns the items from the root to id. Each item has id, name, url, sitemap (the requested ID), index (0 for the requested item), first (a root item), last and selected (the requested item), wildcard, formatname and formaturl (the text has placeholders). With first: true, returns only the item; an unknown ID gives an item with an empty url. A string as the second argument is the language. Results are cached. Alias: F.sitemap().
var items = SITEMAP('detail');
// [{ id: 'homepage', ... }, { id: 'products', ... }, { id: 'detail', ... }]
var url = SITEMAP('products', true).url; // '/products/'
method
F.sitemap_add(line)
Adds items to the sitemap from code.
F.sitemap_add(line)| Parameter | Type | Description |
|---|---|---|
line |
String|Array |
One line or an array of lines. |
Returns undefined
Each line has the format of the sitemap file. Existing IDs are replaced. Add items before you declare routes that use them.
F.sitemap_add(['blog : Blog --> /blog/ --> homepage', 'post : {0} --> /blog/{0}/ --> blog']);
method
controller.sitemap([id])
Loads the breadcrumb of the page into the controller.
controller.sitemap([id])| Parameter | Type | Description |
|---|---|---|
id? |
String|Array |
A sitemap ID, or your own breadcrumb. |
Returns Array The breadcrumb.
With an ID, copies the breadcrumb SITEMAP(id) into the repository, where views read it as sitemap, and sets the title to the name of the last item when there is no title yet. Without an argument, returns the current breadcrumb and loads the one of the route's sitemap ID on the first call. With an array, uses that array as the breadcrumb and returns the controller.
function detail() {
this.sitemap('detail');
this.view('detail');
}
property
controller.sitemapid
The sitemap ID of the current page.
controller.sitemapidReturns String
The ID set by controller.sitemap(id), otherwise the ID of the route (ROUTE('#id')), or an empty string.
function page() {
console.log(this.sitemapid);
this.view('page');
}
method
controller.sitemap_url([id], [a], [b], [c], [d], [e], [f])
Returns the URL of a sitemap item.
controller.sitemap_url([id], [a], [b], [c], [d], [e], [f])| Parameter | Type | Description |
|---|---|---|
id? |
String |
The sitemap ID. Default: controller.sitemapid |
a? |
Object |
The value for {0}, then b for {1} and so on. |
Returns String
The URL is translated to the language of the request and its placeholders {0}…{5} are filled with the other arguments. The default ID is the current one. Returns an empty string for an unknown ID. Also available in views as @{sitemap_url(...)}.
<a href="@{sitemap_url('detail', m.id)}">@{sitemap_name('detail', m.name)}</a>
method
controller.sitemap_url2(language, id, [a], [b], [c], [d], [e], [f])
Returns the URL of a sitemap item in another language.
controller.sitemap_url2(language, id, [a], [b], [c], [d], [e], [f])| Parameter | Type | Description |
|---|---|---|
language |
String |
The language. |
id |
String |
The sitemap ID. Empty means the current one. |
a? |
Object |
The value for {0} and so on. |
Returns String
Works as sitemap_url() with the language as the first argument, for example for links to other language versions.
<a href="@{sitemap_url2('de', '')}">Deutsch</a>
method
controller.sitemap_name([id], [a], [b], [c], [d], [e], [f])
Returns the title of a sitemap item.
controller.sitemap_name([id], [a], [b], [c], [d], [e], [f])| Parameter | Type | Description |
|---|---|---|
id? |
String |
The sitemap ID. Default: controller.sitemapid |
a? |
Object |
The value for {0}, then b for {1} and so on. |
Returns String
The title is translated to the language of the request and its placeholders {0}…{5} are filled with the other arguments. The default ID is the current one. Returns an empty string for an unknown ID. Also available in views as @{sitemap_name(...)}.
<a href="@{sitemap_url('detail', m.id)}">@{sitemap_name('detail', m.name)}</a>
method
controller.sitemap_name2(language, id, [a], [b], [c], [d], [e], [f])
Returns the title of a sitemap item in another language.
controller.sitemap_name2(language, id, [a], [b], [c], [d], [e], [f])| Parameter | Type | Description |
|---|---|---|
language |
String |
The language. |
id |
String |
The sitemap ID. Empty means the current one. |
a? |
Object |
The value for {0} and so on. |
Returns String
Works as sitemap_name() with the language as the first argument, for example for links to other language versions.
<a href="@{sitemap_name2('de', '')}">Deutsch</a>
method
controller.sitemap_change(id, property, value, [b], [c], [d], [e], [f])
Changes one property of an item in the current breadcrumb.
controller.sitemap_change(id, property, value, [b], [c], [d], [e], [f])| Parameter | Type | Description |
|---|---|---|
id |
String |
The ID of the item in the breadcrumb. |
property |
String |
Usually name or url. |
value |
Object|Function |
The new value, the first placeholder value, or a function. |
b? |
Object |
Values for {1} and so on. |
Returns Array The breadcrumb.
Works on a copy for this request only. For name and url with placeholders, value and the next arguments fill {0}…{5}; otherwise the property gets value. A function receives the current value and returns the new one. When the changed name was the page title, the title changes too. In a view: @{sitemap_change(...)}.
function detail(id) {
var self = this;
self.sitemap('detail');
self.sitemap_change('detail', 'name', 'Book ' + id);
self.sitemap_change('detail', 'url', id);
self.view('detail');
}
method
controller.sitemap_replace(id, title, url)
Replaces the title and the URL of an item in the current breadcrumb.
controller.sitemap_replace(id, title, url)| Parameter | Type | Description |
|---|---|---|
id |
String |
The ID of the item. |
title |
String|Function |
The new title. |
url |
String|Function |
The new URL. |
Returns Array The breadcrumb.
Each of title and url can be a string or a function that receives the current value. A string fills the placeholder {0} when the item has one. Empty values are not changed. The page title follows the change. In a view: @{sitemap_replace(...)}.
this.sitemap('detail');
this.sitemap_replace('detail', product.name, product.slug);
method
controller.sitemap_add(parent, name, url)
Inserts an item into the current breadcrumb.
controller.sitemap_add(parent, name, url)| Parameter | Type | Description |
|---|---|---|
parent |
String |
The ID of the item after which the new one goes. |
name |
String |
The title. |
url |
String |
The URL. |
Returns Array The breadcrumb.
The new item is placed after the item with the ID parent. Nothing changes when parent is not in the breadcrumb. In a view: @{sitemap_add(...)}.
this.sitemap('detail');
this.sitemap_add('products', category.name, '/products/?category=' + category.id);
command
@{sitemap} @{sitemap('id')}
The breadcrumb in a view.
@{sitemap} @{sitemap('id')}sitemap in a view is the breadcrumb loaded by controller.sitemap() or by an ID route. @{sitemap('id')} loads another one from the view and prints nothing. The controller methods sitemap_url(), sitemap_name(), sitemap_url2(), sitemap_name2(), sitemap_navigation(), sitemap_change(), sitemap_replace() and sitemap_add() are available with the same names. See Views.
<ol class="breadcrumb">
@{foreach m in sitemap}
@{if m.last}
<li>@{m.name}</li>
@{else}
<li><a href="@{m.url}">@{m.name}</a></li>
@{fi}
@{end}
</ol>