pug/slim 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pug/slim

Composer 安装命令:

composer require pug/slim

包简介

Pug template engine adapter for Slim

README 文档

README

Latest Stable Version Build Status Code Climate Test Coverage Issue Count StyleCI

For details about the template engine see phug-lang.com

Installation

Install with Composer:

composer require pug/slim

Usage with Slim 4

routes.php

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);
});

Usage with Slim 3

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

include 'vendor/autoload.php';

$slimOptions = []; // here you can pass 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();

PS: If you don't pass an application to the create method, we will automatically initialize one, so you can just do:

use Slim\Pug\PugRenderer;

include 'vendor/autoload.php';

$app = PugRenderer::create(null, './templates');

Usage with any PSR-7 Project

//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);

Template Variables

You can add variables to your renderer that will be available to all templates you render.

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

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

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

Data passed in via ->render() takes precedence over attributes.

$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"

By default, pug-php is used. But you can specify an other engine:

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

PS: Phug is automatically installed with default install since Pug-php 3 use it internally. But you can also install different renderer engine, for example tale-pug:

composer require talesoft/tale-pug
$app = PugRenderer::create(null, null, [
  'renderer' => \Tale\Pug\Renderer::class,
]);

Note that in this case, you have no guarantee that all options will work.

References

Credits

This project is forked from https://github.com/MarcelloDuarte/pug-slim And we added to it phug, pug-php 3, tale-jade and tale-pug support.

统计信息

  • 总下载量: 1.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固