linux counter image.

This site uses PHP and PUG

bartonlp.org

My Home Weather Station

About My Weather Station

Pug was formally Jade. You can find out all about it at pugjs.org or at github.com.

These are two examples of using Pug to render content with PHP. The first one really does not need PHP per say. It could have been a plain HTML file if it were not for the need of my framework to get some variables.

The fist examples uses Pug pug --client --no-debug index.pug to gererate a JavaScript file from the pub file.

We loaded pug via npm sudo npm install -g pug-cli. The JavaScript is used to generate the output.

PHP Using JavaScript File

<?php
// Get info for SiteClass Framework.  
$_site = require_once(getenv("SITELOAD") . '/siteload.php');
$S = new $_site->className($_site);

$id = $S->LAST_ID;
$ip = $_SERVER['REMOTE_ADDR'];
$date = date("l F j, Y");
$hits = $S->getHitCount();
$via = $_GET['via'];

$y = 0;
foreach(['pub-via-js.php', 'pug/pug-example.js'] as $file) {
  $x = filemtime($file);
  if($x > $y) {
    $y = $x;
  }
}

$mtime = date('Y-m-d H:i:s T', $y); // Last Modified

// Now use the 'pug-example.js' file that was generated from the index.pug file.
// Note this is all generated via javascript.
// The function 'template()' was created by running
// pug --client --no-debug index.pug
// which created the function and the text to interperet.

echo <<<EOF
<script src="pug/pug-example.js"></script>
<script>
var pug_html = template({args: {
  copyright: "2016 Barton Phillips",
  author: "Barton Phillips http://www.bartonphillips.com",
  desc: "Pug Example using javascript file",
  msg: "Counter Reset Sep 20, 2016",
  title: "Pug from javascript",
  via: "$via",
  footer: $hits,
  today: "$date",
  ip: "$ip",
  id: $id,
  mtime: "$mtime",
}});

document.write(pug_html);
</script>
EOF;
Run Pug via JavaScript

The second example uses pug-php. We got pug-php via composer.

composer require pug-php/pug
composer install

Then we instantiate pub via $pug = new Pug\Pug();

PHP Using Pug File

<?php
// Get info for SiteClass Framework NOT NEEDED except to get some variables.
$_site = require_once(getenv("SITELOAD") . '/siteload.php');
$S = new $_site->className($_site);

// The pug-php was loaded via 'composer'. Run
// composer require pug-php/pug
// composer install

use Pug\Pug;

$pug = new Pug(); // Instantiate Pug in namespace Pug.

// Array to pass to pug

$args = array(
              "copyright"=>"2016 Applied Technology Resources Inc.",
              "title"=>"Applitec Inc.",
              "author"=>"Barton Phillips",
              "desc"=>"Pug using pug-php",
              "msg"=>"Content Reset Sep. 20, 2016",
              "site"=>"Applitec",
              "title"=>"PHP+PUG",
             );

// Fill in some extra stuff
$args['id'] = $S->LAST_ID;
$args['ip'] = $_SERVER['REMOTE_ADDR'];
$args['today'] = date("l F j, Y");
$args['footer'] = $S->getHitCount();
$args['via'] = $_GET['via'];

$y = 0;
foreach(['pug-via-php.php', 'pug/pug-example.pug', 'pug/layout.pug'] as $file) {
  $x = filemtime($file);
  if($x > $y) {
    $y = $x;
  }
}

$args['mtime'] = date('Y-m-d H:i:s T', $y); // Last Modified

$output = $pug->render('pug/pug-example.pug', array('args'=>$args));

echo $output;
Run Pug via PHP

Here is the original pug file.

PUG FILE

extends layout

block content
    h1 This is an Example of PUG via #{args.via}
    p There are two example file.
    ul
      li
        a(href="pug-via-js.php?via=JavaScript") This uses a java script image of the pug-example.pug file.
      li
        a(href="pug-via-php.php?via=PHP") This one uses PHP to render the pug-example.pug file.
    

And here is this file which is also a pug file.

THIS FILE

<?php
$_site = require_once(getenv("SITELOAD") . '/siteload.php');
$S = new $_site->className($_site);
ErrorClass::setNoEmailErrs(true);
ErrorClass::setDevelopment(true);

$h->banner = "<h1>Pug Examples</a>";

$indexpug = file_get_contents("pug/pug-example.pug");
$thisfile = file_get_contents("pug-examples.php");
$thisfile = preg_replace(["/</", "/>/"], ["<", ">"], $thisfile);
$thisfilepug = file_get_contents("pug/pug-example-index.pug");
$example1 = file_get_contents("pug-via-js.php");
$example1 = preg_replace(["/</", "/>/"], ["<", ">"], $example1);
$example2 = file_get_contents("pug-via-php.php");
$example2 = preg_replace(["/</", "/>/"], ["<", ">"], $example2);

use Pug\Pug;

$pug = new Pug(); // Instantiate Pug in namespace Pug.

// Array to pass to pug

$args = array(
              "copyright"=>$S->copyright,
              "title"=>"Pug Example",
              "author"=>"Barton Phillips",
              "desc"=>"Pug Example",
              "msg"=>"Content Reset Sep. 20, 2016",
              "site"=>"Bartonlporg",
              "title"=>"PHP+PUG Example",
             );

// Fill in some extra stuff
$args['id'] = $S->LAST_ID;
$args['ip'] = $_SERVER['REMOTE_ADDR'];
$args['today'] = date("l F j, Y");
$args['footer'] = $S->getHitCount();
$args['via'] = $_GET['via'];
$args['indexpug'] = $indexpug;
$args['example1'] = $example1;
$args['example2'] = $example2;
$args['thisfile'] = $thisfile;
$args['thisfilepug'] = $thisfilepug;

$y = 0;
foreach(['pug-examples.php', 'pug/pug-example-index.pug', 'pug/layout.pug'] as $file) {
  $x = filemtime($file);
  if($x > $y) {
    $y = $x;
  }
}

$args['mtime'] = date('Y-m-d H:i:s T', $y); // Last Modified
//error_log("pug-example mtime: {$args['mtime']}");

// I could also use $args as an object but would hove to do ['args'=>(array)$args] instead.

$output = $pug->render('pug/pug-example-index.pug', array('args'=>$args));

echo $output;

THIS FILE PUG

extends layout

append stylesheets
  link(rel="stylesheet" href="http://bartonphillips.net/css/theme.css")
  style.
    .syntaxhighlighter {
      height: 10rem;
      font-size: .8rem !important;
    }
    code {
      background-color: lightgray;
      padding: .1rem .5rem;
    }

append jscripts
  script(src="http://bartonphillips.net/js/syntaxhighlighter.js")

block content

  p
    b Pug 
    | was formally 
    b Jade
    | . You can find out all about it at 
    a(href="https://pugjs.org/api/getting-started.html") pugjs.org
    |  or at 
    a(href="https://github.com/pugjs/pug") github.com
    | .

  p 
    | These are two examples of using 
    b Pug 
    |  to render content with PHP. The first one really does not need PHP per say. 
    | It could have been a plain HTML file if it were not for the need of my framework to get some variables.

  p 
    | The fist examples uses 
    b Pug 
    code pug --client --no-debug index.pug
    |  to gererate a JavaScript file from the 
    b pub
    |  file.

  p 
    | We loaded 
    b pug
    |  via npm 
    code sudo npm install -g pug-cli
    | . The JavaScript is used to generate the output.
  
  p
    caption PHP Using JavaScript File 
  
  pre(class="brush: php") #{args.example1}

  a(href="/pug-via-js.php?via=JavaScript") Run Pug via JavaScript
  
  p 
    | The second example uses 
    b pug-php
    | . We got 
    b pug-php
    |  via 
    b composer.
  
  pre(class="brush: plain") composer require pug-php/pug
    | composer install

  p 
    | Then we instantiate 
    b pub
    |  via 
    code $pug = new Pug\Pug();

  p
    caption PHP Using Pug File
  
  pre(class="brush: php") #{args.example2}

  a(href="/pug-via-php.php?via=PHP") Run Pug via PHP
  
  p 
    | Here is the original 
    b pug
    |  file.

  p
    caption PUG FILE 
  
  pre(class="brush: plain") #{args.indexpug}

  p And here is this file which is also a 
    b pug
    |  file.

  p
    caption THIS FILE

  pre(class="brush: php") #{args.thisfile}
 
  p
    caption THIS FILE PUG

  pre(class="brush: plain") #{args.thisfilepug}

  p 
    | As you can see the 
    b pug
    |  is more concise than HTML with much less typing. Also 
    b pug
    |  has a nice 
    code extends
    |  syntax which lets you load things like headers and footers from other 
    b pub
    |  files.

As you can see the pug is more concise than HTML with much less typing. Also pug has a nice extends syntax which lets you load things like headers and footers from other pub files.