Template variables/switches/loops in php

Tutorials for <a href="http://www.phpbbstyles.com/viewtopic.php?t=356">eXtreme Styles mod</a> for phpBB 2.0.

Moderator: Moderators

Template variables/switches/loops in php

Postby PostBot on Wed Nov 26, 2003 11:47 am

How to use template variables, switches and loops in php


Template variables like {VAR1} and {postrow.VAR1} cannot be used in php inside templates directly.

To use root level variable like {VAR1} you should use this:
Code: Select all
<?php
echo 'variable VAR1 is ', $this->vars['VAR1'];
?>


To use loop variables like {postrow.VAR1} you should use this:
Code: Select all
<?php
echo 'variable postrow.VAR1 is ', $postrow_item['VAR1'];
?>


To use more complex variable like {loop1.loop2.loop3.VAR1} you should use only last loop item like this:
Code: Select all
<?php
echo 'variable loop1.loop2.loop3.VAR1 is ', $loop3_item['VAR1'];
?>




About loops:

Loops in templates start with <!-- BEGIN something --> and ends with <!-- END something -->.

To use variable inside current loop use $loopname_item['VARIABLE_NAME
'] where 'loopname' is name of last loop.

To get number of items in current loop use $loopname_count, to get current item in loop use $loopname_i
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby HardySim on Mon Dec 01, 2003 2:43 pm

hey,

one question: i want to use phpBB vars like $userdata['username'] with php directly in the template

Code: Select all
overall_header.tpl:

<?php
echo $userdata['username'];
?>

but it doesn't work :( or isn't it possible to use it like that? ... $this->userdata['username'] doesn't work, too :(
HardySim
Registered User
Registered User
 
Posts: 1
Joined: Mon Dec 01, 2003 2:40 pm

Postby PostBot on Mon Dec 01, 2003 8:40 pm

You need to declare that variable as global
Code: Select all
<?php
global $userdata;
echo $userdata['username'];
?>
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

loop syntax to insert things after x thread

Postby must on Mon Mar 29, 2004 2:34 am

Hi,

I have xs installed on my forum...

Can you show me the loop syntax for inserting, for example text-based ads, after x number of threads/topics?

See example here: http://www.pokersavvy.com/forums/viewforum.php?f=2

TIA
must
Registered User
Registered User
 
Posts: 3
Joined: Mon Mar 29, 2004 2:30 am

Postby PostBot on Mon Mar 29, 2004 8:34 am

To put ad after every 10th thread put this code in viewforum_body.tpl before <!-- END topicrow -->:
Code: Select all
<?php if($topicrow_i % 10 == 9) { ?><tr><td class="row1" align="center" valign="middle"><span class="gen">here is ad code</span></td></tr><?php } ?>
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby must on Mon Mar 29, 2004 11:33 pm

Thanks!

you should make this into a mod or xs add-on. I think it will be popular.
must
Registered User
Registered User
 
Posts: 3
Joined: Mon Mar 29, 2004 2:30 am

Postby must on Mon Mar 29, 2004 11:41 pm

CyberAlien wrote:To put ad after every 10th thread put this code in viewforum_body.tpl before <!-- END topicrow -->:
Code: Select all
<?php if($topicrow_i % 10 == 9) { ?><tr><td class="row1" align="center" valign="middle"><span class="gen">here is ad code</span></td></tr><?php } ?>


Can you also show me the syntax for showing the ad only once after x post (without looping/repeating)?

TIA
must
Registered User
Registered User
 
Posts: 3
Joined: Mon Mar 29, 2004 2:30 am

Postby PostBot on Tue Mar 30, 2004 8:45 am

before <!-- END topicrow --> put this code:
Code: Select all
<?php if($topicrow_i == 5) { ?><tr><td class="row1" align="center" valign="middle"><span class="gen">here is ad code</span></td></tr><?php } ?>
This will put ad after 6th post.
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby heyyo on Tue May 04, 2004 5:32 pm

I would like to display a text if there is no item in the loop ?

I tried to test if the variable _count is equal to 0, but I don't know how to do it. Any idea ?
heyyo
Registered User
Registered User
 
Posts: 3
Joined: Fri Jan 30, 2004 3:25 pm

Postby PostBot on Tue May 04, 2004 5:38 pm

You should check $postrow_count after <!-- END postrow --> because part between BEGIN and END will not be executed with 0 items.
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby heyyo on Tue May 04, 2004 5:43 pm

Thanks I didn't try this !
heyyo
Registered User
Registered User
 
Posts: 3
Joined: Fri Jan 30, 2004 3:25 pm

Postby php_user on Sat Jun 12, 2004 9:36 pm

Hello. I have php chat installed om my phpbb forum. And in chatlink.php
there is string <?php echo $UserInChat ?>. This get value who is
chattering, names from phpbb_c_users table. And now i want
$UserInChat (chat users) show in overall_header.tpl.

Regards
Mathias
Göteborg
php_user
Registered User
Registered User
 
Posts: 2
Joined: Sat Jun 12, 2004 9:29 pm

Postby PostBot on Sat Jun 12, 2004 10:26 pm

When using variables in templates you should declare it as global because templates are included inside function. So instead of <?php echo $UserInChat; ?> you should use <?php global $UserInChat; echo $UserInChat; ?>
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby php_user on Sun Jun 13, 2004 8:32 am

Thank you Cyber Alien. It works on that page chatlink.php, included overall.header.tpl but i want same on index.php top on overall.header.tpl
and on index_body.tpl. I insert this <?php global $UserInChat; echo $UserInChat; ?> on overall_header.tpl and index_body.tpl and i get
no names from phpbb chat.


Soruce code; http://www.phpbb.com/phpBB/viewtopic.ph ... highlight=

Regards
Mathias
Göteborg
php_user
Registered User
Registered User
 
Posts: 2
Joined: Sat Jun 12, 2004 9:29 pm

Postby Gogi on Tue Jun 22, 2004 2:35 am

As correctly write condition in viewforum_body.tpl:
If (forum id) = 5, then print "bla, bla, bla " ?

Sorry for my bad English
Last edited by Gogi on Mon Oct 02, 2006 6:55 am, edited 2 times in total.
Gogi
Registered User
Registered User
 
Posts: 8
Joined: Tue Jun 22, 2004 2:32 am
Location: made in Russia

Next

Return to eXtreme Styles mod tutorials

Who is online

Users browsing this forum: No registered users and 2 guests

cron