PHP code example of inanepain / view

1. Go to this page and download the library: Download inanepain/view library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

inanepain / view example snippets

 declare(strict_types=1);
 declare(strict_types=1); 

declare(strict_types=1);

use Inane\View\Renderer\PhpRenderer;

$renderer = new PhpRenderer(__DIR__ . '/templates');

echo $renderer->renderPartial('item', ['name' => 'Ada']);
echo $renderer->render('list', [
    'items' => [
        ['name' => 'Ada'],
        ['name' => 'Grace'],
    ],
]);
----

The first call produces `<li>Ada</li>`. The second produces `<ul><li>Ada</li><li>Grace</li></ul>` (apart from template line endings). Inside `list.phtml`, `$this` is the renderer because no explicit context object was supplied. Each call to `renderEach` renders `item.phtml` with a separate variable array.

=== String Partials and Shared Defaults

String partials resolve names from the template stack. Render the collection first, then pass its markup into the parent placeholder. Escape user-provided values before passing them to the renderer.

[source,php]
----

declare(strict_types=1);

use Inane\View\Renderer\StringRenderer;

$renderer = new StringRenderer([
    'item' => '<li>{$name}: {$role}</li>',
    'list' => '<ul>{$content}</ul>',
]);

$content = $renderer->renderEach(
    template: 'item',
    items: [
        ['name' => 'Ada'],
        ['name' => 'Grace', 'role' => 'editor'],
        ['name' => 'Linus'],
    ],
    data: ['role' => 'reader'],
);

echo $renderer->renderPartial('list', ['content' => $content]);
----

.Output
[source,html]
----
<ul><li>Ada: reader</li><li>Grace: editor</li><li>Linus: reader</li></ul>
----

Grace's item overrides the shared `role`, but Linus still receives the shared default. To render the named parent with `render` instead, use `$renderer->render(template: 'list', data: ['content' => $content], useStack: true)`; without `useStack`, `render('list')` returns the literal string `list`.

== Model Trees

`HttpModel::addChild` attaches a named child model. `HttpModel::render` renders children recursively before rendering the parent with a configured `PhpRenderer`. Alternatively, initialise `ViewManager` with that renderer and pass the root model to its `render` method.

Child output is available both as a variable bearing the child's name and through `$this` on the bound model context. Rendered children override parent variables with the same name. Children inherit parent variables, including iteration variables, but explicitly supplied child variables take precedence. Child-property access outside a rendering context throws `Inane\View\Exception\RuntimeException`.

`IterativeModel` extends `HttpModel` and accepts `items`, shared `variables` and model `options`. Each item is an array of template variables; its values override shared defaults. Every item renders the model's template and child tree, and the resulting strings are concatenated. Iterables are materialised at construction so even generator-backed models can be rendered repeatedly. An empty model produces an empty string.

Models retain their original variables and child models after rendering. Circular model trees throw `Inane\View\Exception\RuntimeException`. Rendering exceptions propagate, output buffers are cleaned, and models can be retried after correcting the failure. Layout and HTTP response handling are not performed by these rendering methods.

=== A Nested Iterative Model

This example attaches an `IterativeModel` to a parent `HttpModel` and gives each repeated row its own child view. Create the following templates in the same `templates` directory.

.templates/page.phtml
[source,php]
----
 declare(strict_types=1); 
 declare(strict_types=1); 
 declare(strict_types=1);