Templating ... a simple guide

All style (template, theme and imageset) related questions for the new release; advice, feedback here please.
Post Reply
Ptirhiik
Registered User
Posts: 144
Joined: Sun Apr 06, 2003 12:29 pm

Re: Templating ... a simple guide

Post by Ptirhiik » Sat Nov 15, 2003 9:16 pm

There also let's go to a more detailled module :

Code: Select all

[../..]some stuff regarding registering the module (basicely add to the modules[] array the names of the functions and other parameters if required)

function pre_process_thisone($module)
{
	[../..]initialisation specific to the module, ie count the max records of a selection
	return '';
}

function process_thisone($module)
{
	global $template;
	// sav template state
	// process
	$template->set_filename(array('module' => 'thisone.tpl'));
	$template->assign_vars(array(
	));
	// some stuff
	template->assign_block_vars('row', array(
	));
	// get back the value
	$template->assign_var_from_handle('module', '_module');
	$res = $template->_tpldata['.'][0]['_module'];
	// restore the template state
	// return
	return $res;
}

function post_process_thisone($module)
{
	[../..]some stuff regarding the pagination, the system vars, hidden fields, etc
}
As you can see, the parsing the template thisone.tpl is not linked to any display, and result to a single entity than can be used for display, or to generate formatted files, and so. The main script never refered directly to the template file (why should it do, it never manages it ?), but always to this final result.

How will this be covered by 2.1 templating system ? I don't think the include feature is a respond to this need, as it sounds rather be a merge of templates files together (if I have correctly got it). The real need is to get back the result of assigning vars to a template, and only of this template, even if the main template has already vars assigned. (note that the need is exactly the same than parsing bbcodes ie in a message, but is solved here by using the templating system than rather writing a new one as it is done for bbcode in phpBB 2.0)

User avatar
Arty
Former Team Member
Posts: 985
Joined: Wed Mar 06, 2002 2:36 pm
Location: Mars
Contact:

Re: Templating ... a simple guide

Post by Arty » Sat Nov 15, 2003 10:46 pm

Here it is. Compatible with 2.2 template system and much faster than your method (because assign_var_from_handle is awfully slow):

Code: Select all

function process_thisone($module)
{
   global $template;
   // sav template state
   // process
   $template->set_filename(array('module' => 'thisone.tpl'));
   $template->assign_vars(array(
   ));
   // some stuff
   template->assign_block_vars('row', array(
   ));
   // get back the value
   ob_start();
   $template->display('module');
   $res = ob_get_contents();
   ob_end_clean();
   // restore the template state
   // return
   return $res;
}

Ptirhiik
Registered User
Posts: 144
Joined: Sun Apr 06, 2003 12:29 pm

Re: Templating ... a simple guide

Post by Ptirhiik » Sat Nov 15, 2003 10:57 pm

Rhaaa no : I don't want no echoing to the display : it hasn't to go to the output page until it is required, and there it is not (remember the result can go to a flat file, or being repeated many time on the display). It is the purpose of storing the result to a var. :)

(and as stated the ob_ funcs can't be retained as a solution because linked to the server setting).

User avatar
Arty
Former Team Member
Posts: 985
Joined: Wed Mar 06, 2002 2:36 pm
Location: Mars
Contact:

Re: Templating ... a simple guide

Post by Arty » Sat Nov 15, 2003 11:08 pm

Ptirhiik wrote:I don't want no echoing to the display
Read php manual about ob_start, ob_get_contents, ob_end_clean. Without it this discussion is pointless.
Ptirhiik wrote:and as stated the ob_ funcs can't be retained as a solution because linked to the server setting
No, it isn't linked to settings. It works with all configurations.

User avatar
psoTFX
Registered User
Posts: 1984
Joined: Tue Jul 03, 2001 8:50 pm
Contact:

Re: Templating ... a simple guide

Post by psoTFX » Sat Nov 15, 2003 11:10 pm

Ptirhiik wrote:(and as stated the ob_ funcs can't be retained as a solution because linked to the server setting).
What server setting? Output control functions are part of the PHP 4.x core. The only (current) issue relates to implicit_flush which needs changing in php.ini when upgrading to 4.3+. phpBB 2.2 requires PHP 4.1.0+ to work.

Ptirhiik
Registered User
Posts: 144
Joined: Sun Apr 06, 2003 12:29 pm

Re: Templating ... a simple guide

Post by Ptirhiik » Sun Nov 16, 2003 12:05 am

/me stop there the discussion and note a reminder as a prealable modification to apply to any 2.2 version for further

metest12
Registered User
Posts: 15
Joined: Fri Jun 20, 2003 5:29 pm

Re: Templating ... a simple guide

Post by metest12 » Thu Nov 27, 2003 6:21 pm

I found a way to include variable templates in your templates. But first, run this query on your DB to enable PHP in your templates:

Code: Select all

INSERT INTO `phpbb_config` VALUES ('tpl_php', '1', 1);
That was for MySql of course. Now, all you have to do is use the _tpl_include function like this in your templates (ignore the starting <?php as this is just to enable syntax highlighting):

Code: Select all

<?php
<!-- PHP -->
$this->_tpl_include($this->_tpldata['.'][0]['MY_TPL']);
<!-- ENDPHP -->
That will include the template whos name is in the root template var {MY_TPL}. It functions exactly the same as <!-- INCLUDE --> (actually because all it is really is the compiled version of <!-- INCLUDE -->. I looked in the cached templates).

EDIT: For some reason syntax highlighting doesn't work anymore so totally ignore the <?php

Roberdin
Registered User
Posts: 1546
Joined: Wed Apr 09, 2003 8:44 pm
Location: London, United Kingdom

Re: Templating ... a simple guide

Post by Roberdin » Fri Nov 28, 2003 4:36 pm

Of course, note that phpBB will not support running php in templates for obvious reasons.
Rob

User avatar
Ptirhiik_
Registered User
Posts: 526
Joined: Tue Nov 18, 2003 8:35 am

Re: Templating ... a simple guide

Post by Ptirhiik_ » Sat Nov 29, 2003 3:53 am

For the reasons explains above, this won't be convinient, but thanks anyway ;). Forget that a template is only usable to generate a display, as it is not my need : my need is to use the template to generate a var. Anyway, I know now how I will proceed to add the required function, without using any output nor includes.

metest12
Registered User
Posts: 15
Joined: Fri Jun 20, 2003 5:29 pm

Re: Templating ... a simple guide

Post by metest12 » Fri Dec 05, 2003 4:22 pm

Roberdin wrote:Of course, note that phpBB will not support running php in templates for obvious reasons.


2.2 will, as stated in the first post of this topic:
psoTFX wrote:PHP

A contentious decision has seen the ability to include PHP within the template introduced. This is achieved by enclosing the PHP within relevant tags:

Post Reply