|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
njava Smarty n00b
Joined: 24 Aug 2004 Posts: 1
|
Posted: Tue Aug 24, 2004 6:21 am Post subject: Can smarty execute PHP code? |
|
|
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 (and if such thing is doable at all in smarty) instead of displaying it? Many thanks.
template file: content_table.html
Code: |
<table align="center" width="600">
<tr>
<td>{$Content}</td>
</tr>
</table> |
template file: table.html
Code: | <table border="0" align="center">
<tr>
<td>{$ContentTable}</td>
</tr>
</table>
|
file calling template:
Code: | <?php
include "../includes/class.smarty.php2.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
Code: | <?php
Some PHP code
?> |
function file: functions.php - to get output from source.php and generate template
Code: | <?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;
}
?> |
|
|
Back to top |
|
dthought Smarty Regular
Joined: 21 Apr 2003 Posts: 55 Location: Melbourne, Australia
|
Posted: Wed Aug 25, 2004 7:03 am Post subject: |
|
|
You can use the {php} tags... but really, you should avoid using PHP in your templates files at all costs. If you need to use the PHP tag, you've planned badly... there will always be a better solution |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|