How to use php in templates

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

Moderator: Moderators

How to use php in templates

Postby PostBot on Wed Nov 26, 2003 10:51 am

How to use PHP in templates.

You can use php in any tpl file except for bbcode.tpl (bbcode.tpl is parsed differently by phpBB so you can't use php in it).

PHP code should start with <?php or <!-- PHP --> and must end with ?> or <!-- ENDPHP -->. Short php tags (<? and <?=) are not allowed.

Template is included inside function, so if you want to use global variables you must declare it as global before using it.
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

php not working in template

Postby Moonshadow on Sun Feb 15, 2004 7:38 pm

file name is portal_body.tpl
Code: Select all
<td class="row1" align="center">
<?php

system ("http://www.mysite.com/cgi-bin/script.cgi");


?>
</td>
   </tr>
  </table>

there is nothing......
I'm VERY new to this and have been looking for hours for a way to do this. The script fetchs data from an offsite page and displays it. I just want that data to appear on my portal. I know the scripts works, I can run it directly or even with SSI from a .shtml page. I installed Exstyle mod, so I could use the system call to run the script.... but no luck :(

Any idea what I did wrong?
Thanks!
Moonshadow
Registered User
Registered User
 
Posts: 2
Joined: Sun Feb 15, 2004 6:38 pm

Postby PostBot on Sun Feb 15, 2004 7:58 pm

system() executes external program. you cannot use urls in it
read this document: http://www.php.net/system
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 Moonshadow on Sun Feb 15, 2004 9:20 pm

CyberAlien wrote:system() executes external program. you cannot use urls in it
read this document: http://www.php.net/system


Thanks....
That helped... by using the "include" instead of "system" it worked just fine. :)

Keep up the great work! Thanks again for the fast responce.
Moonshadow
Registered User
Registered User
 
Posts: 2
Joined: Sun Feb 15, 2004 6:38 pm

Postby PostBot on Mon Feb 16, 2004 6:34 pm

Use include() only for local files. For remote files use readfile()
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 dryadsong on Thu Nov 25, 2004 6:01 am

I've been trying to get a short code working, and it's in my overall_footer file. It's supposed to check when I last uploaded a cam picture and give that date and time. So far, it's not working at all on my forum page (it works fine everywhere else on my site, but not the forum):

Code: Select all
<?php
$filename = 'http://www.dryadsong.com/images/webcam.jpg';
if (file_exists($filename))
{
  $time=filemtime($filename);
  echo("Updated " . date ("m/d H:i", $time));
}
?>


what is the problem with it?
dryadsong
Registered User
Registered User
 
Posts: 6
Joined: Thu Nov 25, 2004 5:58 am

Postby PostBot on Thu Nov 25, 2004 10:11 am

Functions filemtime and file_exists don't work on remote files. Use local filename.
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 dryadsong on Thu Nov 25, 2004 3:05 pm

I'm a little confused by what you mean -- my forum is at www.dryadsong.com/forum so wouldn't that make anything at www.dryadsong.com a local file?

What makes it remote?
dryadsong
Registered User
Registered User
 
Posts: 6
Joined: Thu Nov 25, 2004 5:58 am

Postby dryadsong on Thu Nov 25, 2004 3:09 pm

Ah, wait...did you mean I should make the code like this instead:

Code: Select all
<?php 
$filename = '../images/webcam.jpg'; 
if (file_exists($filename)) 

$time=filemtime($filename); 
echo("Updated " . date ("m/d H:i", $time)); 

?> 
dryadsong
Registered User
Registered User
 
Posts: 6
Joined: Thu Nov 25, 2004 5:58 am

Postby PostBot on Thu Nov 25, 2004 3:10 pm

yes. something like that.
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 jer@mia.net on Wed Dec 15, 2004 8:41 am

I have inserted php code in the following files:

viewtopic_body.tpl
viewtopic_forums.tpl
index_body.tpl

Nothing freaks in the format of the forum, but nothing shows up either. The forum is fine, but the PHP includes do not show the content that is supposed to be delivered.

Any ideas? Yes, I am using <?php and ?>

Thanx..
jer@mia.net
Registered User
Registered User
 
Posts: 1
Joined: Wed Dec 15, 2004 8:38 am
Location: Midwest

Postby UseLess on Wed Dec 15, 2004 11:13 am

Greetings,

Do you have the XS Mod installed?

As without it you cannot use PHP in templates on a standard phpBB installation.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby Hater on Wed Feb 09, 2005 9:33 am

Let me just say that I keep finding this MOD to be more and more helpful almost every day.

I am finally starting to take advantage of what it offers, and although I still firmly believe that using <?php tags within the template breaks the entire intent of the template engine, it sure is helping me out right now, and I wanted to write a sincere thank you.

Also, that bit about globalizing variables was very helpful and had me scratching my head for a second. I didn't quite understand it at first until I went snooping around your template.php file to see how the $template class is handled. Again, fantastic work, and it's great that you've backported so much of that phpBB 3 has to offer. :)
Hater
Registered User
Registered User
 
Posts: 4
Joined: Wed Feb 09, 2005 9:30 am

Postby Friends4U on Tue Mar 01, 2005 7:21 pm

Hi,

If i do {TEMPLATE} in a tpl file i get blabla/home/blabla/Forums/templates/subSilver/ but what i want is only the name subSilver when ik do that... how must i do that?

Sorry for my bad english....
Friends4U
Registered User
Registered User
 
Posts: 4
Joined: Tue Mar 01, 2005 3:52 pm
Location: Dordrecht, Nederland

Postby PostBot on Tue Mar 01, 2005 7:29 pm

If you want only template name then open includes/page_header.php, find this:
Code: Select all
   'T_HEAD_STYLESHEET' => $theme['head_stylesheet'],
and add before it:
Code: Select all
   'T_TEMPLATE_NAME' => $theme['template_name'],
and then use {T_TEMPLATE_NAME} in tpl files.
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

Next

Return to eXtreme Styles mod tutorials

Who is online

Users browsing this forum: No registered users and 2 guests

cron