Smarty不在模板文件中显示结果

时间:2014-05-20 19:22:38

标签: php mysql smarty

我创建的代码文件工作正常...但问题是我将文件添加到我的网站标题{include file =" header_featured.html"}它没有显示任何东西......但是如果用浏览器的.php文件名如www.domain.com/header_featured.php在浏览器中编写URL,显示结果有什么问题......?甚至cms / script的所有其他文件也包含在标题中并显示完美的结果..

header_featured.php

<?php

require_once "include/include.php";

global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);


$ads = array();

$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50"); 
while ($temp = mysql_fetch_assoc($featured_ads)) { 
    $record = array();
    $record['ad_id']      = $temp['id'];
    $record['ad_title']   = $temp['title'];

    //check for image if featured ad have image (Need to fetch only single image)
    $featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no"); 
    $img = mysql_fetch_assoc($featured_ads_images);
    $record['img_id']         = $img['id'];
    $record['ad_img']         = $img['picture'];
    $record['img_folder']     = $img['folder'];

    $ads[] = $record;
} 

$smarty->assign('ads', $ads);

$db->close();
if($db->error!='') { $db_error = $db->getError(); $smarty->assign('db_error',$db_error); }

$smarty->display('header_featured.html');
close();
?>

header_featured.html

 {foreach item=item  from=$ads} 
  {$item.ad_id}
  {$item.ad_title}
  {$item.img_id}
  {$item.ad_img}
  {$item.img_folder}
 {/foreach}

我将文件包含在标题中的方式

{include file="header_featured.html"}

1 个答案:

答案 0 :(得分:0)

您的PHP代码似乎应该简单写一下:

<?php include ('header_featured.php'); ?>

显示您的内容。

但实际上你应该将smarty部分移动到index.php,包含header_featured.php文件,并在index.php中显示Smarty模板。

例如我会这样做:

的index.php

<?php

require_once "include/include.php";

global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);

$header_featured = '';

require('header_featured.php');

$db->close();
if($db->error!='') { 
    $db_error = $db->getError(); 
    $smarty->assign('db_error',$db_error); 
}
else {
   $header_featured = $smarty->fetch('header_featured.tpl');
}

$smarty->assign('header_featured', $header_featured );

$smarty->display('index.tpl');


?>

header_featured.php

<?php 
$ads = array();

$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50"); 
while ($temp = mysql_fetch_assoc($featured_ads)) { 
    $record = array();
    $record['ad_id']      = $temp['id'];
    $record['ad_title']   = $temp['title'];

    //check for image if featured ad have image (Need to fetch only single image)
    $featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no"); 
    $img = mysql_fetch_assoc($featured_ads_images);
    $record['img_id']         = $img['id'];
    $record['ad_img']         = $img['picture'];
    $record['img_folder']     = $img['folder'];

    $ads[] = $record;
} 

$smarty->assign('ads', $ads);

在index.tpl

<html>
<head>
....
<body>
{$header_featured}
...
</body>
</html>

header_featured.tpl

 {foreach item=item  from=$ads} 
  {$item.ad_id}
  {$item.ad_title}
  {$item.img_id}
  {$item.ad_img}
  {$item.img_folder}
 {/foreach}