Home > Archive > PHP Smarty Templates > November 2005 > Re: [SMARTY] looping over an array of objects
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: [SMARTY] looping over an array of objects
|
|
| Erik Schmitt 2005-11-01, 7:01 pm |
| 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=20
into an multidimensional-array?
Anyway, this works:
{foreach from=3D$USER_ARRAY item=3DUSER}
{$USER->getName}<br/>
{/foreach}
(And this will not work: {$USER->getCompany()->getName()} )
=2D Erik
Am Dienstag, 1. November 2005 14:40 schrieb Isaac Vetter:
> I'm setting up propel (http://propel.phpdb.org/), which is an=20
> object-relational mapper, like DB_Object. So, database queries return=20
> objects, many of which need to be passed to a smarty template for=20
> display. I could manually move each relevant property into an array=20
> ($ary[$i]['id'] =3D $aryOfObj[$i]->getId()), but ... I really don't want=
=20
> to have to do that.
>=20
> The smarty documentation implies that an array of objects can be looped=20
> over, if the array is assign()'d or assign_by_ref()'d. This doesn't=20
> seem to be working for me. Can someone confirm that this works?
>=20
> If this doesn't work, I'm thinking of writing/using code that uses=20
> reflection to automatically call property getters to create the=20
> smarty-required multi-dimensional arrays, any suggestions?
>=20
> Much Thanks,
>=20
> Isaac Vetter
>=20
>=20
> -----------------------------------------
> Smarty allows access to PHP objects through the templates. There are two=
=20
> ways to access them. One way is to register objects=20
> <http://smarty.php.net/manual/en/api.register.object.php> to the=20
> template, then use access them via syntax similar to custom functions.=20
> The other way is to assign objects=20
> <http://smarty.php.net/manual/en/api.assign.php> to the templates and=20
> access them much like any other assigned variable. The first method has=20
> a much nicer template syntax. It is also more secure, as a registered=20
> object can be restricted to certain methods or properties. However, *a=20
> registered object cannot be looped over or assigned in arrays of=20
> objects*, etc. The method you choose will be determined by your needs,=20
> but use the first method whenever possible to keep template syntax to a=20
> minimum.
>=20
> http://smarty.php.net/manual/en/adv...ced.features.o=
bjects
>=20
>=20
>=20
>=20
>=20
=2D-=20
Mit freundlichen Gr=C3=BC=C3=9Fen
Erik Schmitt
e-novum Software GmbH
D-49078 Osnabr=C3=BCck, Heger-Tor-Wall 19,
=46on.: +49 (0)541 440 630 - 25, Fax: - 26
E-Mail: mailto:e.schmitt@e-novum.com
=2D--------------------------------------------------------
e-novum =C2=AE Software ist eine gute Wahl f=C3=BCr Ihr E-Business.
=46inden Sie heraus warum. www.redpart.de / www.e-novum.com
| |
| Jochem Maas 2005-11-02, 7:00 pm |
| 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()} )
can any of the smarty devs indicate whether is any intention at all
of supporting this 'dereferenced' syntax? (possibly in whatever smarty3
becomes).
I for one would very much welcome it... but I also understand that
there are plenty of people, including the smarty devs that don't see
this as:
a, a priority
b, desirable in a templating system primariliy deisgned as a tool to
enable ['less' technical] graphic designers to better to their work.
rgds,
jochem
>
> - Erik
>
>
> Am Dienstag, 1. November 2005 14:40 schrieb Isaac Vetter:
>
>
>
| |
| Isaac Vetter 2005-11-02, 7:00 pm |
| 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:
>
>
>
>
>
| |
| Erik Schmitt 2005-11-03, 7:58 am |
| http://phocoa.com does combines them.
"binarycloud" mentions Smarty and Propel on the website=20
but I am not if/how they combine them.
=46orm 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=20
HTML page; backed up by a algorithm that=20
identifies new data-columns the user has to fill in but are not
provided in the HTML page yet. This could be interesting for=20
automated front-end testing, too. But at the moment it's just=20
an unadjusted idea of mine :-)
I will outline how I use Propel/Smarty (assuming that more=20
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.=20
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=20
checked at once like this:
if($m_model->isValid(NULL) =3D=3D FALSE)
{
// There is one or more invalidate properties/columns
// in the model.
$smarty->assign("model", $m_model);
}
else
{
// Everything is OK.
$m_model->save();=20
}
b) In the template each colum can be checked on its own:
{if $model->isValid('username') =3D=3D 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=20
// 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=3D$ERROR_KEY_ARRAY item=3DERROR_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}
=2D Erik
Am Mittwoch, 2. November 2005 18:44 schrieb Isaac Vetter:
> Tom, Erik, boots;
>=20
> Thank you for your help. I changed two things to get it working.
>=20
> 1) I upgraded smarty to 2.6.10, but I don't know if this was needed or no=
t.
> 2) I was making a stupid syntax error by not including the parentheses=20
> on property calls from an object passed in with assign().
>=20
> Erik, I'd love to hear about your experiences with propel and smarty,=20
> have you seen any projects that attempt to link them (ie. form generation=
?).
>=20
> Thanks again,
>=20
> Isaac
>=20
> Erik Schmitt wrote:
>=20
=20[color=darkred]
=20[color=darkred]
t=20[color=darkred]
=20[color=darkred]
o=20[color=darkred]
=20[color=darkred]
=20[color=darkred]
=20[color=darkred]
=20[color=darkred]
=20[color=darkred]
=2Eobjects[color=darkred]
>=20
>=20
>=20
=2D-=20
Mit freundlichen Gr=C3=BC=C3=9Fen
Erik Schmitt
e-novum Software GmbH
D-49078 Osnabr=C3=BCck, Heger-Tor-Wall 19,
=46on.: +49 (0)541 440 630 - 25, Fax: - 26
E-Mail: mailto:e.schmitt@e-novum.com
=2D--------------------------------------------------------
e-novum =C2=AE Software ist eine gute Wahl f=C3=BCr Ihr E-Business.
=46inden Sie heraus warum. www.redpart.de / www.e-novum.com
|
|
|
|
|