How to increment a variable in a template?

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
Jobertim
Registered User
Posts: 235
Joined: Wed Dec 28, 2011 2:25 pm

How to increment a variable in a template?

Post by Jobertim » Tue Dec 26, 2017 2:31 pm

I would like to be able to add a banner ad in between posts in a thread, for example every 3 posts, or every 5 posts etc.

So far the closest I've found is an extension that allows me to place a banner ad after the 1st post of a thread. The code is:

Code: Select all

<!-- IF not S_USER_LOGGED_IN and not S_IS_BOT -->
	<!-- IF GOOGLE_ADSENSE_DISPLAY -->
		<!-- IF ! $GOOGLE_ADSENSE_SHOWN -->
		<div class="post bg2 adsense" style="position: relative;">		
			<div class="inner">		
				<div class="postbody">						
					<div> 			 			
						<div class="content">
							{GOOGLE_ADSENSE_HTML}
						</div>							
					</div>		
				</div>		
			</div>		
		</div>
		<!-- DEFINE $GOOGLE_ADSENSE_SHOWN = true -->
		<!-- ENDIF -->
	<!-- ENDIF -->
<!-- ENDIF -->
I would like to create a counter, $POST_NUMBER so that I can test to know what post number is being displayed and determine wether or not to display an ad. Something like this except "<!-- $POST_NUMBER++ -->" does not increment my variable?

Code: Select all

<!-- IF not S_USER_LOGGED_IN and not S_IS_BOT -->
	<!-- IF GOOGLE_ADSENSE_DISPLAY -->
		<!-- IF not S_USER_LOGGED_IN and not S_IS_BOT -->
			<!-- IF GOOGLE_ADSENSE_DISPLAY -->
				<!-- IF ! $POST_NUMBER -->
					<!-- DEFINE $POST_NUMBER = 0 -->
				<!-- ENDIF -->
				<!-- $POST_NUMBER++ -->
				<!-- IF $POST_NUMBER == 4 -->
				<div class="post bg2 adsense" style="position: relative;">		
					<div class="inner">		
						<div class="postbody">						
							<div> 			 			
								<div class="content">
									{GOOGLE_ADSENSE_HTML}
								</div>							
							</div>		
						</div>		
					</div>		
				</div>
				<!-- ENDIF -->
			<!-- ENDIF -->
		<!-- ENDIF -->
	<!-- ENDIF -->
<!-- ENDIF -->

andreask
Registered User
Posts: 428
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: How to increment a variable in a template?

Post by andreask » Wed Jan 03, 2018 4:07 pm

Perhaps something like this?
https://stackoverflow.com/questions/249 ... e-in-twig-
Using a for loop with twig syntax of course...

Code: Select all

{% set myVal = 50 %}

{% for item in items %}
     {% set myVal = myVal + 1 %}
{% endfor %}
But I'm guessing that with the simple {% set myVal = myVal + 1 %} you are good also...
Here is what I am working on right now...
Inactive User Manager for phpBB, Prime post revision 3.1/3.2
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!

User avatar
posey
Registered User
Posts: 639
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: How to increment a variable in a template?

Post by posey » Thu Jan 04, 2018 1:13 pm

If you're editing in the viewtopic page, you can use the postrow loop, with the S_ROW_COUNT variable.
Possible options:

Code: Select all

{% if postrow.S_ROW_COUNT is even %} .. {% endif %}
{% if postrow.S_ROW_COUNT is odd %} .. {% endif %}
{% if postrow.S_ROW_COUNT is 6%} .. {% endif %}
{% if postrow.S_FIRST_ROW %} .. {% endif %}
{% if postrow.S_LAST_ROW %} .. {% endif %}
Is that what you're looking for?
''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

Jobertim
Registered User
Posts: 235
Joined: Wed Dec 28, 2011 2:25 pm

Re: How to increment a variable in a template?

Post by Jobertim » Tue Jan 09, 2018 8:59 am

Thanks a lot for your help. I ended up using S_ROW_COUNT, like this:

Code: Select all

		<!-- IF postrow.S_ROW_COUNT == 3 -->

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: picaron and 3 guests