How to increment a variable in a template?

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

How to increment a variable in a template?

Post by Jobertim »

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: 752
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: How to increment a variable in a template?

Post by andreask »

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
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
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: How to increment a variable in a template?

Post by mrgoldy »

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?
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
Jobertim
Registered User
Posts: 280
Joined: Wed Dec 28, 2011 2:25 pm

Re: How to increment a variable in a template?

Post by Jobertim »

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”