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

Database queries within templates

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
niol
Smarty n00b


Joined: 11 Apr 2005
Posts: 2

PostPosted: Mon Apr 11, 2005 1:00 pm    Post subject: Database queries within templates Reply with quote

Hi all,

When I discovered the smarty template engine, I lacked a feature I have found in another template engine (Templeet).

So I implemented a basic version of it for the Smarty Template engine.

It basically allows you to perform database queries within a template and do things like this :

Code:

{list query="SELECT
      news.nid,
      news.date,
      news.title,
      users.login

      FROM news,users

      WHERE
      news.uid = users.uid

      ORDER BY date DESC LIMIT 20" prefix="<ul>" trailer="</ul>"}

   <li><a href="{include file="news/news_link.tpl" date=$fld.date nid=$fld.nid title=$fld.title}">{$fld.title}</li>

{/list}


If this is of any interest for anybody, here is the GPLed source code :
[php:1:50b9b3a059]<?php
require_once 'shared.mysql.php';
/*
* Smarty plugin
* -------------------------------------------------------------
* File: block.list.php
* Type: block
* Name: list
* Purpose: This function displays the results of a db quey
* Params:
* - query : The database query
* - prefix : Displayed before the first result row if any
* - trailer : Displayed after the last result row if any
* -------------------------------------------------------------
*/
$smarty_mysql_result = NULL;

function smarty_block_list($params,$content,&$smarty,&$repeat){
global $smarty_mysql_result;
static $mysql_nested_depth = -1;

// Checking whether contents is set tells whether
// it is the beginning of the block or the end.
if (!isset($content)) {
// We are at the beginning of the block

// We go deeper
$mysql_nested_depth++;

$query = $params['query'];
if( DEBUG ) echo "($query)";
// --We do the query
// Run the query
if( !($smarty_mysql_result[$mysql_nested_depth] = smarty_mysql_query($query)) ){
// Query executed improperly

// We climb back
$mysql_nested_depth--;

// Do not repeat
$repeat = false;

}else{
// The query executed okay

// If we have at least one result
if( @mysql_num_rows($smarty_mysql_result[$mysql_nested_depth]) > 0 ){

// Assign the first row for display
$smarty->assign('fld',mysql_fetch_array($smarty_mysql_result[$mysql_nest
ed_depth]));

// We want to repeat
$repeat = true;

// Echo the prefix and return
echo $params['prefix'];
return;

}else{

// Do not repeat, no more content
$repeat = false;

// We climb back
$mysql_nested_depth--;

// Return nothing
return;

}

}

return;// We return nothing in case the function did not exit yet

}else{
// We have more content to display

// Checking for more results
if( $fields = mysql_fetch_array($smarty_mysql_result[$mysql_nested_depth]) ){

// --We have more result rows
// We want to repeat
$repeat = true;
// We assign the row for next display
$smarty->assign('fld',$fields);

// Return the contents with the good variables in it
return $content;

}else{// Last result row

// We climb back
$mysql_nested_depth--;

// Return the contents with the good variables in it and trailer
return $content.$params['trailer'];

}

return;// We return nothing in case the function did not exit yet
}

}

?>
[/php:1:50b9b3a059]
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Apr 11, 2005 4:51 pm    Post subject: Reply with quote

smarty is built to seperate display-logic from business logic.
your plugin seems to defeat this purpose.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
niol
Smarty n00b


Joined: 11 Apr 2005
Posts: 2

PostPosted: Mon Apr 11, 2005 6:00 pm    Post subject: Right but... Reply with quote

I totally agree with you, and as my app is growing, I am beginning to see the limits of this plugin. That is why I am planning to manipulate database items that have a strong meaning using PHP objects.

But this plugin is usefull if the application is small, or if you need to quickly get something from the database. That is why I did not post to the 'Feature request' forum.

But I thought that my code may be usefull someday...
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 -> Tips and Tricks 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