|
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 |
DenVeroid Smarty n00b
Joined: 24 Jun 2003 Posts: 2
|
Posted: Tue Jun 24, 2003 5:02 pm Post subject: How? Fetches templates from a database? |
|
|
I have made so
[php:1:3cdcf61895]
<?php
// test.php
require 'Smarty.class.php';
$smarty = new Smarty;
$smarty->caching = true;
$hostname = "localhost";
$dbUser = "root";
$dbPass = "root";
$dbName = "sw";
$conn = mysql_connect($hostname, $dbUser, $dbPass) or die("Cannot connect to the database");
mysql_select_db($dbName);
// Fetches templates from a database
$result=@mysql_query("SELECT * FROM template where tpl_name='mytem' AND template_show='5' ") or die("error");
while($row=mysql_fetch_array($result)) {
$tpl_content_row = $row["tpl_content"];
}
// Fetches data from a database
$sql = "SELECT * FROM news";
// get all the products from the table
$res = mysql_query($sql);
$results = array();
$i=0;
while ($r=mysql_fetch_array($res)) {
$bgColorS = ($bgColorS == "green" ? "red" : "green");
$bgColorT = ($bgColorT == "#F4F4F4" ? "#eceff2" : "#F4F4F4");
$tmp = array(
'id' => $r['post_id'],
'title' => $r['title'],
'body' => $r['body'],
'bgColorS' => "".$bgColorS."",
'bgColorT' => "".$bgColorT."",
'more'=> $r['more']
);
$results[$i++] = $tmp;
}
$smarty->assign('results', $results);
echo $smarty->fetch($tpl_content_row);
?>
$tpl_content_row Contains :
{section name=list start=0 loop=$results step=0}
<table align=center width=80% cellSpacing=0 cellPadding=0>
<tr bgcolor=c0c0c0>
<td bgcolor=FFFFFF>
<br>
<table border=0 width=100%>
<tr>
<td bgcolor="{$results[list].bgColorT}" width=100%>
{$smarty.section.list.rownum}
<a href=show.php?id={$results[list].id}>{$results[list].title}</a> <br>
{$results[list].body}<br>
</td>
</tr>
</table>
<p><br>
</p>
</td>
</tr>
</table>
{/section}
[/php:1:3cdcf61895]
When I have opened : http://localhost/sw/admin/Smarty/test.php
But why the empty page show???
What not so ??? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Jun 24, 2003 9:22 pm Post subject: |
|
|
$smarty->fetch() is expecting a template resource handle, not the template contents itself. By default this would be a file under your $template_dir
$smarty->fetch('index.tpl');
Since you want to fetch yours from a database, you need a custom resource handle:
$smarty->fetch('db:index.tpl');
where "db" is the resource type and "index.tpl" is the resource value. See the documentation on how to setup custom resource functions.
http://smarty.php.net/manual/en/plugins.resources.php
Monte |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
DenVeroid Smarty n00b
Joined: 24 Jun 2003 Posts: 2
|
Posted: Wed Jun 25, 2003 9:49 am Post subject: |
|
|
Спасибо
Я русский поэтому может непонятно объясняю так как я незнаю английского языка, ну если кто знает русский язык то може кто поможет мне разобраться.
Мне надо чтобы шаблоны грузились не из файла а из переменной, тоесть базы данных MySQL
я привёл код в начале, но он почему-то невыодит данныё, почему??
показывает просто пустую страницу. |
|
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
|