Spanish Total.js
English
Documentation For AI agents
Contents

Sitemap

The sitemap file, SITEMAP(), the F.sitemap functions, the controller methods and @{sitemap}.

.md GitHub
On this page

API

type

id : title --> url --> [parent]

The file that describes the pages of a website.

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.

A shop
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.
ParameterTypeDescription
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.

Pages from the sitemap
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.
ParameterTypeDescription
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().

Breadcrumb
var items = SITEMAP('detail');
// [{ id: 'homepage', ... }, { id: 'products', ... }, { id: 'detail', ... }]
One URL
var url = SITEMAP('products', true).url; // '/products/'
method

F.sitemap_navigation([parent], [language])

Returns the sitemap items with one parent, for a menu.
ParameterTypeDescription
parent? String The ID of the parent.
language? String Translates localized titles and URLs.

Returns Array

Without parent, returns the top-level items. Each item has name, url, wildcard, formatname and formaturl. The list is sorted by name and cached.

Main menu
var menu = F.sitemap_navigation('homepage');
method

F.sitemap_add(line)

Adds items to the sitemap from code.
ParameterTypeDescription
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.

From a module
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.
ParameterTypeDescription
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.

In an action
function detail() {
	this.sitemap('detail');
	this.view('detail');
}
property

controller.sitemapid

The sitemap ID of the current page.

Returns String

The ID set by controller.sitemap(id), otherwise the ID of the route (ROUTE('#id')), or an empty string.

Read it
function page() {
	console.log(this.sitemapid);
	this.view('page');
}
method

controller.sitemap_navigation([parent], [language])

Returns the sitemap items with one parent.
ParameterTypeDescription
parent? String The ID of the parent. Default: controller.sitemapid
language? String The language. Default: controller.language

Returns Array

Calls F.sitemap_navigation(). The default parent is the current sitemap ID, so it lists the sub-pages of the current page. The default language is the language of the request.

In a view
<ul>
@{foreach m in sitemap_navigation('homepage')}
	<li><a href="@{m.url}">@{m.name}</a></li>
@{end}
</ul>
method

controller.sitemap_url([id], [a], [b], [c], [d], [e], [f])

Returns the URL of a sitemap item.
ParameterTypeDescription
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(...)}.

In a view
<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.
ParameterTypeDescription
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.

Language switch
<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.
ParameterTypeDescription
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(...)}.

In a view
<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.
ParameterTypeDescription
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.

Language switch
<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.
ParameterTypeDescription
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(...)}.

Product detail
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.
ParameterTypeDescription
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(...)}.

Both at once
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.
ParameterTypeDescription
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(...)}.

A category level
this.sitemap('detail');
this.sitemap_add('products', category.name, '/products/?category=' + category.id);
command

@{sitemap} @{sitemap('id')}

The breadcrumb in a view.

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.

Breadcrumb in a layout
<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>
Made in Europe