Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

looping through phpobjects?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
lepra
Smarty Rookie


Joined: 01 Jun 2003
Posts: 6

PostPosted: Sun Jun 01, 2003 1:50 am    Post subject: looping through phpobjects? Reply with quote

Maybe this is an easy one, but i have looked all over the docs and this forum and haven't found a similar situation, but maybe i'm dumb or blind.. however..

when i wrote it in php (without using smarty) it looked like this:
for($i=0; $i<=$somevalue; $i++)
{
$MenuOne->drawMenu($i);
}

Let's say the value of $i could be like 100, what is the solution to loop through this in smarty?

I can't use smartys register_object function cause it can't be looped like this says the documentation. Is it possible to assign the object ($MenuOne->drawMenu()) as an array? it sounds wrong in my ears.

i have tried many different solutions, i.e. this one:
{section name=i loop=$somevalue }
{$MenuOne->drawMenu($i)}
{/section}

(where $somevalue is already smarty-assigned in the php-part)
I tried just typing the object name cause it said it would work somehere in the docs, but it doesn't seem to work.

I would be very thankful for any kind of help.
I run smarty 2.5.0

----

and i also want to share a completely other tip:
if you have a php-script (which maybe not use smarty) and include another php-script in it, which is in another directory and which uses smarty, you should remember that the template must be stored in the same directory as the main php-script, NOT in the same directory as the included php-script.

it might look obvious, but i got stuck on it for some while :]

sorry for my english, i'm just a stupid blonde swede.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Jun 01, 2003 2:49 am    Post subject: Reply with quote

Hi lepra.

AFAIK, the register_object restriction doesn't apply in your case since you are not looping over the object, but merely calling a method of the object in a loop. Furthermore, using assign instead of register removes the remaining restrictions.

For example, the following works great for me:
[php:1:06f4762dcd]<?php
$smarty->assign_by_ref( 'BLOCK', new PageData );
$smarty->display( 'block.tpl' );
?>[/php:1:06f4762dcd]
Code:
{foreach from=$BLOCK->Blocks() key=k item=block}
    <div class="{$BLOCK->style_class()}" {$BLOCK->tag_options()}>
        {include file=$block->template() data=$block->Blocks()}
    </div>
{/foreach}


in this case, $BLOCK->Blocks() returns back an array of blocks to be processed. Notice that contained blocks can themselves be "blocks" and are passed to the included template with the data parameter as $block->Blocks(). This example shows how easy is it is to write confusing code because $BLOCK and $block are NOT the same ($BLOCK is the parent of $block) and both contain the method Blocks()!.

Spam spam, spam spam spam, spam. Spam.
Back to top
View user's profile Send private message
Enquest
Smarty Regular


Joined: 14 May 2003
Posts: 79

PostPosted: Sun Jun 01, 2003 7:02 am    Post subject: Reply with quote

I am not sure if I do understand you completly.

But it apears to me you want to run a function for 10 times or so.

But why don't put all the work in in your function and put the data in an array.
return the result. That way you simple have to assign it smarty.
In smarty you can do a simple {section} or whatever is your wish by getting your data out of the array! Thats seem more logical?

Or am I wrong?
_________________
-----------------------
Learning my self by trying to help others
Back to top
View user's profile Send private message
lepra
Smarty Rookie


Joined: 01 Jun 2003
Posts: 6

PostPosted: Sun Jun 01, 2003 1:37 pm    Post subject: Reply with quote

hi!

Thanks a lot for the help, it helped me partly but i'm unfortunatly still stuck. I'm a newbie at smarty and not very experienced with oop, however assign_by_ref seems to be the right solution as you said. The debug window now shows an array containing all possible values. My problem now is that i don't know how to loop through it in a correct way.

Since the number of loops may vary depending on what the parameter to the method is (the variable $i in: $MenuOne->drawMenu($i)) , i can't use a foreach-loop since it loops through the complete array, doesn't it? so i guess i'll have to use a section-loop so that i can control how many times it will loop, and that's not a problem. The problem is that i don't understand how to write to display the values of the method.

