Custom functions making in Twig with Codeigniter 3 in PHP
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Welcome extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper("twig");
}
public function index()
{
$data = [
'title' => 'Welcome to CodeIgniter with Twig',
'message' => 'This is a sample page using Twig as the template engine.',
"items" => array(
array("name" => "Item 1", "description" => "This is item 1"),
array("name" => "Item 2", "description" => "This is item 2"),
array("name" => "Item 3", "description" => "This is item 3"),
),
"heading1" => "Harry-ONE",
"heading2" => "Harry-TWO",
"heading3" => "Harry-THREE",
"heading4" => "Harry-FOUR",
"heading5" => "Harry-FIVE",
"heading6" => "Harry-SIX",
"paragraph1" => "This is Harry",
"paragraph2" => "This is new paragraph",
"paragraph3" => "Lorem ipsum dolor sit amet consectetur",
"strong_text" => "This is strong",
"paragraph4" => "adipisicing elit. Harum suscipit a sed magni excepturi nisi id, consequuntur at molestiae incidunt. Sunt harum,",
"emphasized_text" => "This is Emphasized",
"paragraph5" => "architecto vitae est soluta optio perspiciatis. Repudiandae, eveniet?",
"paragraph6" => "Lorem ipsum, dolor sit amet consectetur adipisicing elit.",
"paragraph7" => "At minima impedit, deleniti vero necessitatibus voluptatibus officiis dolorum voluptatum placeat harum fugiat repellendus vel consequatur porro possimus. Facilis pariatur consequatur accusantium!",
];
$this->twig->render('welcome_message.twig', $data);
$this->twig->render("Headings_Paragraphs.twig", $data);
}
public function operationFunction()
{
$data = [];
$this->twig->render("operation_function.twig", $data);
}
}
operation_function.twig
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>addition Function</title>
</head>
<body>
<h2>{{ additionFunction(5, 3) }}</h2>
<h2>{{ subtractionFunction(15, 5) }}</h2>
<h2>{{ multiplicationFunction(10, 20) }}</h2>
<h2>{{ divisionFunction(30, 15) }}</h2>
</body>
</html>
twig_helper.twig
<?php
defined("BASEPATH") or exit("No direct script access allowed");
function my_custom_function($param, $paramTwo)
{
return "This is Twig function named my_custom_function: Hello " . $param . $paramTwo;
}
function additionFunction($arg1, $arg2)
{
return "Addition of Two numbers 5 and 3 is : " . ($arg1 + $arg2);
}
function subtractionFunction($arg1, $arg2)
{
return "Subtraction of Two numbers 15 and 5 is : " . ($arg1 - $arg2);
}
function multiplicationFunction($arg1, $arg2)
{
return "Multiplication of Two numbers 10 and 20 is : " . ($arg1 * $arg2);
}
function divisionFunction($arg1, $arg2)
{
return "Division of Two numbers 30 and 15 is : " . ($arg1 / $arg2);
}
Twig.php
<?php defined('BASEPATH') or exit('No direct script access allowed');
require_once FCPATH . 'vendor/autoload.php';
class Twig
{
protected $CI;
protected $twig;
protected $loader;
public function __construct()
{
$this->CI = &get_instance();
$loader = new \Twig\Loader\FilesystemLoader(APPPATH . 'views');
$this->twig = new \Twig\Environment($loader, [
'cache' => APPPATH . 'cache/twig',
'debug' => true,
]);
if (ENVIRONMENT === 'development') {
$this->twig->enableDebug();
$this->twig->addExtension(new \Twig\Extension\DebugExtension());
}
// Register the custom function
$this->registerCustomFunctions();
$this->additionFunction();
$this->subtractionFunction();
$this->multiplicationFunction();
$this->divisionFunction();
}
protected function registerCustomFunctions()
{
// Ensure the custom helper is loaded
$this->CI->load->helper('twig');
// Register the custom function
$function = new \Twig\TwigFunction('my_custom_function', 'my_custom_function');
$this->twig->addFunction($function);
}
public function additionFunction()
{
$function = new \Twig\TwigFunction("additionFunction", "additionFunction");
$this->twig->addFunction($function);
}
public function subtractionFunction()
{
$function = new \Twig\TwigFunction("subtractionFunction", "subtractionFunction");
$this->twig->addFunction($function);
}
public function multiplicationFunction()
{
$function = new \Twig\TwigFunction("multiplicationFunction", "multiplicationFunction");
$this->twig->addFunction($function);
}
public function divisionFunction()
{
$function = new \Twig\TwigFunction("divisionFunction", "divisionFunction");
$this->twig->addFunction($function);
}
public function render($template, $data = [])
{
echo $this->twig->render($template, $data);
}
}

Comments
Post a Comment