PHP code example of iviphp / view

1. Go to this page and download the library: Download iviphp/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/ */

    

iviphp / view example snippets


<!-- resources/views/home.php -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?= htmlspecialchars($title, ENT_QUOTES, 'UTF-8') 



declare(strict_types=1);

use Ivi\View\View;

$view = View::fromPaths([
    __DIR__ . '/resources/views',
]);

$html = $view->render(
    'home',
    [
        'title' => 'IviPHP',
        'heading' => 'Welcome to IviPHP',
    ]
);

$view = View::fromConfig([
    'paths' => [
        __DIR__ . '/resources/views',
    ],

    'extension' => 'php',

    'shared' => [
        'applicationName' => 'IviPHP',
    ],
]);

$view->render('home');
$view->render('users.profile');
$view->render('admin.dashboard.index');

if ($view->exists('users.profile')) {
    $html = $view->render(
        'users.profile',
        [
            'user' => $user,
        ]
    );
}

$html = $view->render(
    'users.profile',
    [
        'user' => [
            'id' => 42,
            'name' => 'Gaspard',
        ],
    ]
);

<article>
    <h1>
        <?= htmlspecialchars(
            $user['name'],
            ENT_QUOTES,
            'UTF-8'
        ) 

$html = $view->make(
    'users.profile',
    [
        'user' => $user,
    ]
);

$html = $view->render(
    'article',
    [
        'title' => 'Building with IviPHP',
        'content' => 'A small framework can still be powerful.',
        'published' => true,
    ]
);

<article>
    <h1>
        <?= htmlspecialchars(
            $title,
            ENT_QUOTES,
            'UTF-8'
        ) 

$view->render(
    'home',
    [
        '__iviPath' => 'invalid',
    ]
);

<?= htmlspecialchars(
    $userInput,
    ENT_QUOTES | ENT_SUBSTITUTE,
    'UTF-8'
) 

<?= (int) $userId 

<script>
    window.applicationData = <?= json_encode(
        $applicationData,
        JSON_THROW_ON_ERROR
        | JSON_HEX_TAG
        | JSON_HEX_AMP
        | JSON_HEX_APOS
        | JSON_HEX_QUOT
    ) 

$view->share(
    'applicationName',
    'IviPHP'
);

<title>
    <?= htmlspecialchars(
        $applicationName,
        ENT_QUOTES,
        'UTF-8'
    ) 

$view->shareMany([
    'applicationName' => 'IviPHP',
    'environment' => 'production',
    'currentYear' => 2026,
]);

$view->share(
    'title',
    'Default title'
);

$html = $view->render(
    'home',
    [
        'title' => 'Home page',
    ]
);

$view->hasShared('applicationName');

$name = $view->shared(
    'applicationName',
    'Application'
);

$data = $view->allShared();

$view->forgetShared('environment');

$view->flushShared();

$view = View::fromPaths([
    __DIR__ . '/resources/views',
    __DIR__ . '/modules/blog/views',
]);

$view = View::fromPaths([
    __DIR__ . '/resources/views',
    __DIR__ . '/modules/blog/views',
]);

$view->addPath(
    __DIR__ . '/modules/shop/views'
);

$view->prependPath(
    __DIR__ . '/themes/custom/views'
);

$view->addPath(
    __DIR__ . '/themes/custom/views',
    true
);

$view->removePath(
    __DIR__ . '/modules/shop/views'
);

$view->setPaths([
    __DIR__ . '/new/views',
    __DIR__ . '/shared/views',
]);

$paths = $view->paths();

$path = $view->path('users.profile');

$view->render('home');
$view->render('home');

$view->forget('home');

$view->flush();

$view = View::fromPaths(
    paths: [
        __DIR__ . '/resources/views',
    ],
    extension: 'phtml'
);

use Ivi\View\ViewFinder;

$finder = new ViewFinder([
    __DIR__ . '/resources/views',
]);

use Ivi\View\ViewManager;

$manager = new ViewManager($finder);

$html = $manager->render(
    'home',
    [
        'title' => 'IviPHP',
    ]
);

