Solved

Smarty template dilemma

Posted on 2004-08-23
2
419 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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now