syntax for loops

Discussion forum for MOD Writers regarding MOD Development.
Locked
WorldWar
Registered User
Posts: 47
Joined: Sun Mar 08, 2009 3:30 pm

syntax for loops

Post by WorldWar » Mon Nov 09, 2009 10:35 pm

Hello all,

I am passing an integer as one of my template vars and i'd like to make a loop in the style page of the kind:

<!-- loop MY_INT --> .. <!-- endloop -->

which will basically loop MY_INT times..

Thanks for the support.
:mrgreen:

WorldWar
Registered User
Posts: 47
Joined: Sun Mar 08, 2009 3:30 pm

Re: syntax for loops

Post by WorldWar » Tue Feb 09, 2010 10:44 pm

Anyone please?

Thanks

FeyFre
Registered User
Posts: 57
Joined: Wed Dec 17, 2008 5:18 pm
Location: Vinnitsa, Ukraine, Eastern Europe
Contact:

Re: syntax for loops

Post by FeyFre » Tue Feb 09, 2010 11:49 pm

I don't know direct syntax for that, but yon can use this workaround:

Code: Select all

for($i=0;$i<$n;$i++)
  $template->assign_block_vars('my_dummy_loop',array()); 
And in template:

Code: Select all

<!-- BEGIN my_dummy_loop --> loop-ed content <!-- END my_dummy_loop -->
Everybody can do the same things, but nobody can do it identically.

User avatar
Sam
Former Team Member
Posts: 2082
Joined: Thu Jan 22, 2009 3:59 am
Location: Sacramento, CA
Name: Sam Thompson
Contact:

Re: syntax for loops

Post by Sam » Wed Feb 10, 2010 12:42 am

PHP:
Assuming $result is a result from a SELECT query from a table that has 'name' and 'age' columns

Code: Select all

while($db->sql_fetchrow($reuslt))
{
    $template->assign_blog_vars('loopname', array(
        'NAME'    => $row['name'],
        'AGE'    => $row['age'],
    ));
} 
HTML Template

Code: Select all

<!-- BEGIN loopname -->
{loopname.NAME} is {loopname.AGE} years old. <br />
<!-- BEGINELSE -->
No records returned. 
<!-- END loopname -->
If there are records, it will print something like:

Code: Select all

John is 25 years old. <br />
Dan is 19 years old. <br />
Jane is 21 years old. <br />
Kate is 27 years old. <br />
If there are no results, then it will print "No records returned. "

You can even exclude the BEGINELSE part from the template, so it looks like this:

Code: Select all

<!-- BEGIN loopname -->
{loopname.NAME} is {loopname.AGE} years old. <br />
<!-- END loopname -->
It works the same, but it wont tell you if there are no records, nothing will simply appear.

Given that, the same can be applied in a "for" loop, such as what FeyFre described.
PHP

Code: Select all

$n = 4;
for($i = 0; $i < $n; $I++)
{
    $template->assign_blog_vars('loopname', array(
        'NUMBER'    => $i + 1;
    ));
} 
HTML

Code: Select all

<!-- BEGIN loopname -->
{loopname.NUMBER}<br />
<!-- END loopname -->
will output something like:

Code: Select all

1<br />
2<br />
3<br />
4<br />
Further Reading: Tutorial.Template_syntax

WorldWar
Registered User
Posts: 47
Joined: Sun Mar 08, 2009 3:30 pm

Re: syntax for loops

Post by WorldWar » Wed Feb 10, 2010 6:05 pm

Thank you very much both SyntaxError90 and FeyFre.

So, are we saying that the only way to "loop" is to prepare an array in the script file and then to loop over it in the template file? No way what-so-ever to simply pass an integer in my template vars and loop with in in the template?

I can of course prepare my array in the script but I wanted to simply do it in the template file i.e.:

Code: Select all

$template->assign_vars(array(
		'NUMBER' => 3
));
and have a template like this:

Code: Select all

<!-- LOOP NUMBER -->
hello<br>
<!-- ENDLOOP -->
so to have in the end such an output?

Code: Select all

hello<br>hello<br>hello<br>
Again I know how to create such a string or array in the script and then display it in the template but I was wondering if there is a way to do it in my way.

Thank to all again,
Best Regards

WorldWar
Registered User
Posts: 47
Joined: Sun Mar 08, 2009 3:30 pm

Re: syntax for loops

Post by WorldWar » Wed Feb 10, 2010 7:55 pm

OK, I see there is no way to do it (maybe some way hacking your way into the PHP tag..but that is dependent on some variable about allowing php code into the template, I think).

So... since I think it is useful.. I did it myself and here is for everyone a very-beta version with absolutely no error checking..

you have to modify the file /includes/functions_template.php

Insert the following function:

Code: Select all

	function compile_tag_loop($tag_args) {
		preg_match_all('/(?:
			"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"         |
			\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'     |
			[(),]                                  |
			[^\s(),]+)/x', $tag_args, $match);
		return 'for ($loopi = 0; $loopi < ((int)($this->_rootref[\'' . $match[0][0] . '\'])); $loopi++) {';
	}
and also find this:

Code: Select all

		for ($curr_tb = 0, $tb_size = sizeof($blocks); $curr_tb < $tb_size; $curr_tb++)
		{
			$block_val = &$blocks[$curr_tb];

			switch ($block_val[1])
			{
and immidately after add:

Code: Select all

				case 'LOOP':
					$compile_blocks[] = '<?php ' . $this->compile_tag_loop($block_val[2]) . ' ?>'; 					
				break;
				
				case 'ENDLOOP':
					$compile_blocks[] = '<?php } ?>';
				break;
How does it work? Here is a sample template file:

Code: Select all

<!-- LOOP NUM_OF_ITERATIONS -->
   <b>repeat this line num_of_iterations times</b><br>
<!-- ENDLOOP -->
I hope this is of help for someone! :D

Best Regards

Locked

Return to “[3.0.x] MOD Writers Discussion”