2017 © Pedro Peláez
 

library jade-php

HAML-like template engine for PHP

image

kylekatarnls/jade-php

HAML-like template engine for PHP

  • PHP
  • 4 Dependents
  • 1 Suggesters
  • 22 Forks
  • 1 Open issues
  • 100 Versions
  • 4 % Grown

The README.md

Pug-php

Latest Stable Version Monthly Downloads License Build Status StyleCI Test Coverage Code Climate Dependencies, (*1)

Pug-php adds inline PHP scripting support to the Pug template compiler. Since version 3, it uses Phug, a very customizable Pug template engine made by the tale-pug and pug-php developers as the new PHP Pug engine reference., (*2)

Official Phug documentation
See Pug-php demo
Get supported pug-php/pug with the Tidelift Subscription

Install

First you need composer if you haven't yet: https://getcomposer.org/download/, (*3)

Then run:, (*4)

composer require pug-php/pug

Pug in your favorite framework

Phalcon: https://github.com/pug-php/pug-phalcon, (*5)

Symfony: https://github.com/pug-php/pug-symfony, (*6)

Laravel: https://github.com/BKWLD/laravel-pug, (*7)

CodeIgniter: https://github.com/pug-php/ci-pug-engine, (*8)

Yii 2: https://github.com/rmrevin/yii2-pug, (*9)

Slim 3: https://github.com/MarcelloDuarte/pug-slim, (*10)

Zend Expressive: https://github.com/kpicaza/infw-pug, (*11)

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., (*12)

Main methods are render, renderFile, compile, compileFile, display, displayFile and setOption, see the complete documentation here: phug-lang.com., (*13)

You can also use the facade to call methods statically:, (*14)

<?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, (*15)

$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 implementation of the JS project. That's why there are breaking changes in this new version., (*16)

See the changelog to know what's new, (*17)

See the migration guide if you want to upgrade from pug-php 2 to 3, (*18)

Support for custom filters

Filters must be callable: It can be a class that implements the __invoke() method or an anonymous function., (*19)

$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

Install other filters with composer

http://pug-filters.selfbuild.fr/, (*20)

Publish your own filter

https://github.com/pug-php/pug-filter-base#readme, (*21)

Support for custom keywords

You can add custom keywords, here are some examples:, (*22)

Anonymous function:, (*23)

$pug->addKeyword('range', function ($args) {
    list($from, $to) = explode(' ', trim($args));

    return array(
        'beginPhp' => 'for ($i = ' . $from . '; $i <= ' . $to . '; $i++) {',
        'endPhp' => '}',
    );
});

