jueves, 16 de abril de 2020
Install Twig on Codeigniter 4
Twig
Twig for Codeigniter 4
Installation via composer
Use the package with composer install
> composer require daycry/twig
Manual installation
Download this repo and then enable it by editing app/Config/Autoload.php and adding the Daycry\Twig namespace to the $psr4 array. For example, if you copied it into app/ThirdParty:
$psr4 = [
'Config' => APPPATH . 'Config',
APP_NAMESPACE => APPPATH,
'App' => APPPATH,
'Daycry\Twig' => APPPATH .'ThirdParty/twig/src',
];
Configuration
Run command:
> php spark twig:publish
This command will copy a config file to your app namespace. Then you can adjust it to your needs. By default file will be present in
app/Config/Twig.php
.Usage Loading Library
$twig = new \Daycry\Twig\Twig();
$twig->display( 'file.html', [] );
Usage as a Service
$twig = \Config\Services::twig();
$twig->display( 'file.html', [] );
Usage as a Helper
In your BaseController - $helpers array, add an element with your helper filename.
protected $helpers = [ 'twig_helper' ];
And then you can use the helper
$twig = twig_instance();
$twig->display( 'file.html', [] );
Add Globals
$twig = new \Daycry\Twig\Twig();
$session = \Config\Services::session();
$session->set( array( 'name' => 'Daycry' ) );
$twig->addGlobal( 'session', $session );
$twig->display( 'file.html', [] );
File Example
<!DOCTYPE html>
<html lang="es">
<head>
<title>Example</title>
<meta charset="UTF-8">
<meta name="title" content="Example">
<meta name="description" content="Example">
</head>
<body>
<h1>Hi {{ name }}</h1>
{{ dump( session.get( 'name' ) ) }}
</body>
</html>
How Run Tests
vendor\bin\phpunit vendor\daycry\twig\tests
Related Posts
Suscribirse a:
Enviar comentarios
(
Atom
)
Sígueme en las Redes Sociales
Donaciones
Datos personales
Entradas populares
-
En este apartado vamos a explicar como ejercutar archivos PHP a través del terminal de Ubuntu. Lo primero que tendríamos que hacer es inst...
-
En este blog voy a comentar un tema que se utilizan en casi todas las páginas web que existen, y es el tema de la paginación. La paginaci...
-
Este post trata de la integración de la librería PHPExcel en Codeigniter, aunque se podría aplicar a cualquier librería, como por ejemplo mP...
-
Ejemplo para añadir o sumar un número determinado de hora/s, minuto/s, segundo/s a una fecha en php. Con la función strtotime se puede ...
-
Este tema es uno de los temas primordiales sobre el framework Codeigniter, ya que en alguna ocación nos hemos visto obligados a recoger dato...
© Espacio Daycry - Espacio de programación 2013 . Powered by Bootstrap , Blogger templates and RWD Testing Tool
No hay comentarios :
Publicar un comentario