Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How? Fetches templates from a database?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
DenVeroid
Smarty n00b


Joined: 24 Jun 2003
Posts: 2

PostPosted: Tue Jun 24, 2003 5:02 pm    Post subject: How? Fetches templates from a database? Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Jun 24, 2003 9:22 pm    Post subject: Reply with quote

$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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jun 25, 2003 5:10 am    Post subject: Reply with quote

In addition to Monte's comments, there are also threads here at the forums that discuss this to some extent. These come to mind:

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=37&highlight=template+resource+dbx

and here http://www.phpinsider.com/smarty-forum/viewtopic.php?t=517.

xo boots
Back to top
View user's profile Send private message
DenVeroid
Smarty n00b


Joined: 24 Jun 2003
Posts: 2

PostPosted: Wed Jun 25, 2003 9:49 am    Post subject: Reply with quote

Спасибо
Я русский поэтому может непонятно объясняю так как я незнаю английского языка, ну если кто знает русский язык то може кто поможет мне разобраться.

Мне надо чтобы шаблоны грузились не из файла а из переменной, тоесть базы данных MySQL

я привёл код в начале, но он почему-то невыодит данныё, почему??
показывает просто пустую страницу.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP