PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.smarty.general > DB_DataObject and Smarty
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
DB_DataObject and Smarty

Réponse
 
LinkBack Outils de la discussion
Vieux 05/10/2006, 19h30   #1
Chris Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut DB_DataObject and Smarty

Does anyone know of any tutorials out there that show how I can get smarty
working with DBDO? I've searched but haven't been able to come up with
anything beyond using DB::getAll() method. I'd rather stay away from using
that if at all possible. I'd rather use DBDO::find(). Here's one (among
many) attempt I made to get it to work:

sandbox.php:
<?php
$users = new Users();
$users->country = 'USA';
$users->limit( 10 );
$users->find();

$smarty = new Smarty();
$smarty->assign( 'users', $users );
$smarty->display( 'sandbox.tpl' );

?>

sandbox.tpl
<html><head><title></title></head><body>
{section name=bar loop=$users}
Current User: {$users[bar].uid}<br>
{/section}
</body></html>

but doing that gives me an error saying that it cannot use object of type
Users as array. Am I just doing it wrong? Do I need to implement the
iterator interface in the Users class?

If someone can offer any pointers as to what I'm doing wrong or if you can
point me to a howto/tutorial on the web, that'd be awesome!

thnx,
Chris
  Réponse avec citation
Vieux 05/10/2006, 21h42   #2
Ken Snyder
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] DB_DataObject and Smarty

Yes, you probably do need to implement the iterator interface in the
users class. I believe that Smarty uses foreach loops for all iteration
so that objects with iterator interfaces can be used. You should also
get the same error/result if you were to add the following to sandbox.php:

foreach( $users as $user ) {
echo "Current User: ".$user['id']."<br>";
}


--Ken


Chris Boget wrote:
> Does anyone know of any tutorials out there that show how I can get
> smarty working with DBDO? I've searched but haven't been able to come
> up with anything beyond using DB::getAll() method. I'd rather stay
> away from using that if at all possible. I'd rather use DBDO::find().
> Here's one (among many) attempt I made to get it to work:
>
> sandbox.php:
> <?php
> $users = new Users();
> $users->country = 'USA';
> $users->limit( 10 );
> $users->find();
>
> $smarty = new Smarty();
> $smarty->assign( 'users', $users );
> $smarty->display( 'sandbox.tpl' );
>
> ?>
>
> sandbox.tpl
> <html><head><title></title></head><body>
> {section name=bar loop=$users}
> Current User: {$users[bar].uid}<br>
> {/section}
> </body></html>
>
> but doing that gives me an error saying that it cannot use object of
> type Users as array. Am I just doing it wrong? Do I need to
> implement the iterator interface in the Users class?
>
> If someone can offer any pointers as to what I'm doing wrong or if you
> can point me to a howto/tutorial on the web, that'd be awesome!
>
> thnx,
> Chris

  Réponse avec citation
Vieux 06/10/2006, 02h09   #3
Chris Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] DB_DataObject and Smarty

> Yes, you probably do need to implement the iterator interface in the users
> class. I believe that Smarty uses foreach loops for all iteration so that
> objects with iterator interfaces can be used. You should also get the
> same error/result if you were to add the following to sandbox.php:
> foreach( $users as $user ) {
> echo "Current User: ".$user['id']."<br>";
> }


Well, I was able to use MDB2_Iterator as my iterator. After instantiating
and initializing it, I'm able to iterate through it fine using straight PHP
(similar to the above code), but I can't get it to work in Smarty. I no
longer get the error (using ->register_object() but I do get it
using ->assign()) but now it's not displaying anything; it doesn't appear
that Smarty is iterating through anything.

Is there something I'm missing?

thnx,
Chris
  Réponse avec citation
Vieux 06/10/2006, 10h24   #4
Chris Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] DB_DataObject and Smarty [Solved, I think]

> Well, I was able to use MDB2_Iterator as my iterator. After instantiating
> and initializing it, I'm able to iterate through it fine using straight
> PHP (similar to the above code), but I can't get it to work in Smarty. I
> no longer get the error (using ->register_object() but I do get it
> using ->assign()) but now it's not displaying anything; it doesn't appear
> that Smarty is iterating through anything.
> Is there something I'm missing?


Neither register_object() nor assign() were working for me so I decided to
try assign_by_ref() as sort of a last ditch attempt. Surprisingly, it
worked!
However, I'm not sure if that is the correct solution. Why did
assign_by_ref()
work and register_object(), at the very least, did not?

thnx,
Chris
  Réponse avec citation
Vieux 06/10/2006, 18h22   #5
Urb LeJeune
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Configuration

I would like to accomplish the following:

1. Have Smarty always look in path /home/e-gov/htdocs/templates for templates.
That works fine if I hard code the path in the display() function.

2. Have Smarty always look in the that

/home/e-gov/htdocs/tempates_c or
/home/e-gov/htdocs/templates/templates_

Can this be done?

Thanks

Urb
  Réponse avec citation
Vieux 06/10/2006, 18h31   #6
Max Schwanekamp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [SMARTY] Configuration

> From: Urb LeJeune
> I would like to accomplish the following:
>
> 1. Have Smarty always look in path
> /home/e-gov/htdocs/templates for templates.
> That works fine if I hard code the path in the display() function.
>
> 2. Have Smarty always look in the that
>
> /home/e-gov/htdocs/tempates_c or
> /home/e-gov/htdocs/templates/templates_
>
> Can this be done?


I find it easiest to extend the Smarty class and set that stuff in the
constructor, e.g. in php4:

require_once 'Smarty.class.php';

class Template extends Smarty
{
function Template()
{
//Call Smarty Constructor
$this->Smarty();

//Determine where the smarty files are -- slashes on both
sides
$base_path = _LOCALPATH.'/some/path/';

//Assign Smarty defaults -- trailing slash only
$this->template_dir = _LOCALPATH.'/public_html/templates/';
$this->compile_dir = $base_path.'templates_c/';
$this->config_dir = $base_path.'configs/';
$this->cache_dir = $base_path.'cache/';
$this->plugins_dir = array(SMARTY_DIR.'/plugins',
$base_path.'plugins/');

//cache settings, for when we do use caching
$this->use_sub_dirs = 1; // enable cache subdirectories
$this->cache_lifetime = 3600; // 60 minutes

//caching turned off by default
$this->caching = 0;

}
}

Maybe there's a better way, but this works well for me.

--
Max Schwanekamp
NeptuneWebworks.com

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14678 seconds with 14 queries