[RFC] Template procedures

Note: We are moving the topics of this forum and it will be deleted at some point

Publish your own request for comments/change or patches for the next version of phpBB. Discuss the contributions and proposals of others. Upcoming releases are 3.2/Rhea and 3.3.
APTX
Registered User
Posts: 680
Joined: Thu Apr 24, 2003 12:07 pm

[RFC] Template procedures

Post by APTX »

This is something, that IIRC was not requested by anyone. I had this idea today and wanted to see if something like this is possible with the current template engine. Turns out it is. :)

Procedures in templates is just some template code you can use wherever and as many times as you like. It has no understanding of scope.

Syntax
A simple procedure:

Code: Select all

<!-- PROCEDURE my_procedure -->
	hello world<br />
<!-- ENDPROCEDURE -->
As you surely know, it will print "hello world<br />".

You can use any tags (except PROCEDURE) in a procedure:

Code: Select all

<!-- PROCEDURE my_procedure -->
	hello world<br />

	{S_USERNAME}<br />

	<!-- IF 1 -->
		{$FOO}<br />
	<!-- ENDIF -->

	{L_WHO_IS_ONLINE}<br />

	<!-- INCLUDE forumlist_body.html --><br />

<!-- ENDPROCEDURE -->
Procedures are invoked using the CALL tag:

Code: Select all

<!-- CALL my_procedure -->
The argument to the CALL tag can be a variable:

Code: Select all

<!-- PROCEDURE my_procedure -->
	hello world<br />
<!-- ENDPROCEDURE -->

<!-- DEFINE $MY_PROCEDURE = 'my_procedure' -->
<!-- CALL $MY_PROCEDURE -->
As there is no scope things like this will work:

Code: Select all

<!-- PROCEDURE my_procedure -->
	$VARIABLE is {$VARIABLE}
<!-- ENDPROCEDURE -->
<!-- DEFINE $VARIABLE = 'hello world' -->
<!-- CALL my_procedure -->
... and print "VARIABLE is hello world"

The code is attached to this post. Is it worth adding it to trunk?
Attachments
functions_template.php.txt
(23.02 KiB) Downloaded 920 times
Don't give me my freedom out of pity!

User avatar
Kellanved
Former Team Member
Posts: 407
Joined: Sun Jul 30, 2006 4:59 pm
Location: Berlin

Re: Template procedures

Post by Kellanved »

I vote "yes". Very nice.
No support via PM.
Trust me, I'm a doctor.

User avatar
naderman
Consultant
Posts: 1727
Joined: Sun Jan 11, 2004 2:11 am
Location: Berlin, Germany
Contact:

Re: Template procedures

Post by naderman »

Slowly I'm starting to wonder why we don't just use PHP instead of our own template language?

User avatar
DavidMJ
Registered User
Posts: 932
Joined: Thu Jun 16, 2005 1:14 am
Location: Great Neck, NY

Re: Template procedures

Post by DavidMJ »

naderman wrote:Slowly I'm starting to wonder why we don't just use PHP instead of our own template language?
It would be too easy to cheat and do bad things in template land... They would also be harder to verify by the styles team... Our system is very nice for lots of loops
Freedom from fear

APTX
Registered User
Posts: 680
Joined: Thu Apr 24, 2003 12:07 pm

Re: Template procedures

Post by APTX »

naderman wrote:Slowly I'm starting to wonder why we don't just use PHP instead of our own template language?
The most important reason is the ease of use. Would you really want to write templates like the compiled ones?
It is much harder to break things by in templates. The only way to break something is to make syntax errors.
Don't give me my freedom out of pity!

User avatar
Acyd Burn
Posts: 1838
Joined: Tue Oct 08, 2002 5:18 pm
Location: Behind You
Contact:

Re: Template procedures

Post by Acyd Burn »

naderman wrote:Slowly I'm starting to wonder why we don't just use PHP instead of our own template language?
Not using PHP on the user-facing side does not only has benefits for controlling what is possible and not, but also for not putting too much code logic into templates (something i utterly hate in smarty), as well as being a well-understood format [html-comments] (the format is something i utterly hate in smarty and php templates) - which comes with correct syntax highlighting. Another benefit is us using .html (here, php parsing is not allowed, or should not). If you would use .php, style authors would need to use echo or heredoc or even opening/closing php tags - can you imagine the mess? Stylers do not necessarily know PHP - they normally do not even know what it is at all.

No, i really like our template engine and i think it is the best part in phpBB 2.0.x for example and moreso in phpBB 3.0.x. Do not go back to the 1.0.x times...

Re procedures:

Where would someone define those? Since these are procedures i think they are mostly not defined within the same file they are used in?
What are the benefits, the real world uses? Could you give a real-world example where it is really beneficial?

Image

User avatar
Kellanved
Former Team Member
Posts: 407
Joined: Sun Jul 30, 2006 4:59 pm
Location: Berlin

Re: Template procedures

Post by Kellanved »

Essentially we are doing it with include now - jumpbox.html and the other very small template files are in effect "procedures".
No support via PM.
Trust me, I'm a doctor.

User avatar
DavidMJ
Registered User
Posts: 932
Joined: Thu Jun 16, 2005 1:14 am
Location: Great Neck, NY

Re: Template procedures

Post by DavidMJ »

a) I suggest a nicer name, like SNIPPET
b) does it make sense to allow us to pass a parameter or no?

Other than that, it looks pretty good.

Btw, any hope of support for arbitrary general expressions in places like DEFINE?
Freedom from fear

APTX
Registered User
Posts: 680
Joined: Thu Apr 24, 2003 12:07 pm

Re: Template procedures

Post by APTX »

Acyd Burn wrote:Where would someone define those? Since these are procedures i think they are mostly not defined within the same file they are used in?
They can be defined anywhere. It's implemented as PHP functions so it can be used like PHP functions. ;)
Acyd Burn wrote:What are the benefits, the real world uses? Could you give a real-world example where it is really beneficial?
I didn't have anything in mind while writing this.
DavidMJ wrote:b) does it make sense to allow us to pass a parameter or no?
Sense? Yes, of course it makes sense! To make normal functions these procedures would need to be able to take arguments. To allow them that the template engine would have to understand scope and for the arguments to be useful you'd need that "arbitrary general expressions" support.
DavidMJ wrote:Btw, any hope of support for arbitrary general expressions in places like DEFINE?
There's always hope. :P
Don't give me my freedom out of pity!

User avatar
Acyd Burn
Posts: 1838
Joined: Tue Oct 08, 2002 5:18 pm
Location: Behind You
Contact:

Re: Template procedures

Post by Acyd Burn »

Personally i think without any real world usage it is pointless to add something like this to the template engine. Our strength comes from simplicity - do not try to go the Smarty route in implementing anything and everything and trying to mimick PHP in a template engine. I still want to hear real usage examples. For example, could it be used to generate something for the "User name and colouring"? We have one function within phpBB to do it, we have several different places where we display coloured usernames and groups and basically everywhere we use the same HTML "Snippet". Additionally, it really does not make sense to just say: "Define it where you want" - it has to be determined where the best place would be. As i see it, the feature is meant to make these snippets available on every pageview, be it viewtopic or viewforum - but what are the benefits if you need to add them to both template files. Overall header is also not really the right place. So, best would be maybe an INCLUDE in overall_header. We want to split up template files a bit more anyway.

Image

Post Reply