All Projects → pug-php → pug-symfony

pug-php / pug-symfony

Licence: MIT license
Pug (Jade) template engine for Symfony

Programming Languages

PHP
23972 projects - #3 most used programming language
Pug
443 projects

Projects that are alternatives of or similar to pug-symfony

web-generator
👑 Gulp based task runner which creates HTML output from Pug HTML templates
Stars: ✭ 13 (-67.5%)
Mutual labels:  minify, pug, jade
Jade
Jade.go - pug template engine for Go (golang)
Stars: ✭ 251 (+527.5%)
Mutual labels:  pug, jade
Bootstrap3 Pug Former jade Node Express Grunt
Bootstrap 3 templated by Jade
Stars: ✭ 242 (+505%)
Mutual labels:  pug, jade
tale-pug
Tale Pug is the popular JavaScript Template Engine Pug, formerly Jade, for PHP!
Stars: ✭ 32 (-20%)
Mutual labels:  pug, jade
Laravel Pug
Pug view adapter for Laravel and Lumen
Stars: ✭ 130 (+225%)
Mutual labels:  pug, jade
Pug As Jsx Loader
Stars: ✭ 168 (+320%)
Mutual labels:  pug, jade
Glup
Some of the gulp tutorial -《gulp笔记》
Stars: ✭ 136 (+240%)
Mutual labels:  minify, jade
Pug Sass Boilerplate Starter Kit
A Front-end template for building web apps or sites using Pug(Jade) and Sass
Stars: ✭ 92 (+130%)
Mutual labels:  pug, jade
frontend-starter-kit-with-gulp
Frontend Starter Kit with Gulp for either Themeforest Projects or customizable projects.
Stars: ✭ 34 (-15%)
Mutual labels:  minify, pug
emitty
A platform for finding dependencies between files and building tools for incremental compilation or build.
Stars: ✭ 69 (+72.5%)
Mutual labels:  pug, jade
pretty-harp-jade-skeleton
💀 Harp & Jade/Pug skeleton theme for a personal blog
Stars: ✭ 15 (-62.5%)
Mutual labels:  pug, jade
Jade Html5 Boilerplate
HTML5 Boilerplate ported to Jade. Great as a drop and go markup skeleton for Express apps.
Stars: ✭ 111 (+177.5%)
Mutual labels:  pug, jade
Node.js Bootstrap Starter Template
Node.js, Express, Pug, Twitter Bootstrap, Starter Template
Stars: ✭ 107 (+167.5%)
Mutual labels:  pug, jade
Compile Hero
🔰Visual Studio Code Extension For Compiling Language
Stars: ✭ 169 (+322.5%)
Mutual labels:  pug, jade
Wordless
All the power of Pug, Sass, Coffeescript and WebPack in your WordPress theme. Stop writing themes like it's 1998.
Stars: ✭ 1,374 (+3335%)
Mutual labels:  pug, jade
pypugjs
PugJS syntax adapter for Django, Jinja2 and Mako templates
Stars: ✭ 237 (+492.5%)
Mutual labels:  pug, jade
Expug
Pug templates for Elixir
Stars: ✭ 74 (+85%)
Mutual labels:  pug, jade
Gulp Pug Sass Seed
🍹 A Pug and Sass starter project using gulp for task automation.
Stars: ✭ 84 (+110%)
Mutual labels:  pug, jade
Frontend-StarterKit
Frontend StarterKit - [Gulp 4, Pug, SCSS, ES6+]
Stars: ✭ 13 (-67.5%)
Mutual labels:  pug, pug-template-engine
idiots.win
Google Autocomplete Guessing Game
Stars: ✭ 26 (-35%)
Mutual labels:  pug, jade

Pug-Symfony

Latest Stable Version GitHub Actions StyleCI Test Coverage

Pug template engine for Symfony

This is the documentation for the ongoing version 3.0. Click here to load the documentation for 2.8

Install

In the root directory of your Symfony project, open a terminal and enter.

composer require pug-php/pug-symfony

When your are asked to install automatically needed settings, enter yes.

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

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

Usage

Create Pug views by creating files with .pug extension in app/Resources/views such as contact.pug:

h1
  | Contact
  =name

Note: standard Twig functions are also available in your pug templates, for instance:

!=form_start(form, {method: 'GET'})

Then call it in your controller:

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class MyController extends AbstractController
{
    /**
     * @Route("/contact")
     */
    public function contactAction()
    {
        return $this->render('contact/contact.pug', [
            'name' => 'Us',
        ]);
    }
}

Configure

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

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

Same can be ran globally on a given event such as onKernelView to apply customization before any view rendering.

See the options in the pug-php documentation: https://phug-lang.com/#options

Initial options can also be passed in parameters in your config/services.yaml:

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

Note: you can also create a config/packages/pug.yaml to store the Pug settings.

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:

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

Make the translator available in every views:

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'.

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

# 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:

// 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:

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.

Deployment

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

php bin/console assets:publish --env=prod
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].