Welcome Guest, Not a member yet? Register   Sign In
Smarty with CI4
#2

(This post was last modified: 02-12-2024, 11:39 AM by n2fole00.)

Ok, I got something working Smile

Well I decided to go with Twig, but I think the same idea will work for Smarty.

Source info: https://twig.symfony.com/doc/3.x/intro.h...-api-usage

First install twig with composer.
Code:
composer require "twig/twig:^3.0"


In BaseController.php

PHP Code:
namespace App\Controllers;
...
use 
Twig;

abstract class 
BaseController extends Controller
{
    ...
    protected $twig;

    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        // Preload any models, libraries, etc, here.
        $this->initTwig();
    }

    private function initTwig(): void {
        $loader = new Twig\Loader\FilesystemLoader(APPPATH.'Views');
        $this->twig = new Twig\Environment($loader, [
            'cache' => ENVIRONMENT === 'development' false APPPATH.'writable/cache',
            'debug' => ENVIRONMENT === 'development' true false,
        ]);
        $this->twig->addExtension(new \Twig\Extension\DebugExtension()); // add this if you want to run the {{ dump() }} function
    }

    protected function twigRender(string $filename, array $dataArray)  {
        echo $this->twig->render($filename$dataArray);
    }



Then in your controller

PHP Code:
namespace App\Controllers;

class 
Test extends BaseController
{
    public function index()
    {
        // echo $this->twig->render('test.twig', ['name' => 'Noel']);
        $this->twigRender('test.twig', ['name' => 'Noel']);
    }


Here, I created twigRender as an alternative to

PHP Code:
echo $this->twig->render('test.twig', ['name' => 'Noel']); 


but it's not required.
Reply


Messages In This Thread
Smarty with CI4 - by n2fole00 - 01-08-2024, 08:55 AM
RE: Smarty with CI4 - by n2fole00 - 02-11-2024, 07:06 AM
RE: Smarty with CI4 - by kenjis - 02-11-2024, 05:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB