Page 1 of 8

phpBB Standalone Template Engine

Posted: Sat Apr 18, 2009 12:59 am
by nanothree
In my opinion phpBB has the best templating system i have used. Its so simple and does everything i want a template engine to do. I wanted to be able to use my template engine in my own projects but didnt want to use anything else from phpBB.

After about a year of putting it off i finally decided i would take the plunge and try and get it to work standalone. After doing it, it took about 30 minutes, im really annoyed i didnt do it earlier.

Here is how to do it, it should take you about 10 minutes.

Firstly for convenience sake, you need to get the related classes together.

These are template (includes/template.php) and template_compile(includes/functions_template.php).

Take both of these classes and put them into a single file (template.php). Make sure that you don't include the:

Code: Select all

if (!defined('IN_PHPBB'))
{
    exit;
}
  
This will stop the script. Since we are not using this class from within phpBB we will not have that constant defined.
Once these classes are together you need to make 2 changes to the code.
First find:

Code: Select all

$this->files_template[$handle] = $user->theme['template_id'];
  
and comment it out.
Finally find:

Code: Select all

phpbb_chmod($filename, CHMOD_WRITE);
  
imkingdavid wrote:in 3.0.6. Here's the line you should be looking for:

Code: Select all

phpbb_chmod($filename, CHMOD_READ | CHMOD_WRITE);
and replace it with

Code: Select all

//phpbb_chmod($filename, CHMOD_WRITE);
chmod($filename, 0777);  
Your standalone templating class is now ready. Use is exactly the same as in phpBB except that you need to output the template at the end of your script, as this is taken care of by page_footer usually.

index.php:

Code: Select all

include('classes/Template.php');

$template = new Template();

$template->set_custom_template('templates', 'default'); //this is important as it states where the template files are located

$template->assign_var('TITLE', 'Standalone phpBB Template Class');

$template->set_filenames(array(
    'body' => 'index_body.html'
));

$template->display('body');
 
The code above shows how to use the class. Nearly the same as inside phpBB but you must include the $template->set_custom.... line.

The template files in my example are held in a folder named 'templates'. The directory structure for my little example is as follows.

Code: Select all

/index.php
/classes/
/cache/
    template.php
/templates/
    index_body.html
    site_header.html
    site_footer.html    
By creating 'cache' folder the class caches the files for me, if its not there it simply doesn't do any caching

and my index_body.html looks like this:

Code: Select all

<!-- INCLUDE site_header.html -->
		<h1>{TITLE}</h1>
<!-- INCLUDE site_footer.html -->

I hope some one finds this useful.

Re: phpBB Standalone Template Engine

Posted: Sat Apr 18, 2009 6:48 pm
by Raimon
Moved to mod writers discussion since it is related to coding ;)

Re: phpBB Standalone Template Engine

Posted: Thu Jun 18, 2009 7:48 am
by Lyr
Hello.

I would like to add one advice from my own experience as "Template engine beginner who want to try this one for his portal and know very few about phpBB".


I had to replace this thing

Code: Select all

$template->set_custom_template('templates', 'default');
by this one

Code: Select all

$template->set_custom_template(PHPBB_TEMPLATES, 'default');
Otherwise it was unable to find the templates just looking for "templates" directory. PHPBB_TEMPLATES is defined in my entry point as the following

Code: Select all

...
$null = define('PORTAL_BASE_DIR', dirname($_SERVER['SCRIPT_FILENAME'])  );
...
// phpBB template engine informations
$null = define('PHPBB_ROOT_PATH', PORTAL_BASE_DIR.'/view');
$null = define('PHPBB_TEMPLATES', PHPBB_ROOT_PATH.'/templates');
Note that defining PHPBB_ROOT_PATH is mandatory (or so I understood).


Thanks nanothree for the explanation !

Re: phpBB Standalone Template Engine

Posted: Thu Nov 19, 2009 4:37 am
by Bogey
It took me a while to post this here but... thanks for posting this :D It was great

Re: phpBB Standalone Template Engine

