Solved

Smarty template dilemma

Posted on 2004-08-23
2
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

735 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