$manager = ViewManager::fromPaths(
    paths: [
        __DIR__ . '/resources/views',
    ],
    sharedData: [
        'applicationName' => 'IviPHP',
    ]
);

use Ivi\View\View;

$view = new View($manager);

use Ivi\View\ViewFinder;

$finder = new ViewFinder([
    __DIR__ . '/resources/views',
]);

$path = $finder->find('users.profile');

$exists = $finder->exists('users.profile');

$relativePath = $finder->relativePath(
    'users.profile'
);

$extension = $finder->extension();

$finder->setExtension('php');

use Ivi\View\Engines\PhpEngine;

$engine = new PhpEngine();

$html = $engine->render(
    view: 'home',
    path: __DIR__ . '/resources/views/home.php',
    data: [
        'title' => 'IviPHP',
    ]
);

<h1>Hello</h1>

 ob_start(); 

 

<section>
    <h1>
        <?= htmlspecialchars(
            $heading,
            ENT_QUOTES,
            'UTF-8'
        ) 

$content = $view->render(
    'pages.home',
    [
        'heading' => 'Welcome',
        'message' => 'Your application is ready.',
    ]
);

$html = $view->render(
    'layouts.app',
    [
        'title' => 'Home',
        'content' => $content,
    ]
);

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>
        <?= htmlspecialchars(
            $title,
            ENT_QUOTES,
            'UTF-8'
        ) 

<div class="alert alert-<?= htmlspecialchars(
    $type,
    ENT_QUOTES,
    'UTF-8'
) 

$alert = $view->render(
    'components.alert',
    [
        'type' => 'success',
        'message' => 'The operation completed successfully.',
    ]
);

$html = $view->render(
    'users.index',
    [
        'users' => [
            [
                'id' => 1,
                'name' => 'Alice',
            ],
            [
                'id' => 2,
                'name' => 'Bob',
            ],
        ],
    ]
);

<ul>
     foreach ($users as $user): 

 if ($user !== null): 

Ivi\View\Exceptions\ViewException

use Ivi\View\Exceptions\ViewException;

try {
    $html = $view->render(
        'users.profile',
        [
            'user' => $user,
        ]
    );
} catch (ViewException $exception) {
    error_log(
        json_encode([
            'message' => $exception->getMessage(),
            'context' => $exception->context(),
        ])
    );
}

$context = $exception->context();

[
    'view' => 'users.profile',
    'path' => '/var/www/application/resources/views/users/profile.php',
]

[
    'view' => 'users.profile',
    'variables' => [
        'user',
        'pageTitle',
    ],
]

use Ivi\View\Exceptions\ViewException;

ViewException::invalidViewName(
    $view,
    $reason
);

ViewException::viewNotFound(
    $view,
    $paths
);

ViewException::invalidPath(
    $path,
    $reason
);

ViewException::directoryUnavailable(
    $directory
);

ViewException::viewUnreadable(
    $view,
    $path
);

ViewException::renderFailed(
    $view,
    $path,
    $previous
);

ViewException::invalidData(
    $view,
    $variables,
    $reason
);

ViewException::unsupportedEngine(
    $engine
);

ViewException::invalidConfiguration(
    $engine,
    $reason
);

use Ivi\View\Exceptions\ViewException;

try {
    $html = $view->render(
        'dashboard.index',
        [
            'user' => $currentUser,
            'statistics' => $statistics,
        ]
    );
} catch (ViewException $exception) {
    error_log(
        json_encode([
            'message' => $exception->getMessage(),
            'context' => $exception->context(),
        ])
    );

    $html = '<h1>Unable to render this page.</h1>';
}



declare(strict_types=1);

use Ivi\View\Exceptions\ViewException;
use Ivi\View\View;


    ],

    'shared' => [
        'applicationName' => 'IviPHP',
        'currentYear' => 2026,
    ],
]);

try {
    $content = $view->render(
        'pages.home',
        [
            'heading' => 'Modern PHP, kept simple.',
            'message' => 'Build your application from focused packages.',
        ]
    );

    $html = $view->render(
        'layouts.app',
        [
            'title' => 'Home',
            'content' => $content,
        ]
    );

    echo $html;
} catch (ViewException $exception) {
    error_log(
        json_encode([
            'message' => $exception->getMessage(),
            'context' => $exception->context(),
        ])
    );

    http_response_code(500);

    echo 'Unable to render the page.';
}

