Re: [SMARTY] looping over an array of objects

From: Date: Thu, 03 Nov 2005 13:09:27 +0000
Subject: Re: [SMARTY] looping over an array of objects
References: 1 2 3  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-13145@lists.php.net to get a copy of this message
http://phocoa.com does combines them. "binarycloud" mentions Smarty and Propel on the website but I am not if/how they combine them. Form generation is an interesting feature -- especially for prototyping. It's my experience that in "real" project where a designer is involved, form generation is not of much use. Actually, I am thinking about a way to "extend" a generated HTML page; backed up by a algorithm that identifies new data-columns the user has to fill in but are not provided in the HTML page yet. This could be interesting for automated front-end testing, too. But at the moment it's just an unadjusted idea of mine :-) I will outline how I use Propel/Smarty (assuming that more experienced programmers would do it in another way): At the moment I am using the Propel-validator mechanism to validate data entered by the user, since from my point of view the model-objects should in-/validate themselves. To accomplish this I added my own BaseClass with a "clever" isValid()-method to validate an model-object. a) In the Controller all properties/columns of the model can be checked at once like this: if($m_model->isValid(NULL) == FALSE) { // There is one or more invalidate properties/columns // in the model. $smarty->assign("model", $m_model); } else { // Everything is OK. $m_model->save(); } b) In the template each colum can be checked on its own: {if $model->isValid('username') == FALSE} // Display some big, red, blinking icon nect to the username-textfield. // But: model->isValid('username') will return TRUE iff // the formular is a "fresh" one and the model hast no been // check beforehand. So, this works for newly created models (INSERT) // as for models retrieved from the database (UPDATE). {/if} c) In the error-template all error-keys of the Propel-validators are looked up in a smarty-configuration file: {foreach from=$ERROR_KEY_ARRAY item=ERROR_KEY} // The ERROR_KEY may be 'username_invalid_chars' and will // be translated to 'The username you entred contains invalid chars.' {$smarty.config.$ERROR_KEY}<br/> {/foreach} - Erik Am Mittwoch, 2. November 2005 18:44 schrieb Isaac Vetter: > Tom, Erik, boots; > > Thank you for your help. I changed two things to get it working. > > 1) I upgraded smarty to 2.6.10, but I don't know if this was needed or not. > 2) I was making a stupid syntax error by not including the parentheses > on property calls from an object passed in with assign(). > > Erik, I'd love to hear about your experiences with propel and smarty, > have you seen any projects that attempt to link them (ie. form generation?). > > Thanks again, > > Isaac > > Erik Schmitt wrote: > > >Hello, > > > >if you want to iterate over an array holding propel objects: it works. > > > >I read your question on the propel list and I am not sure if I understood > >it correctly. Is it your intention to "convert" values of propel objects > >into an multidimensional-array? > > > >Anyway, this works: > > > >{foreach from=$USER_ARRAY item=USER} > > {$USER->getName}<br/> > >{/foreach} > > > >(And this will not work: {$USER->getCompany()->getName()} ) > > > >- Erik > > > > > >Am Dienstag, 1. November 2005 14:40 schrieb Isaac Vetter: > > > > > >>I'm setting up propel (http://propel.phpdb.org/), which is an > >>object-relational mapper, like DB_Object. So, database queries return > >>objects, many of which need to be passed to a smarty template for > >>display. I could manually move each relevant property into an array > >>($ary[$i]['id'] = $aryOfObj[$i]->getId()), but ... I really don't > >>want > >>to have to do that. > >> > >>The smarty documentation implies that an array of objects can be looped > >>over, if the array is assign()'d or assign_by_ref()'d. This doesn't > >>seem to be working for me. Can someone confirm that this works? > >> > >>If this doesn't work, I'm thinking of writing/using code that uses > >>reflection to automatically call property getters to create the > >>smarty-required multi-dimensional arrays, any suggestions? > >> > >>Much Thanks, > >> > >>Isaac Vetter > >> > >> > >>----------------------------------------- > >>Smarty allows access to PHP objects through the templates. There are two > >>ways to access them. One way is to register objects > > >>>><http://smarty.php.net/manual/en/api.register.object.phpÎn(@:<�¨�¯ ˜? > >>D;> to the > >>template, then use access them via syntax similar to custom functions. > >>The other way is to assign objects > >><http://smarty.php.net/manual/en/api.assign.php> > >>to the templates and > >>access them much like any other assigned variable. The first method has > >>a much nicer template syntax. It is also more secure, as a registered > >>object can be restricted to certain methods or properties. However, *a > >>registered object cannot be looped over or assigned in arrays of > >>objects*, etc. The method you choose will be determined by your needs, > >>but use the first method whenever possible to keep template syntax to a > >>minimum. > >> > > >>>>http://smarty.php.net/manual/en/advanced.features.php#advanced.features.objects > >> > >> > >> > >> > >> > >> > >> > > > > > > > > > -- Mit freundlichen Grüßen Erik Schmitt e-novum Software GmbH D-49078 Osnabrück, Heger-Tor-Wall 19, Fon.: +49 (0)541 440 630 - 25, Fax: - 26 E-Mail: mailto:e.schmitt@e-novum.com --------------------------------------------------------- e-novum ® Software ist eine gute Wahl für Ihr E-Business. Finden Sie heraus warum. www.redpart.de / www.e-novum.com

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