---
title: Sitemap
project: Total.js V4
version: 0.0.99
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/reference/sitemap/
updated: 2026-11-18T16:57:39.578Z
---

# Sitemap

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

## API

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

Type: `type`

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.

Example: A shop
```text
homepage : Homepage     --> /
products : @(Products)  --> /products/       --> homepage
detail   : {0}          --> /products/{0}/   --> products
docs     : Docs         --> /docs/*          --> homepage
```

### ROUTE('[METHOD] #id[/path]', [action], [flags])

Type: `method`

Declares a route by a sitemap ID.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | yes |  | The sitemap ID with `#`, optionally with an HTTP method before it. |
| `action` | `Function|String` | no |  | An action or a view name. |
| `flags` | `Array` | no |  | 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.`

Example: Pages from the sitemap
```js
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');
}
```

### SITEMAP(id, [first], [language])

Type: `method`

Returns the breadcrumb of a sitemap item, or the item itself.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | yes |  | The sitemap ID. |
| `first` | `Boolean` | no | `false` | Returns only the item. |
| `language` | `String` | no |  | 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()`.

Example: Breadcrumb
```js
var items = SITEMAP('detail');
// [{ id: 'homepage', ... }, { id: 'products', ... }, { id: 'detail', ... }]
```

Example: One URL
```js
var url = SITEMAP('products', true).url; // '/products/'
```

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

Type: `method`

Returns the sitemap items with one parent, for a menu.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `parent` | `String` | no |  | The ID of the parent. |
| `language` | `String` | no |  | 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.

Example: Main menu
```js
var menu = F.sitemap_navigation('homepage');
```

### F.sitemap_add(line)

Type: `method`

Adds items to the sitemap from code.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `line` | `String|Array` | yes |  | 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.

Example: From a module
```js
F.sitemap_add(['blog : Blog --> /blog/ --> homepage', 'post : {0} --> /blog/{0}/ --> blog']);
```

### controller.sitemap([id])

Type: `method`

Loads the breadcrumb of the page into the controller.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String|Array` | no |  | 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.

Example: In an action
```js
function detail() {
	this.sitemap('detail');
	this.view('detail');
}
```

### controller.sitemapid

Type: `property`

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.

Example: Read it
```js
function page() {
	console.log(this.sitemapid);
	this.view('page');
}
```

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

Type: `method`

Returns the sitemap items with one parent.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `parent` | `String` | no | `controller.sitemapid` | The ID of the parent. |
| `language` | `String` | no | `controller.language` | The 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.

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

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

Type: `method`

Returns the URL of a sitemap item.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | no | `controller.sitemapid` | The sitemap ID. |
| `a` | `Object` | no |  | 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(...)}`.

Example: In a view
```html
<a href="@{sitemap_url('detail', m.id)}">@{sitemap_name('detail', m.name)}</a>
```

### controller.sitemap_url2(language, id, [a], [b], [c], [d], [e], [f])

Type: `method`

Returns the URL of a sitemap item in another language.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `language` | `String` | yes |  | The language. |
| `id` | `String` | yes |  | The sitemap ID. Empty means the current one. |
| `a` | `Object` | no |  | 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.

Example: Language switch
```html
<a href="@{sitemap_url2('de', '')}">Deutsch</a>
```

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

Type: `method`

Returns the title of a sitemap item.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | no | `controller.sitemapid` | The sitemap ID. |
| `a` | `Object` | no |  | 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(...)}`.

Example: In a view
```html
<a href="@{sitemap_url('detail', m.id)}">@{sitemap_name('detail', m.name)}</a>
```

### controller.sitemap_name2(language, id, [a], [b], [c], [d], [e], [f])

Type: `method`

Returns the title of a sitemap item in another language.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `language` | `String` | yes |  | The language. |
| `id` | `String` | yes |  | The sitemap ID. Empty means the current one. |
| `a` | `Object` | no |  | 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.

Example: Language switch
```html
<a href="@{sitemap_name2('de', '')}">Deutsch</a>
```

### controller.sitemap_change(id, property, value, [b], [c], [d], [e], [f])

Type: `method`

Changes one property of an item in the current breadcrumb.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | yes |  | The ID of the item in the breadcrumb. |
| `property` | `String` | yes |  | Usually `name` or `url`. |
| `value` | `Object|Function` | yes |  | The new value, the first placeholder value, or a function. |
| `b` | `Object` | no |  | 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(...)}`.

Example: Product detail
```js
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');
}
```

### controller.sitemap_replace(id, title, url)

Type: `method`

Replaces the title and the URL of an item in the current breadcrumb.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | yes |  | The ID of the item. |
| `title` | `String|Function` | yes |  | The new title. |
| `url` | `String|Function` | yes |  | 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(...)}`.

Example: Both at once
```js
this.sitemap('detail');
this.sitemap_replace('detail', product.name, product.slug);
```

### controller.sitemap_add(parent, name, url)

Type: `method`

Inserts an item into the current breadcrumb.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `parent` | `String` | yes |  | The ID of the item after which the new one goes. |
| `name` | `String` | yes |  | The title. |
| `url` | `String` | yes |  | 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(...)}`.

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

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

Type: `command`

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](../../guides/views/).

Example: Breadcrumb in a layout
```html
<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>
```
