PHP smarty中为什么include file无效???

新手上路,请多包涵

图片描述

图片是我的站点目录,index.php在根目录下,head.php和实例化smarty类文件(config.php)在include目录下,templates是存放index.html和head.html文件的目录。

想问一下为什么我分别在index.php和head.php里面实例化了类,然后assign了一个属性,display了对应的html文件,然后在index.html里面使用{include file='head.html'}显示不存在我在head.php里面给的变量?分别访问index.php与head.php正常

帖代码:

index.php文件

<?php

require_once 'include/config.php';

$sm->assign('title','hello');

$sm->display('templates/index.html');

?>

index.html文件

{include file='head.html'}

{$title}

</body>

</html>

head.php文件

<?php

require_once 'config.php';

$sm->assign('hea','这是head头部');

$sm->display(FILES.'templates/head.html');

?>

head.html文件

<!DOCTYPE html>

<html>

<head>

    <title></title>

</head>

<body>

{$hea}

阅读 4.4k
1 个回答
{include file='head.html'}

只是引用了head.html模板文件,并不会通过head.php渲染的。

建议将head.php 修改为

<?php
$sm->assign('hea','这是head头部');
?>

index.php修改为

<?php

require_once 'include/config.php';

// 引入head.php文件
require_once 'head.php';

$sm->assign('title','hello');

$sm->display('templates/index.html');

?>

其他文件不变。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题