Solved

Smarty template dilemma

Posted on 2004-08-23
2
428 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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