php - CodeIgniter 不需要模板引擎

标签 php codeigniter phpmyadmin cakephp-1.3 codeigniter-2

请解释一下什么是模板引擎以及它的用途。下面的段落想表达什么?我不明白作者的解释。

Although CodeIgniter does come with a simple template parser that can be optionally used, it does not force you to use one. Template engines simply can not match the performance of native PHP, and the syntax that must be learned to use a template engine is usually only marginally easier than learning the basics of PHP. Consider this block of PHP code:

<ul>

<?php foreach ($addressbook as $name):?>

<li><?=$name?></li>

<?php endforeach; ?>

</ul>

Contrast this with the pseudo-code used by a template engine:

<ul>

{foreach from=$addressbook item="name"}

<li>{$name}</li>

{/foreach}

</ul>

Yes, the template engine example is a bit cleaner, but it comes at the price of performance, as the pseudo-code must be converted back into PHP to run. Since one of our goals is maximum performance, we opted to not require the use of a template engine.

最佳答案

模板引擎只是将后端代码嵌入到 html 中的一种方法。这在 Python 和 Ruby 等语言中比 PHP 更常见,因为 PHP 已经支持嵌入 html 代码。

对于那些来自其他后端语言的人来说,这个特定的模板引擎可能更容易,特别是 Python 或 Ruby,因为 Django 和 Rails 的模板语法非常相似。

虽然不一定是“模板引擎”,但我通常组织我的 Codeigniter 项目,其中有一个 template.php View ,其中包含骨架布局,并且站点/应用程序的每个页面的内容是加载在其中的单独 View template.php 文件。这样我就不会在每个页面上重写代码。

模板引擎完全取决于个人喜好——它们不是必需的,但有些人如果习惯的话,会觉得使用它们更舒服。人们对工作流程的思考越少,效率就越高!

关于php - CodeIgniter 不需要模板引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14904334/

相关文章:

php - 使用php的Mysql事件错误

mysql - 在phpMyAdmin Edit Index对话框中,保存后SQL语句为 "lost"

php - 将 esc_attr_e() 连接到 WordPress 中的变量问题

php - Laravel 5.2 每次请求都会生成一个新的session文件

javascript - 减少 AJAX 请求大小。与投票系统的简单聊天

php - Mysql 两张表,获取没有相同值的数据

php - CodeIgniter session 数据不工作

php - Codeigniter 分页和 JQuery 操作

php - MySQL GROUP BY 和条件

mysql - 不明白为什么这个 MySQL 函数在 12 点之后无法正常工作