PHP code example of sailantis / clarity-engine

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

    

sailantis / clarity-engine example snippets



Clarity\ClarityEngine;

// Initialize the engine
$engine = new ClarityEngine([
    'viewPath'   => __DIR__ . '/views',
]);

// Render a template. The second argument is the template's scope.
echo $engine->render('welcome', [
    'title' => 'Welcome to Clarity',
    'user'  => (object) ['name' => 'Developer'],   // object -> {{ user.name }}
    // 'user' => ['name' => 'Developer'],          // array  -> {{ user:name }}
]);

use Clarity\Engine\Policy;

$engine->setPolicy(Policy::sandboxed());   // the default
$engine->setPolicy(Policy::open());        // everything on
$engine->setPolicy(Policy::custom()        // grant one thing, not all
    ->allowCapability('methodCalls')
    ->allowFunctions('strtoupper', 'count'));

// Initialize with config array
$engine = new ClarityEngine([
    'viewPath' => __DIR__ . '/views',
    'cachePath' => __DIR__ . '/cache',
]);

// Or configure via setters
$engine = ClarityEngine::create()
    ->setViewPath(__DIR__ . '/views')
    ->setCachePath(__DIR__ . '/cache'); // Default: sys temp + /clarity_cache

// Additional configuration
$engine->setExtension('.tpl.html');           // Default: .clarity.html

// Register named namespaces (convenience method)
$engine->addNamespace('admin',  __DIR__ . '/views/admin');
$engine->addNamespace('emails', __DIR__ . '/views/emails');
// Templates with a namespace prefix: {% 2));
// Clear compiled templates
$engine->flushCache();

// Modules: bundle filters, functions, and directives
$engine->use(new \Clarity\Localization\IntlFormatModule(['locale' => 'en_US']));
$engine->use(new \Clarity\Localization\TranslationModule([
    'locale'            => 'en_US',
    'translations_path' => __DIR__ . '/locales',
]));

use Clarity\Engine\Policy;

$engine->setPolicy(Policy::open());        // full PHP: any function, methods, raw PHP
$engine->setPolicy(Policy::custom()
    ->allowCapability('methodCalls')
    ->allowFunctions('strtoupper', 'count'));
bash
php vendor/bin/phpunit