Page 1 of 1

Reapeating a block multiple times using smarty

Posted: Fri Nov 28, 2014 1:20 pm
by acore
I wanto to repeat a custom set of blocks multiple times, and i would like to write it once and repeat it with a foor loop for example.

There is an example of what I want to do. Supose in my template there is place for data 6 teammates and I want their name and phone number. What i would like to do is something like:

Code: Select all

{for $id=1 to 6}
  {content block="teammate`$id`_name" label="Name of teammate `$id`" oneline="true"} 
  {content block="teammate`$id`_phone" label="Teammate  `$id` phone number" oneline="true"} 
{/for}
Is it possible by any way? or I have to repeat the block six times changing only its number.

Re: Reapeating a block multiple times using smarty

Posted: Fri Nov 28, 2014 2:56 pm
by velden
It can't be done that way.

You could however use a module to accomplish what you like.

That module could be the News module which is installed by default. My personal choice would be ListItExtended (aka LI2 aka ListIt2)

Re: Reapeating a block multiple times using smarty

Posted: Fri Nov 28, 2014 3:13 pm
by acore
But what I want to do is to repeat blocks for the backend, not in the frontend page.

Re: Reapeating a block multiple times using smarty

Posted: Fri Nov 28, 2014 6:17 pm
by velden
acore wrote:But what I want to do is to repeat blocks for the backend, not in the frontend page.
That's not very likely I guess. But even if you´d really want that you can´t do it the way you proposed.

Maybe you should explain what exactly you want to accomplish (not technically, but a use case). Then I tell you to use News Module/ListItExtended module :-)

Re: Reapeating a block multiple times using smarty

Posted: Sun Nov 30, 2014 12:18 am
by paulbaker
Your way (even if it could be done) means going in to the template each time a team member leaves or joins. Not very easy to use.

Using ListIt2 means adding a new team member is a matter of adding a new item in the admin area. When people leave it's even easier, just untick the green status tick box (or delete them completely).

See the screen shot which shows a team within a school from one of my sites, only 4 people remain from the 9 that were entered - see the tick boxes?

Don't be scared of ListIt2, it's dead easy once you get into it. Certainly a lot easier than the Smarty you're trying to do in your template.

Re: Reapeating a block multiple times using smarty

Posted: Mon Dec 01, 2014 9:25 am
by acore
OK. I see what do you say, but I have explained poorly (forget the teammates and their phone numbers). Lets try to do it better:

In short, the template I'm writing have six blocks with the same field structure so what I want to do is define the structure once and tell the template to repeat the structure six times. I think it is important because the structure may change in the future, but there allways be the same for the six blocks so I don't want to rewrite six blocks each time there is a new field.

This is the code I want to change:

Code: Select all

<div class="outerdiv">
  <div id="f0" class="foto">
    <img class="smallFoto" src="{content_image urlonly='1' block='f0_smallimage' dir='fdir' label='Small picture 0}"/>
    <p>{content block='f0_title' label='Title 0' oneline='true'}</p>
    <p>{content block='f0_subtitle' label='Subtitle 0' oneline='true'}</p>
  </div>
  <div id="f1" class="foto">
    <img class="smallFoto" src="{content_image urlonly='1' block='f1_smallimage' dir='fdir' label='Small picture 1}"/>
    <p>{content block='f1_title' label='Title 1' oneline='true'}</p>
    <p>{content block='f1_subtitle' label='Subtitle 1' oneline='true'}</p>
  </div>
  <!--
      ...
  -->
  <div id="f6" class="foto">
    <img class="smallFoto" src="{content_image urlonly='1' block='f6_smallimage' dir='fdir' label='Small picture 6}"/>
    <p>{content block='f6_title' label='Title 6' oneline='true'}</p>
    <p>{content block='f6_subtitle' label='Subtitle 6' oneline='true'}</p>
  </div>

</div>

Re: Reapeating a block multiple times using smarty

Posted: Mon Dec 01, 2014 9:51 am
by velden
Use News module or ListItExtended (Listit2, LI2) or ...

It can NOT be done your way!

Re: Reapeating a block multiple times using smarty

Posted: Mon Dec 01, 2014 10:23 am
by paulbaker
acore wrote:so what I want to do is define the structure once and tell the template to repeat the structure six times. I think it is important because the structure may change in the future
You've just described ListIt2 pretty accurately.