scorninpc/slim-smarty-view

基于Smarty模板组件构建的Slim框架4视图助手

2.0.6 2023-07-06 12:00 UTC

This package is auto-updated.

Last update: 2024-09-14 21:33:36 UTC


README

Latest Stable Version Total Downloads License

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

如何安装

通过Composer

$ composer require scorninpc/slim-smarty-view "2.*"

示例

<?php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

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


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

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

// Set view in Container
$container->set("view", function($container) {

  // Create smarty view
  $view = new \Slim\Views\Smarty(
    [
      'template_dir' => [__DIR__ . "/templates"],   // Where to put .tpl files
      'compile_dir' =>  __DIR__ . "/templates_c",   // Where to save compiled

      'cache_dir' =>  __DIR__ . "/templates_c",   // Where to cache
      'caching' => FALSE,               // Enable usa of cache
      'cache_lifetime' => 4600,           // Time for cache

      'force_compile' => TRUE,            // Force to compile .tpl all the time (compile .tpl every time . this is slow for production)
      'debugging' => FALSE,             // Enable debug console
      'compile_check' => TRUE,            // Enable check if need compile (this will check timestamp of file and compile again. set to false for performance)
    ]
  );

  return $view;
});

// Route
$app->get('/', function (Request $request, Response $response, $args) {

  return $this->get('view')->render($response, 'index.tpl', [
    'variable' => "Hello!",
  ]);
  
});

// Run
$app->run();

致谢

此项目仅是为了添加示例并将包添加到Packagist以配合Composer使用而进行的分支,所有关于这个优秀重构的荣誉都归功于Matheus Marques

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件