Re: [SMARTY] Basic question about insert
| From: | messju mohr | Date: | Wed, 16 Jul 2003 23:19:40 +0000 |
| Subject: | Re: [SMARTY] Basic question about insert | ||
| References: | 1 2 3 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-9914@lists.php.net to get a copy of this message | ||
On Thu, Jul 17, 2003 at 12:58:39AM +0200, Luc Saint-Elie wrote:
> At 00:27 17/07/2003 +0200, messju mohr wrote:
> >"$smarty.server.HTTP_REFERER" is cached also (all parameters to insert
> >a cached). this means getGreeting always uses the same value. remove
> >the parameter from the {insert}-tag and use $_SERVER['HTTP_REFERER']
> >inside your getGreeting-function instead.
>
>
> Is it possible (and if yes how) to pass an object to insert ?
>
> Let's say I have a class that manages some database access stuff
> Among these database access stuff is a set of two function devoted to see
> "how many people are currently online".
>
> The result must not be cached so I have
>
> index.php
>
> [...]
> include 'include/smarty/lib/display.class.php';
> include 'include/smarty/lib/insert.fonctions.php';
> [...]
> $mydisplay = new DisplayClass;
> [...]
>
> then in insert.fonctions.php
> function insert_getNbreVisiteurs()
> {
> $myvisiteur = new DisplayClass;
> $myvisiteur->update_frequency = 3;
> $myvisiteur->timeout = 5;
> $nombre = $myvisiteur->getNbreEnLigne();
> if ($nombre == 1) {
> return '<strong>'.$nombre.'</strong> visiteur en ligne..
> vous !';
> } else {
> return '<strong>'.$nombre.'</strong> visiteurs en
> ligne';
> }
> }
>
> and in the tpl file
>
> {insert name="getNbreVisiteurs"}
>
> This works but this is stupid because I create two version of the object.
> How can I pass the current object ($mydisplay = new DisplayClass) to my
> insert function passing through the caching system.
hmm, the most simple (though not very nice) way would be
function insert_getNbreVisiteurs($params, &$smarty) {
global $mydisplay;
$mydisplay->update_frequency = 3;
...
[globals are bad]
another (also not übernice) way would be (since Smarty-2.5.0):
$smarty->register_object('display', $mydisplay);
...
function insert_getNbreVisiteurs($params, &$smarty) {
$myvisiteur =& $smarty->get_registered_object('display');
$myvisiteur->update_frequency = 3;
...
[misuse of registered objects is bad]
a third (also arguable) way would be:
$smarty->luc_display =& $mydisplay;
...
function insert_getNbreVisiteurs($params, &$smarty) {
$myvisiteur =& $smarty->luc_display;
$myvisiteur->update_frequency = 3;
[making up properties of class Smarty and hoping nobody else uses
them is bad]
choose what works best for you.
> Thanks in advance
>
> Luc