Document


The Document class is a small, simplified server-side reproduction of some elements and behaviors typical of a DOM, built around a lightweight tree of tag-like objects, such as the title, meta tags, canonical URL, links, styles and scripts, which only become real HTML once the page is finally printed. It's a singleton, so you never instantiate it directly: you always reach it through \Document::get(), and whatever you add to it stays available for the rest of the request, no matter which view or module added it.

How it ends up on the page

The base view that wraps every page (src/views/html.php) reads from \Document::get() when it prints the final HTML: the title, the meta tags, the canonical link and the stylesheets are printed inside <head>, while the scripts are printed at the end of <body>, right before it closes.

Title and canonical URL

\Document::get()->title = "My page";
\Document::get()->title_append = " - Section"; // appended right after the title, before the project name
\Document::get()->canonical = "https://example.com/my-page";

The final <title> is built by joining the title, its appended text, and the project name, so the example above renders as "My page - Section - Monti".

Meta tags

\Document::get()->setMetaTag("description", [
    "name" => "description",
    "content" => "A short description of the page"
]);

The first argument is just an internal key used to avoid duplicates, it doesn't end up in the HTML, while the array of attributes is rendered as-is on the <meta> tag, so the same method also covers any attribute name:

\Document::get()->setMetaTag("og:title", [
    "property" => "og:title",
    "content" => "My page"
]);

Links, styles and scripts

appendLink(), appendStyle(), appendScript() and appendInlineScript() all work the same way: you provide the attributes (and, for styles and inline scripts, the content) as an array or string, and Document wraps them into a Tag, the same minimal, DOM-like element used internally for everything it manages, which takes care of rendering them into real HTML once the page is printed.

\Document::get()->appendLink(["rel" => "stylesheet", "href" => "/src/css/theme.css"]);
\Document::get()->appendScript(["src" => "/src/js/theme.js"]);

In practice you'll rarely call these directly from a view, since the start_style()/stop_style() and start_script()/stop_script() helpers, already covered in the MVC documentation, let you write plain <style>/<link>/<script> markup inline in a view and call these same Document methods for you behind the scenes.

Both appendStyle() and appendInlineScript() accept a group option, so elements sharing the same group have their content concatenated into a single tag instead of being printed one after another.

SEO endpoints

Beyond tag management, Document also exposes three request-terminating helpers, normally wired to their own routes: sitemap() reads src/seo/sitemap.php and outputs it as XML, robots() outputs a default robots.txt (or src/seo/robots.php, if present), and webmanifest() builds a site.webmanifest from the project's name, description and theme color.