2017 © Pedro Peláez
 

library pug-symfony

Pug template engine for Symfony

image

pug-php/pug-symfony

Pug template engine for Symfony

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 37 Versions
  • 15 % Grown

The README.md

Pug-Symfony

Latest Stable Version GitHub Actions StyleCI Test Coverage, (*1)

Pug template engine for Symfony, (*2)

This is the documentation for the ongoing version 3.0. Click here to load the documentation for 2.8, (*3)

Install

In the root directory of your Symfony project, open a terminal and enter., (*4)

composer require pug-php/pug-symfony

When you are asked to install automatically needed settings, enter yes., (*5)

It for any reason, you do not can or want to use it, you will have to add to your config/bundles.php file:, (*6)

Pug\PugSymfonyBundle\PugSymfonyBundle::class => ['all' => true],

Usage

Create Pug views by creating files with .pug extension in templates such as contact.pug:, (*7)

h1
  | Contact
  =name

Then inject Pug\PugSymfonyEngine to call it in your controller:, (*8)

namespace App\Controller;

use Pug\PugSymfonyEngine;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;

#[AsController]
class MyController
{
    #[Route('/contact')]
    public function contactAction(PugSymfonyEngine $pug)
    {
        return $pug->renderResponse('contact/contact.pug', [
            'name' => 'Us',
        ]);
    }
}

Or alternatively you can use \Pug\Symfony\Traits\PugRenderer to call directly ->render() from any method of a controller (or service):, (*9)

namespace App\Controller;

use Pug\Symfony\Traits\PugRenderer;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;

#[AsController]
class MyController
{
    use PugRenderer;

    #[Route('/contact')]
    public function contactAction()
    {
        return $this->render('contact/contact.pug', [
            'name' => 'Us',
        ]);
    }
}

No matter if your controller extends AbstractController as it can also render twig views, so it will just work the same as before rather you ->render('view.html.twig') or ->render('view.pug')., (*10)

Note: standard Twig functions are also available in your pug templates, for instance:, (*11)

!=form(form)

As per https://symfony.com/doc/current/forms.html, (*12)

Pass the FormView as usual from the controller:, (*13)

$task = new Task();
// ...

$form = $this->createFormBuilder($task)
    // ...
    ->getForm();

return $pug->renderResponse('home.pug', [
    'form' => $form->createView(),
]);

Configure

You can inject Pug\PugSymfonyEngine to change options, share values, add plugins to Pug at route level:, (*14)

// In a controller method
#[Route('/contact')]
public function contactAction(\Pug\PugSymfonyEngine $pug)
{
    $pug->setOptions(array(
      'pretty' => true,
      'pugjs' => true,
      // ...
    ));
    $pug->share('globalVar', 'foo');
    $pug->getRenderer()->addKeyword('customKeyword', $bar);

    return $pug->renderResponse('contact/contact.pug', [
        'name' => 'Us',
    ]);
}

If you use the PugRenderer trait, you don't need to inject the service again and can just use $this->pug., (*15)

Same can be run globally on a given event such as onKernelView to apply customization before any view rendering., (*16)

See the options in the pug-php documentation: https://phug-lang.com/#options, (*17)

Initial options can also be passed in parameters in your config/services.yaml:, (*18)

# config/services.yaml
parameters:
    # ...
    pug:
        expressionLanguage: php

Note: you can also create a config/packages/pug.yaml to store the Pug settings., (*19)

Globals of Twig are available in Pug views (such as the app variable to get app.token or app.environment) and any custom global value or service you will add to twig.yaml:, (*20)

# config/packages/twig.yaml
twig:
    # ...
    globals:
        translator: '@translator'

Make the translator available in every view:, (*21)

p=translator.trans('Hello %name%', {'%name%': 'Jack'})

Keys (left) passed to globals are the variable name to be used in the view, values (right) are the class name (can be '@\App\MyService') or the alias to resolve the dependency injection. It can also be static values such as ga_tracking: 'UA-xxxxx-x'., (*22)

If you need more advanced customizations to be applied for every Pug rendering, you can use interceptor services., (*23)

# config/services.yaml
parameters:
    # ...
    pug:
        interceptors:
            - App\Service\PugInterceptor
            # You can add more interceptors

services:
    # ...

    # They all need to be public
    App\Service\PugInterceptor:
        public: true

Then the interceptor would look like this:, (*24)

// src/Service/PugInterceptor.php
namespace App\Service;

use Pug\Symfony\Contracts\InterceptorInterface;
use Pug\Symfony\RenderEvent;
use Symfony\Contracts\EventDispatcher\Event;

class PugInterceptor implements InterceptorInterface
{
    public function intercept(Event $event)
    {
        if ($event instanceof RenderEvent) {
            // Here you can any method on the engine or the renderer:
            $event->getEngine()->getRenderer()->addKeyword('customKeyword', $bar);
            $event->getEngine()->getRenderer()->addExtension(MyPlugin::class);

            // Or/and manipulate the local variables passed to the view:
            $locals = $event->getLocals();
            $locals['foo']++;
            $event->setLocals($locals);

            // Or/and get set the name of the view that is about to be rendered:
            if ($event->getName() === 'profile.pug') {
                // if user variable is missing
                if (!isset($event->getLocals()['user'])) {
                    $event->setName('search-user.pug');
                    // Render the search-user.pug instead of profile.pug
                }
            }
        }
    }
}

As services, interceptors can inject any dependency in their constructor to use it in the intercept method:, (*25)

class PugInterceptor implements InterceptorInterface
{
    private $service;

    public function __construct(MyOtherService $service)
    {
        $this->service = $service;
    }

    public function intercept(Event $event)
    {
        if ($event instanceof RenderEvent) {
            $event->getEngine()->share('anwser', $this->service->getAnwser());
        }
    }
}

And interceptors are lazy-loaded, it means in the example above, neither PugInterceptor nor MyOtherService will be loaded if they are not used elsewhere and if the current request does not end with a pug rendering (pure-Twig view, API response, websocket, etc.) so it's a good way to optimize things you only need to do before pug rendering., (*26)

Deployment

In production, you better have to pre-render all your templates to improve performances using the command below:, (*27)

php bin/console assets:publish --env=prod

Security contact information

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

The Versions

13/07 2017

2.2.5

2.2.5.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

13/07 2017

2.2.4

2.2.4.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

12/07 2017

2.2.3

2.2.3.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

04/05 2017

2.2.2

2.2.2.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

04/05 2017

dev-fix-issue-11

dev-fix-issue-11

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

09/01 2017

dev-906c5a8

dev-906c5a8

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

09/01 2017

dev-css-helper

dev-css-helper

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

03/01 2017

2.2.1

2.2.1.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

02/01 2017

dev-analysis-8P33GA

dev-analysis-8P33GA

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

04/08 2016

2.1.5

2.1.5.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

04/08 2016

2.1.4

2.1.4.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

04/08 2016

2.1.3

2.1.3.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

02/08 2016

2.1.2

2.1.2.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

02/08 2016

2.1.1

2.1.1.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

02/08 2016

2.1.0

2.1.0.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

29/07 2016

2.0.1

2.0.1.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

29/07 2016

2.0.0

2.0.0.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

27/07 2016

1.3.3

1.3.3.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

30/06 2016

1.3.2

1.3.2.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

30/06 2016

1.3.1

1.3.1.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires

 

23/06 2016

1.3.0

1.3.0.0

Pug template engine for Symfony

  Sources   Download

MIT

The Requires