PHP code example of brightnucleus / view

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

    

brightnucleus / view example snippets


 namespace View\Example;

use View\Example\App;
use BrightNucleus\View;
use BrightNucleus\View\Location\FilesystemLocation;

$folders = [
    App::ROOT_FOLDER . '/child-theme/templates',
    App::ROOT_FOLDER . '/parent-theme/templates',
    App::ROOT_FOLDER . '/plugin/templates',
];

foreach ($folders as $folder) {
    Views::addLocation(new FilesystemLocation($folder));
}

 namespace View\Example;

use View\Example\User;
use BrightNucleus\Views;

echo Views::render('welcome-user', [ 'userId' => User::getCurrentId() ]);

 namespace View\Example;

// This is our template that is being rendered.


 namespace View\Example;

use BrightNucleus\Config\ConfigFactory;
use BrightNucleus\View\ViewBuilder;
use BrightNucleus\View\Location\FilesystemLocation;

// Fetch the Config from somewhere.
$config = ConfigFactory::create(__DIR__. '/config/views.php');

// Create a new instance of the ViewBuilder and add a location.
$viewBuilder = new ViewBuilder( $config );
$viewBuilder->addLocation(new FilesystemLocation(__DIR__ . '/views'));

// Create a new instance of a specific View.
$view = $viewBuilder->create('my-view');

// Render the view.
echo $view->render(['answer' => 42]);

 namespace View\Example;

use BrightNucleus\View\Engine\EngineFinder;
use BrightNucleus\View\View\ViewFinder;

$engineFinder = [
    EngineFinder::CLASS_NAME_KEY => AwesomeEngineFinder::class,
    EngineFinder::ENGINES_KEY    => [
        'AwesomeEngine' => AwesomeEngine::class,
    ],
    EngineFinder::NULL_OBJECT    => AwesomeNullEngine::class,
];

$viewFinder = [
    ViewFinder::CLASS_NAME_KEY => AwesomeViewFinder::class,
    ViewFinder::VIEWS_KEY      => [
        'AwesomeView' => AwesomeView::class,
    ],
    ViewFinder::NULL_OBJECT    => AwesomeNullView::class,
];

return [
    'BrightNucleus' => [
        'View' => [
            'EngineFinder' => $engineFinder,
            'ViewFinder'   => $viewFinder,
        ],
    ],
];