projek-xyz/slim-plates

使用 Plates 模板引擎渲染 Slim 3 应用程序的视图。

v0.2.2 2016-10-09 23:22 UTC

README

VERSION LICENSE Actions Status Coveralls Code Climate Maintainability SymfonyInsight Grade

Slim 微框架 3 的 Plates 模板集成

使用 Plates 模板引擎渲染 Slim 3 应用程序的视图。

安装

通过 Composer

$ composer require projek-xyz/slim-plates --prefer-dist

需要 Slim 微框架 3 和 PHP 5.5.0 或更高版本。

用法

// Create Slim app
$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register Plates View helper:
// Option 1, using PlatesProvider
$container->register(new \Projek\Slim\PlatesProvider);

// Option 2, using Closure
$container['view'] = function ($c) {
    $view = new \Projek\Slim\Plates([
        // Path to view directory (default: null)
        'directory' => 'path/to/views',
        // Path to asset directory (default: null)
        'assetPath' => 'path/to/static/assets',
        // Template extension (default: 'php')
        'fileExtension' => 'tpl',
        // Template extension (default: false) see: http://platesphp.com/extensions/asset/
        'timestampInFilename' => false,
    ]);

    // Set \Psr\Http\Message\ResponseInterface object
    // Or you can optionaly pass `$c->get('response')` in `__construct` second parameter
    $view->setResponse($c->get('response'));

    // Instantiate and add Slim specific extension
    $view->loadExtension(new Projek\Slim\PlatesExtension(
        $c->get('router'),
        $c->get('request')->getUri()
    ));

    return $view;
};

// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->view->render('profile', [
        'name' => $args['name']
    ]);
})->setName('profile');

// Run app
$app->run();

注意:

  • 如果你使用的是 选项 1,请确保你的配置文件中已经有 $container['settings']['view']
  • 使用 Plates::render() 需要 Plates::setResponse(),否则将抛出 \LogicException

自定义函数

此组件将一些 Slim 函数暴露给你的 Plates 模板。

pathFor()

你可以使用此函数生成任何名为路由的 Slim 应用的完整 URL。例如:

<?php $this->layout('base-template') ?>

<?php $this->start('body') ?>
<h1>Hallo <?=$this->e($name)?></h1>
<small><?=$this->pathFor('profile', ['name'=> $name])?></small>
<?php $this->stop() ?>

baseUrl($permalink = '')

检索 Slim 应用的基础 URL。例如:

<a href="<?=$this->baseUrl()?>">Some Link</a>

或者你可以传递一个永久链接

<a href="<?=$this->baseUrl('some/path')?>">Some Other Link</a>

basePath()

检索 Slim 应用的基础 URL。例如:

<link rel="stylesheet" href="<?=$this->basePath().'asset/css/main.css'?>">

uriFull()

检索完整的请求 URI。

uriScheme()

检索 URI 的 方案 组件。

uriHost()

检索 URI 的 主机 组件。

uriPort()

检索 URI 的 端口 组件。

uriPath()

检索 URI 的 路径 组件。

uriQuery()

检索 URI 的 查询字符串 组件。

uriFragment()

检索 URI 的 片段 组件。

贡献

请参阅 CONTRIBUTINGCONDUCT 以获取详细信息。

许可证

此库是开源软件,受 MIT 许可证 授权。