I have tried like this now:
[php:1:ef94bca4ae]
...
$smarty->assign_by_ref( 'menuthing', $MenuOne );
...
[/php:1:ef94bca4ae]
and in the template:
Code:

{section name=i loop=$a_value_that_may_vary_from_1_to_4 }
{$menuthing->drawMenu(i)}
<br>
{/section}


..but it doesn't work. I'm sure it isn't a very big problem for those who are experienced in both smarty and objects, classes and methods and such stuff, but unfortunatly i'm not very good at it.

I would be very thankful for even more help :]


boots wrote:

AFAIK, the register_object restriction doesn't apply in your case since you are not looping over the object, but merely calling a method of the object in a loop. Furthermore, using assign instead of register removes the remaining restrictions.

For example, the following works great for me:
[php:1:ef94bca4ae]<?php
$smarty->assign_by_ref( 'BLOCK', new PageData );
$smarty->display( 'block.tpl' );
?>[/php:1:ef94bca4ae]
Code:
{foreach from=$BLOCK->Blocks() key=k item=block}
    <div class="{$BLOCK->style_class()}" {$BLOCK->tag_options()}>
        {include file=$block->template() data=$block->Blocks()}
    </div>
{/foreach}



Back to top
View user's profile Send private message
lepra
Smarty Rookie


Joined: 01 Jun 2003
Posts: 6

PostPosted: Sun Jun 01, 2003 1:41 pm    Post subject: Reply with quote

Enquest wrote:
I am not sure if I do understand you completly.

But it apears to me you want to run a function for 10 times or so.

But why don't put all the work in in your function and put the data in an array.
return the result. That way you simple have to assign it smarty.
In smarty you can do a simple {section} or whatever is your wish by getting your data out of the array! Thats seem more logical?

Or am I wrong?


yeah, in fact it is only between 1 and 4 times, but it could be even more in the future.

I can't change much in the function since it will affect other pages which i don't want to change. And i still have to pass this value ($i) so that it can determine which type of array it will return.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Jun 01, 2003 2:37 pm    Post subject: Reply with quote

{$menuthing->drawMenu($smarty.section.i.index)} ??
Back to top
View user's profile Send private message
Enquest
Smarty Regular


Joined: 14 May 2003
Posts: 79

PostPosted: Sun Jun 01, 2003 2:54 pm    Post subject: Reply with quote

lepra wrote:
Enquest wrote:
I am not sure if I do understand you completly.

But it apears to me you want to run a function for 10 times or so.

But why don't put all the work in in your function and put the data in an array.
return the result. That way you simple have to assign it smarty.
In smarty you can do a simple {section} or whatever is your wish by getting your data out of the array! Thats seem more logical?

Or am I wrong?


yeah, in fact it is only between 1 and 4 times, but it could be even more in the future.

I can't change much in the function since it will affect other pages which i don't want to change. And i still have to pass this value ($i) so that it can determine which type of array it will return.


Well than you limit it on the page where you want to limit it. Try the manual its realy easy reading. look at the {section} part and you'll see how to limit a result page.
And if you still at a lost you still could write two functions!
_________________
-----------------------
Learning my self by trying to help others
Back to top
View user's profile Send private message
lepra
Smarty Rookie


Joined: 01 Jun 2003
Posts: 6

PostPosted: Sun Jun 01, 2003 3:22 pm    Post subject: Reply with quote

boots wrote:
{$menuthing->drawMenu($smarty.section.i.index)} ??


ah thanks, i recently discovered that you could write in that way. thanx a lot anyhow. i have to admit that i didn't know how to write simple loops. i also thought the solution was trickier than it was Smile

anyhow if anyones interested, the working code went like this:
php:
$smarty->assign_by_ref( 'menuthing', $MenuOne );

and the smarty code:
{section name=i loop=$s_securitycounter }
{$menuthing->drawMenu($smarty.section.i.index)}
{/section}


(where $s_securitycounter is the value that could vary and it is assigned in the php code)

thanx a lot boots!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP