blat/slim-plates

基于Plates模板组件构建的Slim Framework 4视图助手

v0.1.1 2022-08-01 15:15 UTC

This package is auto-updated.

Last update: 2024-09-29 06:14:53 UTC


README

这是一个基于Plates模板组件构建的Slim Framework视图助手。您可以使用此组件在Slim Framework应用程序中创建和渲染模板。

安装

通过Composer

$ composer require blat/slim-plates

需要Slim Framework 4、Plates 3和PHP 7.4或更高版本。

用法

use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Plates;
use Slim\Views\PlatesExtension;

require __DIR__ . '/vendor/autoload.php';

// Create Container
$container = new Container();
AppFactory::setContainer($container);

// Set Plates View in Container
$container->set('view', function () {
    return new Plates(__DIR__ . '/../templates');
});

// Create App
$app = AppFactory::create();

// Add Plates Extension Middleware
$app->add(new PlatesExtension($app));

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

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

自定义模板函数

PlatesExtension为您的Plates模板提供以下函数

  • urlFor() - 返回给定路由的URL。例如:/hello/world
  • fullUrFor() - 返回给定路由的URL。例如:http://www.example.com/hello/world
  • isCurrentUrl() - 如果提供的路由名称和参数对当前路径有效,则返回true。
  • currentUrl() - 返回当前路径,包括或不包括查询字符串。
  • basePath() - 返回基本路径。

您可以使用urlFor生成任何命名路由的完整URL,并使用isCurrentUrl确定是否需要将链接标记为活动状态,如下面的Plates模板示例所示

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

<h1>User List</h1>
<ul>
    <li><a href="<?= $this->urlFor('profile', ['name' => 'josh']) ?>" <?php if ($this->isCurrentUrl('profile', ['name' => 'josh'])): ?>}class="active"<?php endif ?>}>Josh</a></li>
    <li><a href="<?= $this->urlFor('profile', ['name' => 'andrew']) ?>">Andrew</a></li>
</ul>

MIT许可(MIT)

特此授予任何获得此软件及其相关文档副本(“软件”)的人免费使用权,在不受限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向提供软件的人员进行此类操作,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他原因,由此软件或其使用或其他交易而产生,无论是否存在。