PHP中display和fetch的区别、mini函数、smarty判断、smarty循环

本文详细探讨了PHP Smarty模板中的display和fetch方法的区别,display实际上是调用fetch并输出内容。在mini函数部分,文章讨论了如何从面向过程转向面向对象的模板处理,遇到的问题以及解决方案。此外,还介绍了Smarty模板中的条件判断和循环结构的应用。
摘要由CSDN通过智能技术生成

一、display和fetch的区别

1、display调用的fetch方法,即:display() = echo fetch()
直接看源码,找到元凶:
$smarty->display('xx.html')//输出
$smarty->fetch('xx.html')//不输出
echo $smarty->fetch('xx.html')//输出

二、mini函数

1、mini模板类
function comp($file) {
   
$html = file_get_contents($file);
$html = str_replace('{$', '<?php echo $', $html);
$html = str_replace('}', ';?>', $html);
$compfile = $temp.'.php';
file_put_contents($compfile, $html);
return $compfile;
}
$sql = 'select ....'; //查询等等
$title = '这是标题';
include( comp('./xx.html') );
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>{
   $title}</h1>
</body>
</html>
2、最直接的修改:
class Mini{
   
function comp($file) {
   
$h = file_get_contents($file);
$h = str_replace('{$', '<?php echo $', $h);
$h = str_replace
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值