PHP in Template Files

Register an Account
 
 
Article Tools Display Modes
  #1 (permalink)  
Old 08-11-2009, 12:55 AM
Yankidank's Avatar
Pligg Founder/Coder/Designer
Pligg Version: SVN
Pligg Template: Wistie
 
Join Date: Dec 2005
Location: Ocala, FL
Posts: 4,934
Send a message via AIM to Yankidank
This article discusses how to use PHP within Pligg template files.
If you are logged in to the Pligg forums you can edit the article below, just click on the edit button at the bottom of the post.

The Twitter Module for Pligg CMS!
Register, Login, and Submit Stories with Twitter. An absolute MUST HAVE for all Pligg sites!

Last edited by Yankidank; 08-11-2009 at 01:15 AM.
Reply With Quote
  #2  

PHP in Template Files

Pligg Templates use Template Lite, a variation of the Smarty template engine. Because of this you are unable to simply write PHP code in the .tpl files and expect it to render as PHP would. But using a {php} tag within your template files will allow you to use PHP in your templates. See below for examples of how to use PHP in a Pligg template file.

Code:
 {php}
// PHP examples for printing the current date and time in various formats
//07/23/09
print date("m/d/y") . "";

//Thu, July 23rd
print date("D, F jS") . "";

//Thursday, July 23rd 2009
print date("l, F jS Y") . "";

//10:10 AM
print date("g:i A") . "";

//Thu, 23 Jul 2009 10:10:53 -0400
print date("r") . "";

//10:10:53 AM Thu, July 23rd 2009
print date("g:i:s A D, F jS Y") . ""; 
{/php}
Using an example from above you could easily insert the current time to the header.tpl file. Below is an example that places the PHP after some Smarty variables that display the logged in user's name if the user is logged in.
Code:
 {if $user_authenticated eq true}Welcome {$user_username}.{/if} The current time is {php}print date("g:i A");{/php}.
Expanding on this idea we can even use {if} conditions that will wrap around {php} tags. For example if we want to redirect users to pligg.com when they try to access the submit page we could use a combination of a pagename variable and PHP's header redirect ability to forward them to another page. The sample code below will do just that and would be added somewhere at the top of either the pligg.tpl file so that it would redirect before the page loads any content.
Code:
{if $pagename eq "submit"}{php} header( 'Location: http://www.pligg.com/' ) ;{/php}{/if}


Contributors: Yankidank
Created by Yankidank, 08-11-2009 at 12:55 AM
Last edited by Yankidank, 08-11-2009 at 12:37 PM
2 Comments , 3869 Views
 

Article Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
/var/log/httpd/error_log messages gr8rcake Questions and Comments 4 06-01-2010 12:25 PM
Pligg Errors tcritic Questions and Comments 4 09-01-2009 10:40 AM
Web Programming Basics Yankidank Wiki Articles 1 09-03-2007 06:51 PM
cann't vote in pligg (buliding 802) haxhax Questions and Comments 27 03-10-2007 07:53 AM


Pligg Modules and Pligg Templates from Pligg Pro Find support on the Pligg CMS Forum - 24 hours a day! Make a donation to support Pligg CMS development