Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Smarty template dilemma

Posted on 2004-08-23
2
Medium Priority
?
452 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to dynamically set the form action using jQuery.
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 …
Suggested Courses

783 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