Templating ... a simple guide

All style (template, theme and imageset) related questions for the new release; advice, feedback here please.
Post Reply
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 »

Ptirhiik wrote:res are never a static tpl
In that case your code for 2.0 wasn't correct. When you use assign_var_from_handle() in 2.0 those handles should already exist, so you should have used set_filenames() before using it. assign_var_from_handle() just parses one of templates that you have assigned. So in code that you wrote number of templates is very limited and <!-- IF --> will work perfectly.
Ptirhiik wrote:When you are at this point, you don't know how many and which tpls you will use.
Then you can use usual output buffering (and if developers would want to implement assign_var_from_handle they would use this method anyway so it doesn't matter):

Code: Select all

$template->set_filenames(array('my_tpl' => $res));
ob_start();
$template->display('my_tpl');
$data = ob_get_contents();
ob_end_clean();
$template->assign_vars(array('MY_VAR' => $data));

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

Re: Templating ... a simple guide

Post by Ptirhiik »

CyberAlien wrote:
Ptirhiik wrote:res are never a static tpl
In that case your code for 2.0 wasn't correct. When you use assign_var_from_handle() in 2.0 those handles should already exist, so you should have used set_filenames() before using it. assign_var_from_handle() just parses one of templates that you have assigned. So in code that you wrote number of templates is very limited and <!-- IF --> will work perfectly.
Sounds like you didn't explored all the capabilities of the phpBB 2.0.x template system :). Believe me, using the template system, you can have dynamic template resulting in a var without any display (very usefull to generate ie txt/php files, or email :)), and in a perfectly correct way : even the various template caches (phpBB contrib dir or extreme style mod) handle this softly :),
Ptirhiik wrote:When you are at this point, you don't know how many and which tpls you will use.
Then you can use usual output buffering (and if developers would want to implement assign_var_from_handle they would use this method anyway so it doesn't matter):

Code: Select all

$template->set_filenames(array('my_tpl' => $res));
ob_start();
$template->display('my_tpl');
$data = ob_get_contents();
ob_end_clean();
$template->assign_vars(array('MY_VAR' => $data));
If you do that, you will be tied with your server configuration, and from there it is not a solution than can be choosen. BTW, it will require to process an echo, what I don't want : only the result of parsing interests me, what perfectly do the assign_var_from_handle() function. The pparse() is never done during this process.

What you are proposing here is hardcoding all the outputs in the main script, for names and numbers. What allows phpBB 2.0 system is to use any templates, unknown from the main script, to be used to fill a single main var, and so have a plug-ins capability (new feature => new func and new templates, 0 modifications of the main script nor main template). At this time, I really don't figure how it can be covered by the templating system of phpBB 2.1 .

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 »

Ptirhiik wrote:Sounds like you didn't explored all the capabilities of the phpBB 2.0.x template system :). Believe me, using the template system, you can have dynamic template resulting in a var without any display (very usefull to generate ie txt/php files, or email :)), and in a perfectly correct way : even the various template caches (phpBB contrib dir or extreme style mod) handle this softly :),
I know, but your code that you pasted wasn't using it correctly. If you would write in that code that you dynamically assign filename to handle then it would be code that you wanted.
Ptirhiik wrote:If you do that, you will be tied with your server configuration, and from there it is not a solution than can be choosen.
No. It works with all configurations.
Ptirhiik wrote:BTW, it will require to process an echo, what I don't want : only the result of parsing interests me, what perfectly do the assign_var_from_handle() function. The pparse() is never done during this process.
I don't think you looked at code of old template.php close enough. in 2.0 assign_var_from_handle() does same as pparse() - it compiles template and runs it. But unlike pparse it puts content to string instead of echo() and that's slower than echo because of constant contecating.

And you didn't look close enough at compiled template code for 2.2 ether. It doesn't use echo().

Using buffering is much faster than assigning to variable. Besides, assigning to variable will require different compilation and execution method and that will slow down 2.2 templates system a lot. I don't see point in adding one slow function that will require to slow down everything else when you can use fast buffering instead.
Ptirhiik wrote:What you are proposing here is hardcoding all the outputs in the main script, for names and numbers.
???
Ptirhiik wrote:What allows phpBB 2.0 system is to use any templates, unknown from the main script, to be used to fill a single main var, and so have a plug-ins capability (new feature => new func and new templates, 0 modifications of the main script nor main template). At this time, I really don't figure how it can be covered by the templating system of phpBB 2.1 .
You can do exactly the same in 2.2 by buffering output. Instead of assigning content to variable better echo that content and use buffering to catch all data - its much faster and more flexible than old method.

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

Re: Templating ... a simple guide

Post by Ptirhiik »

You definitively didn't get the point here, probably because I explain it not enough clearly (or you are not used to use template for other thing than linear display). BTW, ob_ functions aren't available on all config : this is linked to a setup value.

There is a major difference between pparse and assign_var : one add echoes as you mentioned, not the other, and that is preciasly this point that isn't cover by the 2.1 template way. I have a modular approach, where you have a linear approach, that's why I think you don't get what I mean :). Think simply that in a spot a script has to be able to handle anything as long as the client function return a formated as expected result (in the case of the assign_var_from_handle(), a formatted html display part, not qualified by the main script).

What I wrote as example is actually a scheme, not a code : writing a code would have require more lines, but won't have been more understandle ;).

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

Re: Templating ... a simple guide

Post by psoTFX »

You must know which template you're going to use for a given situation else it makes absolutely no sense at all ... it cannot simply go "making up" template names, if they don't exist they cannot be loaded. It may require you to alter the way you code slightly but I don't really see why the INCLUDE system limits you.

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

Re: Templating ... a simple guide

Post by Ptirhiik »

Let's try to reformulated so, in a more complex but remaining simple system.

You have a blank page, and a script managing this blank page with a plug-ins system. With phpBB 2.0.x, you can write this :

Main template

Code: Select all

[../..]some presentation stuff, ie table surrounder
<!-- BEGIN module -->
{module.DATA}
<!-- END module -->
[../..]some other stuff, ie table closure
Main script

Code: Select all

[../..]some initialisation stuff, as header, context, etc.

// each module has its own process functions stored in an array loaded on first include
// get the module
$dir = @opendir($phpbb_root_path . $modules_path);
$setmodules = true;
while( $file = @readdir($dir) )
{
	if( preg_match("/^module_.*?\." . $phpEx . "$/", $file) )
	{
		include($phpbb_root_path . $module_path . '/' . $file);
	}
}
@closedir($dir);
unset($setmodules);

// loop on pre process/process/post process, then on module
for ( $j = 0; $j < 3; $j++ )
{
	for ( $i = 0; $i < count($modules); $i++ )
	{
		switch ($j)
		{
			case 0:
				// pre-process
				$pre = $modules[$i]['pre_process_func'];
				$modules[$i]['init_result'] = $$pre($modules[$i]);
			case 1:
				// process
				$process = $modules[$i]['process_func'];
				$modules[$i]['result'] = $$process($modules[$i]);
			case 2:
				// post process
				$post = $modules[$i]['post_process_func'];
				$modules[$i]['post_result'] = $$post($modules[$i]);
		}
	}
}

// handle some stuff with the pre and post globaly, ie paginations sentences, other sub-menus, etc.
for ( $i = 0; $i < count($modules); $i++ )
{
}

// send to template
for ( $i = 0; $i < count($modules); $i++ )
{
	$template->assign_block_vars('module', array(
		'DATA' => $modules[$i]['result'],
		)
	);
}
Modules
Each modules will perform its own display manipulating its own template, managing its own cases (ie link to the caller context) and giving the results to the modules[]['result'] and others pre_result and post_result, using an assign_var_from_handle() to return it as function result, whatever it looks like. It just doesn't perform the $template->pparse();

Doing like this, you can add - or remove - any module, and use it in other contexts, managing all its specificities only in the module script, and this won't require any modification to the main script : this is what I call plug-ins client functions.

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

Re: Templating ... a simple guide

Post by psoTFX »

Well, we don't have a plug-in system like that in 2.2 so the issue is somewhat moot. However the situation you describe could be solved by simply allowing vars within the INCLUDE directive, e.g. <!-- INCLUDE {loop.MODULE} -->

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

Re: Templating ... a simple guide

Post by Ptirhiik »

How will you call back the template stuff done by the client function ? It is for what the assign_var_from_handle() is usefull, not really setting of a single template var looped in the main template.


ps: btw, this way to have on some place an open for modules could really give phpBB 2.2 a further step in modular board and reduce the need to edit core file while adding new functions/options/capabilities.

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 »

Ptirhiik wrote:There is a major difference between pparse and assign_var : one add echoes as you mentioned, not the other, and that is preciasly this point that isn't cover by the 2.1 template way. I have a modular approach, where you have a linear approach, that's why I think you don't get what I mean :).
Everything you wrote above can be acomplished by using buffering or dynamic include.

And if you would look at my modified template.php for 2.0 for eXtreme Styles mod you'll see that assign_var_from_handle() uses pparse() with buffering (same method that i suggested you here) and it perfectly works with all mods including mods like statistics mod that use modules.

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

Re: Templating ... a simple guide

Post by psoTFX »

Ptirhiik wrote:How will you call back the template stuff done by the client function ? It is for what the assign_var_from_handle() is usefull, not really setting of a single template var looped in the main template.
Sorry again I'm not following ... "call back the template stufff"? You assign values to variables which are "replaced" within the template. You don't and never have assigned vars to specific template files or source. Equally you can assign data to templates "wherever", it's not restricted to any particular file or function.
Ptirhiik wrote:ps: btw, this way to have on some place an open for modules could really give phpBB 2.2 a further step in modular board and reduce the need to edit core file while adding new functions/options/capabilities.
This isn't the place for this discussion ... 2.2 is highly modular, modular acp, mcp and ucp, a plug-in (hopefully) events system. That's as far as we're going with 2.2.

Post Reply