Re: [SMARTY] Is it possible to retrieve a value from a template
| From: | M Mohr | Date: | Sat, 08 Feb 2003 10:35:06 +0000 |
| Subject: | Re: [SMARTY] Is it possible to retrieve a value from a template | ||
| References: | 1 2 3 4 5 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-7586@lists.php.net to get a copy of this message | ||
you can write a custom function that is on top of every block-template.
something like
{load_block name="news"}
this function should ensure that the specific data is retrieved from
the db and assigned to smarty, so the data can be used in the template
after load_block.
see: http://smarty.php.net/manual/en/plugins.functions.php
but be careful not to put to much (non-display-)logic inside the
template. things can get unmaintainable very fast.
i would say:
{load_block name="news"}
is okay, but
{fetch_data db="..." table="news" order="desc"
cols="for,bar,baz"}
isn't
have fun
messju
On Sat, Feb 08, 2003 at 11:12:03AM +0100, LPA wrote:
> If it was so easy, i didn't asked ;)
>
> The problem is.. The user (clients) can modify themsemf the templates, then
> i don't know which module print or not
>
> Look the php code, it's a part of my actual job... but as I don't want to
> recreate the wheel, i just want to use smarty. ;)
>
>
> function Traite_List_Caddy($nom) {
> global $db, $Application;
> $s = array(
> "'\{TOTO}'",
> "'\{BOUCLE_DEBUT}'",
> "'\{BOUCLE_FIN}'",
> "'\{PRODUIT_NOM}'",
> "'\{PRODUIT_NOMWLIEN}'",
> "'\{PRODUIT_QTE}'",
> "'\{PRODUIT_PRIX}'"
> );
> $r = array(
> '<?php echo "<a
>
> href=index.php?page=caddy_listing>'.$Application->Get_Parameter("TOTO").'</a
> >"; ?>',
> '<?php while ($db->Get_Record()) {?>',
> '<?php } ?>',
> '<?php echo $db->Field(\'produitnom\'); ?>',
> '<?php echo "<A
>
> href=\'produits/show.php?id=".$db->Field(\'produitid\')."\'>".$db->Field(\'p
> roduitnom\')."</A>"; ?>',
> '<?php echo $db->Field(\'cartqte\'); ?>',
> '<?php echo Calcul_Prix($db->Field(\'produitprix\')); ?>'
> );
> $st="";
> $texte = Ouvre_Fichier('include/theme/caddy_list.tpl');
> $st = '<?php include_once("functions_fonctions_templates.inc.php");';
> $st .= '$sql = "select * from cart, produits where
> cartcookie=\''.Get_CartID().'\' and produitid=cartidproduit";' ;
> $st .= '$db->Execute($sql); ?>';
> $st .= preg_replace ($s, $r, $texte);
> return $st;
> }
>
> function Genere_Page($nom) {
> global $db;
> $texte_index = Ouvre_Fichier($nom);
>
> if (strpos($texte_index, '{CADDY_LIST}')) {
> $texte = Traite_List_Caddy($txt);
> $texte_index = str_replace( '{CADDY_LIST}', $texte, $texte_index);
> }
>
>
> eval ('?>'.$texte_index);
> }
>
>
> ----- Original Message -----
> From: "Jason Jones" <jason@tekscode.com>
> To: "LPA" <lpasseb@numericable.fr>
> Cc: <smarty-general@lists.php.net>
> Sent: Saturday, February 08, 2003 10:28 AM
> Subject: Re: [SMARTY] Is it possible to retrieve a value from a template
>
>
> > 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
> > >>
> > >>
> > >>
> > >
> > >
> > >
> > >
> > >
> >
> >
> >
> >
> > --
> > Smarty General Mailing List (http://smarty.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> >
>
>
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
--
Reine Programmiersprache: Zu nichts außer für Spielzeuganwendungen verwendbar.
Ausnahme: Assembler.
Holger Schauer