
Pug-php
Pug-php adds inline PHP scripting support to the Pug template compiler. Since the version 3, it uses Phug, a very customizable Pug template engine made by Tale-pug and Pug-php developers as the new PHP Pug engine reference.
Official Phug documentation
See Pug-php demo
Install
First you need composer if you haven't yet: https://getcomposer.org/download/
Then run:
composer require pug-php/pug
Pug in your favorite framework
Phalcon: https://github.com/pug-php/pug-phalcon
Symfony: https://github.com/pug-php/pug-symfony
Laravel: https://github.com/BKWLD/laravel-pug
CodeIgniter: https://github.com/pug-php/ci-pug-engine
Yii 2: https://github.com/rmrevin/yii2-pug
Slim 3: https://github.com/MarcelloDuarte/pug-slim
Use
<?php
include 'vendor/autoload.php';
$pug = new Pug([
// here you can set options
]);
$pug->displayFile('my-pug-template.pug');
Since Pug-php 3.1.2, you no longer need to import the class with use Pug\Pug;
as we provide an alias.
Main methods are render
, renderFile
, compile
, compileFile
, display
, displayFile
and setOption
, see the complete documentation here: phug-lang.com.
You can also use the facade to call methods statically:
<?php
use Pug\Facade as PugFacade;
include 'vendor/autoload.php';
$html = PugFacade::renderFile('my-pug-template.pug');
Pug options
Pug options should be passed to the constructor
$pug = new Pug(array(
'pretty' => true,
'cache' => 'pathto/writable/cachefolder/'
));
Supports for local variables
$pug = new Pug();
$output = $pug->render('file', array(
'title' => 'Hello World'
));
New in pug-php 3
pug-php 3 is now aligned on pugjs 2, it aims to be a perfect port of the JS project. That's why there are breaking changes in this new version.
See the changelog to know what's new
See the migration guide if you want to upgrade from pug-php 2 to 3
Supports for custom filters
Filters must be callable: It can be a class that implements the __invoke() method, or an anonymous function.
$pug->filter('escaped', 'My\\Callable\\Class');
// or
$pug->filter('escaped', function($node, $compiler){
foreach ($node->block->nodes as $line) {
$output[] = $compiler->interpolate($line->value);
}
return htmlentities(implode("\n", $output));
});
Built-in filters
- :css
- :php
- :javascript
- :escaped
- :cdata