简体   繁体   中英

Including a php file with includes into a smarty template

I want to include my header into a script that uses smarty templates . From searching this site, I am partially there, but not quite:

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

This successfully includes the image in the header, but neither of two includes the header contains. One of the includes is a php file, and the other is html (my bootstrap nav bar). I seems from my searches that I need to make a plugin, which according to one post is "easy", but I can't find an example of how to accomplish this?


based on codefreaks inststructions, here's what I did. I'm sure the instructions are correct, I'm just not interpreting them correctly, as this isn't displaying anything.

Here are the three files, with their paths in relation to the public_html directory, and what I added to them. Everything is exactly as I put it: no words here are placeholders.

file 1 sitewide/myheader.php

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

File 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');

File 3 newscript/themes/default/template/header.tpl

{$myheader}

I dont think you can include your php file in smarty.

As samrty is a template engine,

PHP pages executes first, then it sends the data to your smarty page.

Solution : Pass all data needed from php page to smarty, and include html page, with smarty variables.

After getting feedback from you and testing myself, I figured out you might not have set up smarty properly. Follow instructions at http://www.smarty.net/quick_install to install it properly. My final directory structure after setting up properly looks like:

在此处输入图片说明

Once you have it set it up properly this is code I used in files:

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();
?>

Put smarty.tpl in templates folder!

{$header}hellos

I am trying to do a very similar thing except all I want is some global PHP constants that I have defined in a stand alone constants.php file using define .

I want those same said constants in my smarty templates so I've been trying to include constants.php in the smarty template but this is better a better way to do this:

In 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);
  }

Then in my smarty set up function I do this:

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

And really I think variable (or constant) values is all you really want in your Smarty templates to keep your view layer separate from your model and controller layers.

Post Script:

In fact you can use this technique to pass your PHP constants, using Smarty, to JavaScript allowing you to declare you constants in one place for three different environments: PHP, Smarty, and JavaScript.

Here's how:

Call the following from your smarty set up function:

  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);
  }

And then declare the following in your smarty template:

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

Which will give you this in your 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM