1. Go to this page and download the library: Download vheissu/ci-twig 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/ */
vheissu / ci-twig example snippets
namespace Config;
use CodeIgniter\Config\BaseService;
class Services extends BaseService
{
public static function twig(bool $getShared = true): \Vheissu\CiTwig\Twig
{
if ($getShared) {
return static::getSharedInstance('twig');
}
return new \Vheissu\CiTwig\Twig(config('Twig'));
}
}
namespace Config;
use Vheissu\CiTwig\Config\Twig as BaseTwig;
class Twig extends BaseTwig
{
public bool $debug = true;
public array $functions = [
'base_url',
'site_url',
];
}
namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
// Load URL helper if using base_url(), site_url(), etc.
helper('url');
$twig = service('twig');
return $twig->render('home', [
'title' => 'Welcome',
'user' => $this->getUser(),
]);
}
}
namespace Config;
use Vheissu\CiTwig\Config\Twig as BaseTwig;
class Twig extends BaseTwig
{
public bool $debug = false;
public bool $cacheEnabled = true;
public bool $autoReload = false;
public array $functions = [
'base_url',
'site_url',
'lang',
];
public array $filters = [
'esc',
];
}
$twig = service('twig');
// Render and return as string
$html = $twig->render('pages/about', ['title' => 'About Us']);
// Render and output directly
$twig->display('pages/about', ['title' => 'About Us']);
// Extension is optional
$twig->render('pages/about'); // Looks for pages/about.twig
$twig->render('pages/about.twig'); // Same result
// Render a string as a template
$html = $twig->renderString('Hello, {{ name }}!', ['name' => 'World']);
$twig = service('twig');
// Set a single variable
$twig->set('title', 'My Page');
// Set multiple variables
$twig->set([
'title' => 'My Page',
'items' => ['one', 'two', 'three'],
]);
// Set a global variable (persists across all renders)
$twig->set('site_name', 'My Website', global: true);
// Variables can also be passed directly to render()
$twig->render('page', ['title' => 'Passed directly']);
$twig = service('twig');
// Register an existing PHP function
$twig->registerFunction('strtoupper');
// Register with a custom callback
$twig->registerFunction('asset', fn($path) => base_url("assets/{$path}"));
// Use in template: {{ asset('css/style.css') }}
$twig = service('twig');
// Register an existing PHP function as a filter
$twig->registerFilter('ucfirst');
// Register with a custom callback
$twig->registerFilter('money', fn($n) => '$' . number_format($n, 2));
// Use in template: {{ price|money }}
$twig = service('twig');
// Add a path to the search list
$twig->addPath(APPPATH . 'Themes/default/');
// Prepend a path (searched first - useful for theme overrides)
$twig->prependPath(APPPATH . 'Themes/custom/');
// Add a namespaced path
$twig->addPath(APPPATH . 'Modules/Blog/Views/', 'blog');
$twig = service('twig');
// Get the Twig\Environment instance
$env = $twig->getEnvironment();
$env->addExtension(new MyCustomExtension());
// Get the Twig\Loader\FilesystemLoader
$loader = $twig->getLoader();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.