zubdev/ci4smarty

此软件包已被 弃用 并不再维护。作者建议使用 https://github.com/seunex17/codeigniter4-smarty 软件包。

使用 Smarty 将 Codeigniter4 逻辑与展示分离

v0.1 2021-12-11 07:39 UTC

This package is auto-updated.

Last update: 2024-04-03 21:54:07 UTC


README

GitHub Hits

ci4-smarty

轻松在您的 CodeIgniter 4 项目中实现 Smarty 模板引擎。

注意:此库处于早期开发阶段。

描述

使用这个干净且语义化的预构建 Smarty 模板,在 CodeIgniter 4 中将应用逻辑与展示层分离。

要求

  • Codeigniter 4.x
  • Smarty 4.x

安装

最佳安装方式是通过 Composer,您可以使用以下命令:

composer require zubdev/ci4smarty

这将简单地添加最新版本的 ci4-smarty 作为模块到您的项目中。

手动安装

如果您选择不使用 Composer 进行安装,您可以下载此存储库,解压并将该文件夹重命名为 ci4-smarty。然后通过编辑 app/Config/Autoload.php 并将 Zubdev\Ci4Smarty 命名空间添加到 $psr4 数组中来启用它。例如,如果您将其复制到 app/Libraries

    $psr4 = [
        'Config'      => APPPATH . 'Config',
        APP_NAMESPACE => APPPATH,
        'Zubdev\Ci4Smarty' => APPPATH . 'Libraries/Ci4Smarty',
    ];

示例

以下是一个基本用法示例

  • PHP
<?php namespace App\Controllers;

use Zubdev\Ci4Smarty\Smartie;

class Home extends BaseController
{
	public function index() {
	   $smarty = new Smartie();
		
	   return $smarty->view('index');
	}
}

从控制器传递数据到视图

  • PHP
<?php namespace App\Controllers;

use Zubdev\Ci4Smarty\Smartie;

class Admin extends BaseController
{
	public function index() {
	
	    $smarty = new Smartie();
	    $data = [
	      'name' => 'John Doe',
	    ];
		
	   return $smarty->view('admin/dashboard', $data);
	}
}
  • 视图
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Document</title>
</head>
<body>
	Welcome to admin dashboard {$name}
</body>
</html>

全局可用性

通过修改 BaseController: app/Controllers/BaseController 来将库暴露给整个应用程序

<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use Zubdev\Ci4Smarty\Smartie;

class BaseController extends Controller
{
   protected $request;
   protected $smarty;
   
   public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
       $this->smarty = new Smartie();
    }
}
  • 控制器
<?php namespace App\Controllers;

class Home extends BaseController
{
	public function index() {
		
	   return $this->smarty->view('index');
	}
}

贡献

所有贡献都极为欢迎。