Download the PHP package nongbit/codeigniter-twig without Composer
On this page you can find all versions of the php package nongbit/codeigniter-twig. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nongbit/codeigniter-twig
More information about nongbit/codeigniter-twig
Files in nongbit/codeigniter-twig
Package codeigniter-twig
Short Description Integrate Twig to CodeIgniter 4
License MIT
Homepage https://github.com/nongbit/codeigniter-twig#readme
Informations about the package codeigniter-twig
CodeIgniter 4 Twig
Integrate Twig template engine into CodeIgniter 4.
Requirements
- PHP 7.4 or 8.0+
- CodeIgniter 4.4+
- Composer
Installation
Setup
1. BaseController
You can use the package either by directly accessing the Twig singleton or by using the provided trait.
Option A: Direct access (without trait)
Option B: Using the TwigTrait (recommended for simplicity)
2. Optional Configuration
Create app/Config/Twig.php to override defaults:
Note about
autoescape: See Twig documentation for details.
Usage
Basic Rendering
Without trait (direct singleton access):
With trait (using display() method):
The display() method outputs directly to the browser, so you don't need to return anything.
Adding View Paths
By default, Twig looks for templates inside APPPATH/Views. To add more locations:
Globals
In template:
Filters
In template:
Functions
Function behaves similarly to a filter.
In template:
Built-in Functions
site_url()– generates a site URL (CI4 URL helper)base_url()– generates a base URL (CI4 URL helper)d()– dumps variable using Kint (debug mode only)dd()– dumps and dies using Kint (debug mode only)
These are automatically available in all templates.
Singleton Pattern
This package uses the singleton pattern for the Nongbit\Twig\Twig class. Only one instance exists throughout the application.
Why singleton?
- Twig environment (loader, cache, extensions) is initialized only once per request.
- Compiled templates are reused efficiently.
- Better memory usage and performance.
Drawback
- Hard to reset the instance when needed, especially in unit testing (one test may affect another).
Resetting for Tests
Call \Nongbit\Twig\Twig::reset() to create a fresh instance.
You can place this call either:
-
In
setUp()(before each test) – recommended to ensure every test starts with a clean state. - In
tearDown()(after each test) – useful if you want to clean up after tests that may have modified the singleton.
Choose the option that best fits your test structure. The reset() method is intended for testing only and should never be used in production code.
Development vs Production
The environment is automatically detected using the ENVIRONMENT constant (set in .env or index.php).
| Environment | Debug Mode | Cache |
|---|---|---|
development |
ON (DebugExtension added) | OFF |
production |
OFF | ON (stored in writable/twig) |
In development, you can use {{ dump(var) }} (from DebugExtension) as well as {{ d(var) }} and {{ dd(var) }}.
License
MIT
All versions of codeigniter-twig with dependencies
codeigniter4/framework Version ^4.4
twig/twig Version ^3.0
twig/intl-extra Version ^3.0