<section>
    <h1>
        <?= htmlspecialchars(
            $heading,
            ENT_QUOTES,
            'UTF-8'
        ) 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1"
    >

    <title>
        <?= htmlspecialchars(
            $title,
            ENT_QUOTES,
            'UTF-8'
        ) 

$view->exists($name);

$view->render(
    $name,
    $data
);

View::fromPaths(
    $paths,
    $sharedData,
    $extension
);

View::fromConfig(
    $configuration
);

$view->manager();
$view->finder();

$view->exists($name);
$view->path($name);

$view->render(
    $name,
    $data
);

$view->make(
    $name,
    $data
);

$view->share(
    $name,
    $value
);

$view->shareMany($data);

$view->hasShared($name);

$view->shared(
    $name,
    $default
);

$view->allShared();

$view->forgetShared($name);
$view->flushShared();

$view->addPath(
    $path,
    $prepend
);

$view->prependPath($path);
$view->removePath($path);
$view->setPaths($paths);
$view->paths();

$view->forget($name);
$view->flush();

ViewManager::fromPaths(
    $paths,
    $sharedData,
    $extension
);

$manager->finder();
$manager->engine();

$manager->exists($name);
$manager->path($name);

$manager->render(
    $name,
    $data
);

$manager->make(
    $name,
    $data
);

$manager->share(
    $name,
    $value
);

$manager->shareMany($data);

$manager->hasShared($name);

$manager->shared(
    $name,
    $default
);

$manager->allShared();

$manager->forgetShared($name);
$manager->flushShared();

$manager->addPath(
    $path,
    $prepend
);

$manager->prependPath($path);
$manager->removePath($path);
$manager->setPaths($paths);
$manager->paths();

$manager->forget($name);
$manager->flush();

$finder->addPath(
    $path,
    $prepend
);

$finder->prependPath($path);
$finder->removePath($path);
$finder->setPaths($paths);

$finder->paths();

$finder->extension();
$finder->setExtension($extension);

$finder->exists($view);
$finder->find($view);
$finder->relativePath($view);

$finder->forget($view);
$finder->flush();

$engine->render(
    $view,
    $path,
    $data
);

$exception->getMessage();
$exception->context();
$exception->getPrevious();

<?= htmlspecialchars(
    $value,
    ENT_QUOTES | ENT_SUBSTITUTE,
    'UTF-8'
) 

$view->render($_GET['view']);

$pages = [
    'home' => 'pages.home',
    'about' => 'pages.about',
];

$page = $_GET['page'] ?? 'home';
$template = $pages[$page] ?? 'errors.404';

$html = $view->render($template);

<a href="<?= htmlspecialchars(
    $url,
    ENT_QUOTES | ENT_SUBSTITUTE,
    'UTF-8'
) 

<?= $content 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta
        name="viewport"
        content="width=device-width, initial-scale=1"
    >

    <title>
        <?= htmlspecialchars(
            $title,
            ENT_QUOTES,
            'UTF-8'
        ) 
text
src/
├── Engines/
│   └── PhpEngine.php
├── Exceptions/
│   └── ViewException.php
├── View.php
├── ViewFinder.php
├── ViewInterface.php
└── ViewManager.php
text
resources/
└── views/
    └── home.php
text
home.php
users/profile.php
admin/dashboard/index.php
text
GLOBALS
_SERVER
_GET
_POST
_FILES
_COOKIE
_SESSION
_REQUEST
_ENV
this
text
resources/views/layouts/app.php
modules/blog/views/layouts/app.php
text
resources/views/layouts/app.php
text
/var/www/application/resources/views/users/profile.php
text
users/profile.php
text
resources/views/
├── layouts/
│   └── app.php
└── pages/
    └── home.php
text
resources/views/components/alert.php
text
application/
├── public/
│   └── index.php
└── resources/
    └── views/
        ├── layouts/
        │   └── app.php
        └── pages/
            └── home.php