Index
# Document Usage The `Document` class in the provided code is a container used to store and manage data, typically used by a controller or response in a web application. It implements `ArrayAccess` and `LinkProviderInterface`, allowing it to be used like an array and to manage links. This example demonstrates creating a `Document`, adding content and metadata, logging events, rendering the document, accessing data, adding links, and importing files. ### Purpose 1. **Data Storage**: It stores data in the `$body` property, which can be accessed and modified using array-like syntax. 2. **Serialization**: It can be serialized to JSON, including metadata, links, data, and logs. 3. **Logging**: It can log events and store them in the `$logs` property. 4. **Rendering**: It can render its content to a string using a specified printer. 5. **Link Management**: It can manage links, providing methods to add and retrieve links. 6. **Importing**: It supports importing files and capturing their output. ### Example Usage ```php 'value']); // Add more content to the document $document->addContent(['newKey' => 'newValue']); // Set metadata $document->addMeta(['metaKey' => 'metaValue']); // Log an event $document->logEvent(new \stdClass()); // Render the document to a string echo $document->render(); // Access data using array-like syntax echo $document['key']; // Outputs: value // Add a link $link = new \MMO\core\server\Link(['rel' => 'example', 'href' => '/example.jpg']); $document->addLink($link); // Prints the links in the document print_r($document->getLinks()); // Import a file and capture its output $output = $document->import('path/to/file.php', ['var1' => 'value1']); echo $output; ``` ### Templates read `$_VARS`, not bare variables `import()` does not `extract()` anything. Inside the imported file, the variables are in the `$_VARS` array (merged with the document's own `getVars()`), and `$this` is the document: ```php

``` The filename is looked up literally in the document's import paths: pass `'welcome.phtml'`, not `'welcome'`. ### Options A document takes options when it is built: its own (`raw`, `charset`, `printer`, `vars`) and those of the printer that renders its values. Pass them typed, with <@\MMO\core\documents\DocumentOptions> and <@\MMO\core\printers\PrintOptions>: ```php use MMO\core\documents\{DocumentOptions, HtmlDocument}; use MMO\core\printers\PrintOptions; //every value the page's templates print with $this->renderValue() is HTML-escaped $page = new HtmlDocument(null, new DocumentOptions(print: new PrintOptions(escape: true))); //or one value echo $page->renderValue('user input', new PrintOptions(escape: true)); ``` An array still works (`['raw' => true]`), but a misspelled key is silently ignored: `['escpae' => true]` prints the value unescaped, where `new PrintOptions(escpae: true)` fails with "Unknown named parameter $escpae".