PHP code example of pug / slim

1. Go to this page and download the library: Download pug/slim library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pug / slim example snippets


use Slim\Pug\PugRenderer;

// ...

$app->get('/', function (Request $request, Response $response) {
    $renderer = new PugRenderer([
        // The base folder containing the pug template files
        // In this example we assume a home.pug file exist in it
        'basedir' => __DIR__ . '/../templates',
    ]);

    $viewData = [
        'name' => 'John',
    ];

    return $renderer->render($response, 'home.pug', $viewData);
});

use Slim\App;
use Slim\Pug\PugRenderer;

 Slim settings
$app = PugRenderer::create(new App($slimOptions), './templates');

$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->renderer->render($response, 'hello.pug', $args);
});

$app->run();

use Slim\Pug\PugRenderer;

e(null, './templates');

//Construct the View
$pugView = new PugRenderer('./path/to/templates', [
  // Here you can set options
]);

//Render a Template
$response = $pugView->render(new Response(), '/path/to/template.pug', $yourData);

// via the constructor
$templateVariables = [
  'title' => 'Title',
];
$pugView = new PugRenderer('./path/to/templates', [], $templateVariables);

// or setter
$pugView->setAttributes($templateVariables);

// or individually
$pugView->addAttribute($key, $value);

$templateVariables = [
  'title' => 'Title',
];
$pugView = new PhpRenderer('./path/to/templates', $templateVariables);

//...

$pugView->render($response, $template, [
    'title' => 'My Title',
]);
// In the view above, the $title will be "My Title" and not "Title"

$app = PugRenderer::create(null, null, [
  'renderer' => \Phug\Renderer::class,
]);

$app = PugRenderer::create(null, null, [
  'renderer' => \Tale\Pug\Renderer::class,
]);