Posted: Thu Nov 19, 2009 3:25 pm
by stickerboy
Indeed and bookmarked. Starting a new project and this will help me greatly. Was actually about to try and do it myself, just my luck that I find it today :) Cheers again.

Re: phpBB Standalone Template Engine

Posted: Sat Nov 21, 2009 6:19 pm
by nanothree
no problem,

i'm glad its come to some use for you guys

Re: phpBB Standalone Template Engine

Posted: Sat Nov 21, 2009 9:43 pm
by Sam
Nice topic, I will be starting a project in the coming months and I really wanted to use the phpBB template engine, but I was too lazy to go remove all the dependencies to the phpBB install. Thanks!

Bookmarked. :D

Re: phpBB Standalone Template Engine

Posted: Sun Nov 22, 2009 8:16 pm
by imkingdavid
Definitely a great thing to know. Thanks for taking the time to figure this out. I may be using this on a few projects in the future. :D Bookmarked. :ugeek:

Re: phpBB Standalone Template Engine

Posted: Sun Nov 22, 2009 8:51 pm
by John P
Bookmarked!!

Re: phpBB Standalone Template Engine

Posted: Sun Nov 22, 2009 9:17 pm
by igorw
Thanks for sharing, bookmarked. ;)

Re: phpBB Standalone Template Engine

Posted: Sun Nov 22, 2009 11:08 pm
by Nelsaidi
Thanks for sharing + Bookmarked, but noticed one thing:

Code: Select all

chmod($filename, 777);  
Should be

Code: Select all

chmod($filename, 0777);   
Reason: "To ensure the expected operation, you need to prefix mode with a zero (0): " - See http://uk2.php.net/chmod

Re: phpBB Standalone Template Engine

Posted: Tue Nov 24, 2009 4:53 pm
by nanothree
thanks Nelsaidi, i've updated the original post with your fix.

Re: phpBB Standalone Template Engine

Posted: Mon Dec 14, 2009 5:12 am
by thesilmister
I am having some serious problems with using the phpBB template parser in my own website, separate to phpBB.

Does anyone have the modified template.php file with a list of the required variables needed in order to execute the template engine.

Thank you.

Re: phpBB Standalone Template Engine

Posted: Mon Dec 28, 2009 9:45 pm
by squeek
hello, not sure if this topic is still active but thought i'd post here anyway.
this is just what i have been looking for but ive encountered a problem

These are template (includes/template.php) and template_compile(functions/functions_template.php).

the line above doesnt make sense, i can find the (includes/template.php) but i cant find (functions/functions_template.php) because there is no directory called functions? i figured you were referring to the file functions_template.php in the includes folder so i tried using that. i found by using that file i couldnt find the lines below...

$this->files_template[$handle] = $user->theme['template_id'];

or

phpbb_chmod($filename, CHMOD_WRITE);

could someone please correct me and point me in the right direction. i really hope this helps me to get started and thanks for posting :)

Re: phpBB Standalone Template Engine

Posted: Mon Dec 28, 2009 11:42 pm
by imkingdavid
squeek wrote:hello, not sure if this topic is still active but thought i'd post here anyway.
this is just what i have been looking for but ive encountered a problem

These are template (includes/template.php) and template_compile(functions/functions_template.php).

the line above doesnt make sense, i can find the (includes/template.php) but i cant find (functions/functions_template.php) because there is no directory called functions? i figured you were referring to the file functions_template.php in the includes folder so i tried using that. i found by using that file i couldnt find the lines below...

$this->files_template[$handle] = $user->theme['template_id'];

or

phpbb_chmod($filename, CHMOD_WRITE);

could someone please correct me and point me in the right direction. i really hope this helps me to get started and thanks for posting :)
You are correct, he is referring to includes/functions_template.php. For the first line, you were going to comment it out anyway, so if it's not there it shold be alright.

For the second one, they changed it in 3.0.6. Here's the line you should be looking for:

Code: Select all

phpbb_chmod($filename, CHMOD_READ | CHMOD_WRITE);
I hope this helps. :)