Solved

Smarty template dilemma

Posted on 2004-08-23
2
427 Views
Last Modified: 2008-03-17
I am trying to generate a page using Smarty templates. Everything works fine except that instead of parsing
{$Content} in content_table.html I get a raw PHP code from the source.php file. My question is how to make
Smarty parse PHP instead of displaying it? Many thanks.

template file: content_table.html
====================================
<table align="center" width="600">
<tr>
<td>{$Content}</td>
</tr>
</table>

template file: table.html
===================================
<table border="0" align="center">
  <tr>
    <td>{$ContentTable}</td>
  </tr>
</table>


file calling template:
====================================
<?php
include "../includes/class.smarty.phplevel2.php";
include "../includes/functions.php";

$pageTitle="Object-oriented";
$Table = ShowTable($pageTitle);
?>

<html>
<head>
      <title><?php print $pageTitle;?></title>
</head>

<body>
<?php print $Table;?>

</body>
</html>

source file: source.php
====================================
<?php

Some PHP code

?>


function file: functions.php
=================================
<?php
function GetContent($pageTitle){

      $arrFile=Array("Object-oriented"=>"source.php");
      //putting source.php code in a string                               
      $pageContent = file_get_contents(trim($arrFile[$pageTitle]));
            
return $pageContent;
}

function ShowTable($pageTitle){

$smarty = new SmartyPHP();

$pageContent = GetContent($pageTitle);

$smarty->assign(array("Content"=>$pageContent));
$ContentTable = $smarty->fetch("content_table.html");

$smarty->assign(array("ContentTable"=>$ContentTable))
                                
$Table = $smarty->fetch("table.html");

return $Table;

}
?>
0
Comment
Question by:njava
2 Comments
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 11878544
you need to extract the variables from the html code first:

for only simple variables:
preg_match_all('/\\{\\$([a-zA-Z_][a-zA-Z0-9_]*)\\}/U', $HTMLCode, $matches);

for everything:
preg_match_all('/\\{\\$(.*)\\}/U', $HTMLCode, $matches);

now you'll have $matches[0] to have all the matched variables... and you replace them with the variable variables in $matches[1]

if (is_array($matches[0]) and count($matches[0])) {
      for ($i=0;$i<count($matches[0]);$i++) {
            $HTMLCode = str_replace($matches[0][$i], $$matches[1][$i], $HTMLCode);
      }
}
0
 
LVL 3

Accepted Solution

by:
gnudiff earned 125 total points
ID: 11891905
Side note.

Unless you are doing something I didn't grasp (like creating new Smarty plugin funcs), I would advise not mixing PHP and Smarty for output.
a PHP page would be better off leaving all the output for Smarty.

ie.
PHP:
----
...
$tpl = new SmartyPage;
... getting all the variables and doing calcs
$tpl->assign('Content', $mycontent);
$tpl->display('my_smarty_template.html');
// END OF PHP FILE
 ?>
---
and leave all the output from <html> to </html> to template(s).
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question