pug-php / pug

Pug template engine for PHP
https://www.phug-lang.com
MIT License
392 stars 42 forks source link

How to use pug-php #104

Closed ArshSoni closed 7 years ago

ArshSoni commented 7 years ago

I'm trying to use the Readme but I can't seem to understand how to get this to work.

I have pug/jade already installed and that works. I've used the composer require etc and I have the composer files in my project. Where do I go from there?

kylekatarnls commented 7 years ago

Hi, if you use Phalcon, Symfony, Laravel or CodeIgniter, just implement the adapter: https://github.com/pug-php/pug#pug-in-your-favorite-framework

If you use raw PHP, take a look at the example: https://github.com/pug-php/pug/tree/master/example

Thanks,

ArshSoni commented 7 years ago

At the moment I am using .pug files and just compiling them using terminal. I am not using php classes but just php functions. I can't seem to understand the instructions on the readme page.

There is something about adding something to the 'jade construction' where is that? how do I get/create that

kylekatarnls commented 7 years ago

First, you have to understand you can buuild some simple applications entirely in .pug files but you should not. Pug is a template engine. To get the benefit of a template engine you should at least split the logic and the view. Pug should only be for your views. What we usualy call a controller calculate data, then pug will just display this data, in the example (https://github.com/pug-php/pug/tree/master/example), bootstrap.php is a small home-made framework and index.php is the router and controller that use it, but there is no problem to do a procedural framework, the controller-part is totally on your own. Maybe this example with no classes can help you:

index.php

<?php

// Load Composer auload and Pug:
include __DIR__ . '/vendor/autoload.php';
$pug = new \Pug\Pug();

$data = array(
  'view' => isset($_GET['view']) ? $_GET['view'] : null,
);

switch ($data['view']) {
  case 'contact':
    break;
  case 'date':
    $data['date'] = date('d/m/Y');
    break;
  default:
    $data['view'] = 'home';
}

echo $pug->render($data['view'] . '.pug', $data);

layout.pug

mixin menuLink(goTo)
  if $view == $goTo
    b
      block
  else
    a(href="?view=" . $goTo)
      block
doctype html
html(lang="en")
  head
    title My blog
  body
    nav
      +menuLink("home") Home
      +menuLink("date") Date
      +menuLink("contact") Contact
    block page

contact.pug

extends layout

block page
  h1 Contact me
  p at foo@bar.com

date.pug

extends layout

block page
  h1
    | Today is
    =date

home.pug

extends layout

block page
  h1 Welcome home

Demo: http://pug.selfbuild.fr/demo/procedural/