包括一个带有包含的php文件到一个smarty模板中

时间:2013-02-28 13:05:55

标签: php smarty

我想将标题包含在使用智能模板的脚本中。从搜索这个网站,我部分在那里,但不完全:

{include file='/home/username/public_html/header.php'}

这成功地将图像包含在标题中,但两者都不包含标题包含的内容。其中一个是php文件,另一个是html(我的bootstrap导航栏)。我似乎从我的搜索中我需要制作一个插件,根据一篇帖子说这很简单,但我找不到如何实现这个目的的例子?


基于codefreaks inststructions,这就是我所做的。我确定说明是正确的,我只是没有正确解释它们,因为这没有显示任何内容。

以下是三个文件,它们的路径与public_html目录有关,以及我添加到它们的内容。一切都与我说的完全一样:这里没有任何词语是占位符。

档案1     sitewide/myheader.php

<?
ob_start(); 
--- I didn't change the original content here --
$output = ob_get_contents();
ob_end_clean();  ?>

文件2     newscript/includes/page_header.php

$smarty = new Smarty();
require "/home/username/public_html/sitewide/myheader.php";
$smarty->assign('myheader', $output);
$smarty->display('../themes/default/template/header.tpl');

文件3     newscript/themes/default/template/header.tpl

{$myheader}

3 个答案:

答案 0 :(得分:0)

我认为你不能把你的php文件包含在smarty中。

由于samrty是模板引擎,

首先执行PHP页面,然后将数据发送到智能页面。

解决方案:将所有需要的数据从php页面传递给smarty,并包含html页面和smarty变量。

答案 1 :(得分:0)

在收到您的反馈并对自己进行测试后,我发现您可能没有正确设置智能。按照http://www.smarty.net/quick_install上的说明正确安装。正确设置后的最终目录结构如下:

enter image description here

一旦你正确设置它,这就是我在文件中使用的代码:

的index.php

<?php
echo "hello";
require_once "libs/Smarty.class.php";
$smarty = new Smarty();
$smarty->setTemplateDir('smarty/templates');
$smarty->setCompileDir('smarty/templates_c');
$smarty->setCacheDir('smarty/cache');
$smarty->setConfigDir('smarty/configs');
require "header.php";

$smarty->assign('header', $output);
$smarty->testInstall();
$smarty->display('smarty.tpl');
echo "hello";
?>

的header.php:

<?php
ob_start();
?>
hello honey bunny
<?php
$output = ob_get_contents();
ob_end_clean();
?>

将smarty.tpl放在模板文件夹中!

{$header}hellos

答案 2 :(得分:0)

我正在尝试做一个非常相似的事情,除了我想要的是一些全局PHP常量,我使用 define 在一个独立的 constants.php 文件中定义。

我想在智能模板中使用相同的常量,所以我一直试图在smarty模板中包含 constants.php ,但这是更好的方法:

constants.php

  $_CONSTANT['TBL']                      = TRUE;  
  $_CONSTANT['FLD']                      = FALSE;
  $_CONSTANT['DB_READ_SUCCESS']          =     1;
  $_CONSTANT['DB_WRITE_SUCCESS']         =     1;
  $_CONSTANT['DB_WRITE_ERROR_UNKNOWN']   =  -100;    

  //*** Copy the $_CONSTANT array to PHP define() to make global PHP constants
  foreach($_CONSTANT as $key => $value) {
    define($key, $value);
  }

然后在我聪明的设置功能中我这样做:

foreach ($_CONSTANT as $key => $value) {
  Smarty->assign($key, $value);
}

而且我认为您在Smarty模板中真正想要的是变量(或常量)值,以使您的视图层与模型和控制器层分开。

发布脚本:

实际上,您可以使用此技术将使用Smarty的PHP常量传递给JavaScript,从而允许您在三个不同的环境中为一个地方声明常量:PHP,Smarty和JavaScript。

以下是:

从智能设置功能中调用以下内容:

  public function AssignJSConstants($values) {
    $js = '';
    foreach ($values as $key => $value) {
      if ($key != 'CR') {
        if (is_string($value)) {
          $js = $js.$key.' = "'.$value.'";';
        } else {
          $js = $js.$key.' = '.$value.';';
        }
        $js = $js.CR.'      ';
      }
    }
    $this->Smarty->assign('JavaScriptConstants', $js);
  }

然后在智能模板中声明以下内容:

<script>
  //php constants that exported to JavaScript
  {$JavaScriptConstants}
</script>

这将在您的HTML中为您提供:

<script>
  //php constants that exported to JavaScript
  TBL = 1;
  FLD = 0;
  DB_READ_SUCCESS = 1;
  DB_READ_ERROR_UNKNOWN = -10;
  DB_WRITE_SUCCESS = 1;
  DB_WRITE_ERROR_UNKNOWN = -100;
</script>
相关问题