Home

Awesome

ci-pug

ci-pug is a library for CodeIgniter to enable Pug (renamed from Jade) Template Engine to render views with .pug or .jade extension.

If you use PHP 5.4 and composer, consider using the package version of ci-pug, the easiest way to install it, keep it up to date and to use it in your controllers.

Easy installation

Installation with Composer

Open a terminal in your CodeIgniter project

cd application
composer require pug-php/ci-pug
cd libraries
wget https://raw.githubusercontent.com/pug-php/ci-pug/master/{Jade,Pug}.php

If wget is not available on your OS, download Jade.php and Pug.php in your project application/libraries folder.

Be sure $config['composer_autoload'] = true; in your application/config/config.php file

How to use it?

application/controllers/Main.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends CI_Controller {

  public function index()
  {
    $this->load->library('pug');
    $this->pug->view('myview');
  }
}

application/views/myview.pug

doctype html
html(lang='en')
  head
    title My Pug View
  body
    h1 Hello World!

Pass variables

application/controllers/Main.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends CI_Controller {

  public function index()
  {
    $this->load->library('pug');
    $this->load->vars(array(
      'title' => 'My Pug View',
      'authors' => array(
        'Luke',
        'Leia',
        'Lando'
      )
    ));
    $this->pug->view('myview');
  }
}

application/views/myview.pug

doctype html
html(lang='en')
  head
    title=title
  body
    ul
      each author in authors
        li=author

Keep rendered views in cache

We recommend you to do it in production to serve the views faster.

application/controllers/Main.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends CI_Controller {

  public function index()
  {
    $this->load->library('pug', array(
      'cache' => true
    ));
    $this->pug->view('myview');
  }
}

Or you can use a custom storage folder:

$this->load->library('pug', array(
  'cache' => '/tmp/my-cache-folder'
));

If the folder does not exists, the library will try to create it.

Return the view instead of displaying it

$content = $this->pug->view('foo', true);
echo str_replace('<hr>', '----------', $content);
// works also with vars
$content = $this->pug->view('foo', array(
  'var1' => 1,
  'var2' => 2
), true);
// and also with view auto-selection
$content = $this->pug->view(true);
// see view auto-selection section below

Controller settings

This feature requires PHP 5.6 and allow you to specify settings for the whole controller.

class Welcome extends CI_Controller {

  const SETTINGS = [
    'cache' => true
    // here, you can add any option
  ];

  public foo() {
    $this->load->library('pug');
    $this->pug->view('welcome/foo'); // will use the SETTINGS class constant
  }

  public bar() {
    $this->load->library('pug', [
      'cache' => false // will override the SETTINGS class constant
    ]);
    $this->pug->view('welcome/bar');
  }
}

Tip: you can create and abstract controller with SETTINGS constant, then extend this abstract class from several controllers.

View auto-selection

If you do not specify the view file, the most logic one with the given class and method will be taken:

class Foo extends CI_Controller {
  public function index() {
    $this->load->library('pug');
    $this->pug->view(); // load application/views/foo/index.pug
    // or application/views/foo.pug if it does not exists
  }
  public function bar() {
    $this->load->library('pug');
    $this->pug->view(); // load application/views/foo/bar.pug
    // or application/views/foo/bar/index.pug if it does not exists
  }
}
class Yep extends CI_Controller {
  public function index() {
    $this->load->library('pug');
    $this->pug->view(array(
      'some' => 'var'
    )); // load application/views/yep/index.pug
    // or application/views/yep.pug if it does not exists
  }
  public function nop() {
    $this->load->library('pug');
    $content = $this->pug->view(true); // load application/views/yep/nop.pug
    // or application/views/yep/nop/index.pug if it does not exists
  }
  public function dontKnow() {
    $this->load->library('pug');
    $this->pug->view('yep/nop'); // load application/views/yep/nop.pug
    // or application/views/yep/nop/index.pug if it does not exists
  }
}

Custom views folder

If you do no store your .pug or .jade files in application/views, use the view_path setting:

$this->load->library('pug', array(
  'view_path' => APPPATH . 'pug-templates'
));