$pug->render('
range 1 3
    p= i
');

This will render:, (*24)

<p>1</p>
<p>2</p>
<p>3</p>

Note that the existing for..in operator will have the precedence on this custom for keyword., (*25)

Invokable class:, (*26)

class UserKeyword
{
    public function __invoke($arguments, $block, $keyWord)
    {
        $badges = array();
        foreach ($block->nodes as $index => $tag) {
            if ($tag->name === 'badge') {
                $href = $tag->getAttribute('color');
                $badges[] = $href['value'];
                unset($block->nodes[$index]);
            }
        }

        return array(
            'begin' => '<div class="' . $keyWord . '" data-name="' . $arguments . '" data-badges="[' . implode(',', $badges) . ']">',
            'end' => '</div>',
        );
    }
}

$pug->addKeyword('user', new UserKeyword());

$pug->render('
user Bob
    badge(color="blue")
    badge(color="red")
    em Registered yesterday
');

This will render:, (*27)

<div class="user" data-name="Bob" data-badges="['blue', 'red']">
    <em>Registered yesterday</em>
</div>

A keyword must return an array (containing begin and/or end entries) or a string (used as a begin entry)., (*28)

The begin and end are rendered as raw HTML, but you can also use beginPhp and endPhp like in the first example to render PHP code that will wrap the rendered block if there is one., (*29)

PHP Helper functions

If you want to make a php function available in a template or in all of them for convenience, use closures and pass them like any other variable:, (*30)

$myClosure = function ($string) {
    return 'Hey you ' . $string . ', out there on your own, can you hear me?';
};

$pug->render('p=$myClosure("Pink")', array('myClosure' => $myClosure));

This will render:, (*31)

<p>Hey you Pink, out there on your own, can you hear me?</p>

You can make that closure available to all templates without passing it in render params by using the share method:, (*32)

// ... $pug instantiation
$pug->share('myClosure', $myClosure);
$pug->render('p=$myClosure("Pink")');

This will render the same HTML than the previous example. Also note that share allow you to pass any type of value., (*33)

Cache

Important: to improve performance in production, enable the Pug cache by setting the cache option to a writable directory, you can first cache all your template at once (during deployment):, (*34)

$pug = new Pug(array(
    'cache' => 'var/cache/pug',
);
list($success, $errors) = $pug->cacheDirectory('path/to/pug/templates');
echo "$success files have been cached\n";
echo "$errors errors occurred\n";

Be sure any unexpected error occurred and that all your templates in your template directory have been cached., (*35)

Then use the same cache directory and template directory in production with the option upToDateCheck to false to bypass the cache check and automatically use the cache version:, (*36)

$pug = new Pug(array(
    'cache' => 'var/cache/pug',
    'basedir' => 'path/to/pug/templates',
    'upToDateCheck' => false,
);
$pug->render('path/to/pug/templates/my-page.pug');

Templates from pug-js

First remember pug-php is a PHP template engine. Pug-js and Pug-php provide both, a HAML-like syntax for markup and some abstraction of the language behind it (loops, conditions, etc.). But for expressions and raw code, pug-js uses JS, and pug-php uses PHP. By default, we do some magic tricks to transform simple JS syntax into PHP. This should help you to migrate smoother from pug-js if you already have some templates, but benefit from the PHP advantages., (*37)

If you start a new project, we highly recommend you to use the following option:, (*38)

$pug = new Pug(array(
    'expressionLanguage' => 'php'
);

It will disable all translations, so you always have to use explicit PHP syntax:, (*39)

- $concat = $foo . $bar
p=$concat

If you want expressions very close to JS, you can use:, (*40)

$pug = new Pug(array(
    'expressionLanguage' => 'js'
);

It will allow both PHP and JS in a JS-style syntax. But you have to stick to it, you will not be able to mix PHP and JS in this mode., (*41)

Finally, you can use the native pug-js engine with:, (*42)

$pug = new Pug(array(
    'pugjs' => true
);

This mode require node and npm installed as it will install pug-cli and directly call it. This mode will flatten your local variables (it means complex object like DateTime, or classes with magic methods will be stringified in JSON to simple objects) and you will not benefit from some features like mixed indent, pre/post render hooks. But in this mode you will get exact same output as in pug-js., (*43)

Write locals object to JSON file with pug-js

If your locals object is large it may cause a RuntimeException. This is because locals object passed directly to pug-cli as argument. To fix this problem you can use the localsJsonFile option:, (*44)

$pug = new Pug(array(
    'pugjs' => true,
    'localsJsonFile' => true
);

Then your locals will be written to a JSON file and the path of the file will be passed to the compiler., (*45)

Pug CLI

Pug also provide a CLI tool:, (*46)

./vendor/bin/pug render-file dir/my-template.pug --output-file 

See the complete CLI documentation here, (*47)

Check requirements

To check if your environment is ready to use Pug, use the requirements method:, (*48)

$pug = new Pug(array(
    'cache' => 'pathto/writable/cachefolder/'
);
$missingRequirements = array_keys(array_filter($pug->requirements(), function ($valid) {
    return $valid === false;
}));
$missings = count($missingRequirements);
if ($missings) {
    echo $missings . ' requirements are missing.<br />';
    foreach ($missingRequirements as $requirement) {
        switch($requirement) {
            case 'streamWhiteListed':
                echo 'Suhosin is enabled and ' . $pug->getOption('stream') . ' is not in suhosin.executor.include.whitelist, please add it to your php.ini file.<br />';
                break;
            case 'cacheFolderExists':
                echo 'The cache folder does not exists, please enter in a command line : <code>mkdir -p ' . $pug->getOption('cache') . '</code>.<br />';
                break;
            case 'cacheFolderIsWritable':
                echo 'The cache folder is not writable, please enter in a command line : <code>chmod -R +w ' . $pug->getOption('cache') . '</code>.<br />';
                break;
            default:
                echo $requirement . ' is false.<br />';
        }
    }
    exit(1);
}

Contributing

All contributions are welcome, for any bug, issue or merge request (except for security issues) please refer to CONTRIBUTING.md, (*49)

Security

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure., (*50)

Contributors

This project exists thanks to all the people who contribute. , (*51)

And all the people contributing to our depedencies, in particular: The Phug engine The JS syntax converter Js-Phpize, (*52)

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. Become a sponsor, (*53)

, (*54)

And a big thank-you to Jet Brains who provides such a great IDE:, (*55)

, (*56)

The Versions

16/10 2016

2.4.5

2.4.5.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

 

The Development Requires

template jade pug minification

02/10 2016

2.4.4

2.4.4.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

 

The Development Requires

template jade pug minification

21/09 2016

2.4.3.1

2.4.3.1 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

 

The Development Requires

template jade pug minification

21/09 2016

2.4.3

2.4.3.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

 

The Development Requires

template jade pug minification

21/09 2016

2.4.2

2.4.2.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

 

The Development Requires

template jade pug minification

30/08 2016

2.4.1

2.4.1.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

 

The Development Requires

template jade pug minification

30/08 2016

2.4.0

2.4.0.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

 

The Development Requires

template jade pug minification

30/08 2016

dev-js-phpize

dev-js-phpize https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

 

The Development Requires

template jade pug minification

13/08 2016

2.3.0

2.3.0.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

09/08 2016

2.2.1

2.2.1.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

09/08 2016

2.2.0

2.2.0.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

02/08 2016

2.1.3

2.1.3.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

02/08 2016

dev-hooks

dev-hooks https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

01/08 2016

2.1.2

2.1.2.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

01/08 2016

2.1.1

2.1.1.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

31/07 2016

2.1.0

2.1.0.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

30/07 2016

2.0.4

2.0.4.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

29/07 2016

2.0.3

2.0.3.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

26/07 2016

2.0.2

2.0.2.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

25/07 2016

2.0.1

2.0.1.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

21/07 2016

2.0.0

2.0.0.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

19/07 2016

2.0.0-alpha1

2.0.0.0-alpha1 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

09/07 2016

1.13.0

1.13.0.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

08/07 2016

1.12.5

1.12.5.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

08/07 2016

1.12.4

1.12.4.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

01/07 2016

1.12.3

1.12.3.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

30/06 2016

1.12.2

1.12.2.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

30/06 2016

1.12.1

1.12.1.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

28/06 2016

1.12.0

1.12.0.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

27/06 2016

1.11.3

1.11.3.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

25/06 2016

1.11.2

1.11.2.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

21/06 2016

1.11.1

1.11.1.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

21/06 2016

dev-attrs-data-complex

dev-attrs-data-complex https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

12/06 2016

1.11.0

1.11.0.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

11/06 2016

dev-mixin-scope-propagate

dev-mixin-scope-propagate https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

31/05 2016

dev-classes

dev-classes https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

31/05 2016

1.10.4

1.10.4.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

18/05 2016

1.10.3

1.10.3.0 https://github.com/pug-php/pug

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade pug minification

17/05 2016

1.10.2

1.10.2.0 https://github.com/ronan-gloo/jade.php

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade minification

10/05 2016

1.10.1

1.10.1.0 https://github.com/ronan-gloo/jade.php

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade minification

22/03 2016

dev-optimization

dev-optimization https://github.com/ronan-gloo/jade.php

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade minification

20/03 2016

1.10.0

1.10.0.0 https://github.com/ronan-gloo/jade.php

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade minification

19/03 2016

1.9.0

1.9.0.0 https://github.com/ronan-gloo/jade.php

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade minification

17/03 2016

1.8.1

1.8.1.0 https://github.com/ronan-gloo/jade.php

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade minification

14/03 2016

1.8.0

1.8.0.0 https://github.com/ronan-gloo/jade.php

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade minification

14/03 2016

1.8.0-rc1

1.8.0.0-RC1 https://github.com/ronan-gloo/jade.php

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade minification

13/03 2016

1.7.0

1.7.0.0 https://github.com/ronan-gloo/jade.php

HAML-like template engine for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

template jade minification