Page 1 of 1

[SOLVED] Swap Content Layout in a Loop Using Smarty

Posted: Fri Jan 08, 2016 6:20 am
by mr.bacan
Hi I need to show the content of the News Module in an odd/even order. I've tried using {cycle} and {section} but I'm not a programmer so I'm just guessing here. Here's an example of what I'm trying to accomplish:

Image

The sample code for each layout is something like this:

Code: Select all

<!-- Layout A -->
<div class="row">
        <div class="col-lg-8 col-md-8 col-sm-8 text-center">
            <div class="image">
                Image
            </div>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 text-center">
            <div class="text">
                Text Content
            </div>
        </div>
    </div>
<!-- Layout B -->
    <div class="row">
        <div class="col-lg-4 col-md-4 col-sm-4 text-center">
            <div class="text">
                Text Content
            </div>
        </div>
        <div class="col-lg-8 col-md-8 col-sm-8 text-center">
            <div class="image">
                Image
            </div>
        </div>
    </div>
I have several news and they should be presented like this:

1. Layout A
2. Layout B
3. Layout A
4. Layout B
5. And so on...

Is this possible using smarty?
Thanks in advance for any help on this.

Re: Swap Content Layout in a Loop Using Smarty

Posted: Fri Jan 08, 2016 7:34 am
by rotezecke
try (untested):

Code: Select all

{foreach key=i item=row from=$entry}
<div class="row">
{if $i%2==1}

        <div class="col-lg-8 col-md-8 col-sm-8 text-center">
            <div class="image">
                Image
            </div>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 text-center">
            <div class="text">
                Text Content
            </div>
        </div>

{else}

        <div class="col-lg-4 col-md-4 col-sm-4 text-center">
            <div class="text">
                Text Content
            </div>
        </div>
        <div class="col-lg-8 col-md-8 col-sm-8 text-center">
            <div class="image">
                Image
            </div>
        </div>

{/if}
</div>
{/foreach}

Re: Swap Content Layout in a Loop Using Smarty

Posted: Fri Jan 08, 2016 4:02 pm
by mr.bacan
Thanks Rotezecke for your answer. Sadly this didn't worked, it is not rendering any the layouts.

Someone gave me this

Code: Select all

{foreach $myNames as $name}
	{if $name@iteration is div by 2}
		<!-- Layout B -->
	{else}
		<!__ Layout A -->
	{/if}
{/foreach}
But it didn't worked either. I assume it has something to do with the foreach, since it's not telling where to get the info from.

Re: Swap Content Layout in a Loop Using Smarty

Posted: Fri Jan 08, 2016 7:09 pm
by Jeff

Code: Select all

{if $name@iteration is div by 2}
I don't recognize "is div by" I would use:

Code: Select all

{if $name@iteration%2}
It will alternate between 0 and 1.

Re: Swap Content Layout in a Loop Using Smarty

Posted: Fri Jan 08, 2016 10:44 pm
by mr.bacan
Jeff wrote:

Code: Select all

{if $name@iteration is div by 2}
I don't recognize "is div by" I would use:

Code: Select all

{if $name@iteration%2}
It will alternate between 0 and 1.
It worked perfectly. I just made a minor adjustment since my template is using {foreach from=$items item=entry} so it's now

Code: Select all

{foreach from=$items item=entry}
	{if $entry@iteration%2}
I'm not pretty sure what I did is the right thing, but it is working as I needed.
Thanks a lot for your help.