Re: [SMARTY] Is it possible to retrieve a value from a template

From: Date: Sat, 08 Feb 2003 09:28:49 +0000
Subject: Re: [SMARTY] Is it possible to retrieve a value from a template
References: 1 2 3  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-7584@lists.php.net to get a copy of this message
Well, whether or not you pound your database doesn't really have much to do with the template system. Your main script (which makes the SQL calls and works with the $smarty template object) is going to decide at runtime which SQL queries to execute, and hopefully it won't perform more than is needed to get the job done... then it assigns the data that the template needs, and might just decide which template to use, and display it. Depending on your needs, you might have the script dynamically decide which template to show, or you might program some logic into the template to render whatever data is given it. The choice is up to you. I would try the first approach... for example (rough code): switch ($action) { case "news":
       $data = query("whatever");
       $data2 = query("bleh");
       $template = "news.tpl";
       break;
case "something":
       $data = query("some other query");
       $template = "bleh.tpl";
       break;
} $smarty->display($template); -Jason Jones LPA wrote:
Hi, I think i understand the concepts of templates (i hope ;)), but in my pages, i have a lot of blocs wich can be shown or not by the user... If I must execute all sql requests to use only 1, i think it's stupid ;)) then thats the reason I want to know if I must execute a sql before do it ;) for example if the user dont want to see the news bloc, I think its stupid to extract them ;)) Thnx Laurent ----- Original Message ----- From: "Jason Jones" <jason@tekscode.com> To: "LPA" <lpasseb@numericable.fr> Cc: <smarty-general@lists.php.net> Sent: Saturday, February 08, 2003 6:45 AM Subject: Re: [SMARTY] Is it possible to retrieve a value from a template
I think you're missing the whole beauty of the Smarty system. Smarty provides a clean abstraction between your PHP code and a display mechanism... in other words... let PHP do all the grunt work in preparing the data (and getting info/data from an SQL DB), assign all the relevant variables to the Smarty template, and then show the template... something like this, for example: script.php: $count = $DB->getOne("select foo from bar"); $smarty = new Smarty; $smarty->assign("count", $count); $smarty->display("thing.tpl"); thing.tpl <html><body>The count was {$count}</body></html> Maybe that's not what you were getting at. But besides that, the templates can be programmed with conditional logic to display the correct block and utilize the appropriate variables. HTH... -Jason Jones


« previous php.smarty.general (#7584) next »