Home>Article>Backend Development> How to install smarty into MVC architecture (code example)

How to install smarty into MVC architecture (code example)

藏色散人
藏色散人 forward
2020-01-27 13:30:41 2514browse

Smarty是一个使用PHP写出来的模板引擎,是业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。

如何将smarty安装到MVC架构中?

首先是composer.json

{ "require": { "smarty/smarty": "^3.1" }, // 自动加载 // 可以在composer.json的autoload字段找那个添加自己的autoloader "autoload": { "psr-4": { "App\\Controllers\\": "Controllers/", "App\\Models\\": "Models/", "Tools\\": "Tools/" } } }

Models/Users.php


      

Controllers/UserController.php

getUsername(); echo 'username:'.$username;exit; $this->setTemplateDir(dirname(__DIR__) . '/Views/'); $this->setCompileDir(dirname(__DIR__) . '/runtime/Compile/'); // 将$username显示在对应的一个HTML文件当中,并且显示出来 // 表现层 user/user.html // 将变量发送给模板(html文件) $this->assign('username', $username); $this->assign('age', 20); // 显示模板 $this->display('user/user.html'); } }

Views/user/user.html

    Title 

{$username}

{$age}

在本机浏览器中访问

How to install smarty into MVC architecture (code example)

更多相关php知识,请访问php教程

The above is the detailed content of How to install smarty into MVC architecture (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete