Adsense after the first entry

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

.third won't work, although it's a good guess. Of course, if what you want is after every n entries, you can do it this way:

Code: Select all

{if $smarty.foreach.dategroup.index is div by 2}
<!-- Stuff for first, third, etc. entries here -->
{else}
<!-- Stuff for other entries here -->
{/if}
That starts at 0, so the first entry is divisible by 2 (or anything else), so it hits the first, third, fifth, and so on. If you wanted it on the second, fourth, sixth, and so on, you could just switch to .iterator, which starts at 1.
Judebert
---
Website | Wishlist | PayPal
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

And of course, if you don't like "is div by", there's a {cycle} tag that might be a little more intuitive. In this case, we'll have to be just a little tricky:

Code: Select all

{foreach blah blah blah}
    blah blah blah (the important stuff)
    {cycle values='true,false,false' print='false' assign='print_google'}
    {if $print_google == 'true'}
        <!-- Print Google, of course! -->
    {/if}
{/foreach}
This way, you can specify exactly where in each group of entries you want the code to occur. For instance, if you wanted only the first and fourth of each group of seven (don't ask me why you would want this, but if you did), you could just use values='true,false,false,true,false,false,false' and it would do exactly that.

And with this bit, you don't need to name the foreach loop.
Judebert
---
Website | Wishlist | PayPal
carl_galloway
Regular
Posts: 1331
Joined: Sun Dec 04, 2005 5:43 pm
Location: Andalucia, Spain
Contact:

Post by carl_galloway »

oh man you are on a roll with this programming bug aren't you?

I've never thought of being able to extend {cycle} and what you suggest just seems like mana from heaven, imagine what you could do with styling entries or sdebar blocks with code like that. Any chance you could break it down a little more so we follow the full logic, I think anyone else reading this thread might really appreciate it.
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Well, sure, I can give it a try.

The normal {cycle} tag allows you to cycle through a bunch of printed values. You just need to specify the values, which can be anything at all. For instance, if you wanted to print "even" or "odd" for each entry, you could easily do it like this:

Code: Select all

{foreach from='$entries' item='dategroup'}
    <div class="even_odd">{cycle values='odd,even'}</div>
    <!-- Other entries stuff -->
{/foreach}
Notice that {cycle} starts over at the beginning when it runs out of values, so if we printed five entries, they'd say odd, even, odd, even, odd.

If you had a music blog, you might want to print a whole note, half note, quarter note, and eighth note for each entry in order. No problem:

Code: Select all

{foreach from='$entries' item='dategroup'}
    <div class="serendipity_entries">
        <img src="{cycle values='wholenote,halfnote,quarternote,eigthnote'}.gif" />
        <!-- other entry stuff -->
    </div>
{/foreach}
Note that {cycle} just prints out the next entry in the cycle. So if those weren't all .gif files, you could actually specify them with values='wholenote.gif,halfnote.jpg,quarternote.png,eigthnote.mng' or whatever else you had.

{cycle} is most often used in CSS styles. If you wanted to alternate colors, you could use this:

Code: Select all

{foreach from='$entries' item='dategroup'}
    <div style="background: {cycle values='white,black'};">
       <!-- other entry stuff -->
    </div>
{/foreach}
The problem with that little loop is that we need some really interesting text color to show up on both white and black. Another cycle would do the job, right? Maybe; since we didn't name it, it's automatically named "default". That gives us this:

Code: Select all

{foreach from='$entries' item='dategroup'}
    <div style="background: {cycle name='bgcolor' values='white,black'}; color: {cycle name='textcolor' values='black,white'};">
        <!-- Other entry stuff -->
    </div>
{/foreach}
Of course, you don't normally want 'style=' attributes in your template. You'd rather say class="{cycle values='class1,class2'}" instead.

{cycle} has lots of other options, too. For instance, you may find yourself respecifying the same color in multiple locations. In that case, you could just tell the cycle not to advance by adding advance=false. (Unfortunately, you must specify the same values every time.)

In my case, I wanted to store the value of the cycle in a variable, and not print it out. So I used assign='name', which assigns the value of the cycle to a variable, and print=false, which tells Smarty not to print the value of the cycle. Once it was assigned to a variable, I could check it with {if}.

Pretty simple when you look at it, eh?
Judebert
---
Website | Wishlist | PayPal
Fila
Regular
Posts: 34
Joined: Thu Oct 26, 2006 11:16 am

Post by Fila »

Thanks everybody for help. I appreciate it. I choose to use judebert's solution

Anyway when i use

Code: Select all

{if $smarty.foreach.dategroup.index is div by 2 }
it didnt work

I have to add

Code: Select all

{if $smarty.foreach.dategroup.index is div by 2 AND $smarty.foreach.outerdategroup.index is div by 2}
so will it be alright?

Thanks again :)
Fila
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Nothing wrong with the syntax. If it does what you want, use it!

If it does something else, let us know what's wrong and we'll try to fix it.
Judebert
---
Website | Wishlist | PayPal
Fila
Regular
Posts: 34
Joined: Thu Oct 26, 2006 11:16 am

Post by Fila »

Thanks again Jubert, Carl and Garvin.. appriciate your help :)
Fila
Regular
Posts: 34
Joined: Thu Oct 26, 2006 11:16 am

Post by Fila »

garvinhicking wrote:Hi!

Now that carl mentions it: Did you have a look at the serendipity 'serendipity_event_includeentry' plugin ("Include entry blocks" or something like that). This actually allows you to post/use specific "blocks" after X entries. :)

Best regards,
Garvin
I tried that plugin but it didn't work on my blog..
Post Reply