Giter Site home page Giter Site logo

codeigniter-ss-twig's Introduction

CodeIgniter Simple and Secure Twig

Latest Stable Version Total Downloads Latest Unstable Version License

This package provides simple Twig integration for CodeIgniter 4.x.

If you use CodeIgniter3, check master branch.

Requirements

  • PHP 7.3 or later
  • CodeIgniter 4.2 or later
  • Twig 3.3.8 or later

Installation

With Composer

$ cd /path/to/codeigniter/
$ composer require kenjis/codeigniter-ss-twig

Usage

Loading Twig Library

$this->twig = new \Kenjis\CI4Twig\Twig();

You can override the default configuration:

$config = [
	'paths' => ['/path/to/twig/templates', VIEWPATH],
	'cache' => '/path/to/twig/cache',
];
$this->twig = new \Kenjis\CI4Twig\Twig($config);

Rendering Templates

Render Twig template and output to browser:

$this->twig->display('welcome', $data);

The above code renders Views/welcome.twig.

Render Twig template:

$output = $this->twig->render('welcome', $data);

The above code renders Views/welcome.twig.

Adding a Global Variable

$this->twig->addGlobal('sitename', 'My Awesome Site');

Getting Twig\Environment Instance

$twig = $this->twig->getTwig();

Supported CodeIgniter Helpers

  • base_url
  • site_url
  • anchor
  • form_open
  • form_close
  • form_error
  • form_hidden
  • set_value

Some helpers are added the functionality of auto-escaping for security.

Adding Your Functions

You can add your functions with configuration:

$config = [
	'functions' => ['my_helper'],
	'functions_safe' => ['my_safe_helper'],
];
$this->twig = new \Kenjis\CI4Twig\Twig($config);

If your function explicitly outputs HTML code, you will want the raw output to be printed. In such a case, use functions_safe, and you have to make sure the output of the function is XSS free.

References

Documentation

Samples

@TODO

How to Run Tests

$ cd codeigniter-ss-twig
$ composer install
$ vendor/bin/phpunit

Related Projects for CodeIgniter 4.x

codeigniter-ss-twig's People

Contributors

kenjis avatar kimtree avatar miqueiaspenha avatar ytetsuro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.