|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> 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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
> 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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
> 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 |
|
![]() |
| Outils de la discussion | |
|
|