These are the phpBB Coding Guidelines for Olympus, all attempts should be made to follow it as closely as possible. This document is (c) 2006 phpBB Group, copying or redistribution is not allowed without permission.
+These are the phpBB Coding Guidelines for Olympus, all attempts should be made to follow it as closely as possible.
This document is (c) 2006 phpBB Group, copying or redistribution is not allowed without permission.
Coding Guidelines
@@ -280,9 +283,7 @@ body {/** -* * {HEADER} -* */ /** @@ -304,31 +305,26 @@ class ...
-Top @@ -830,9 +960,11 @@ for ($i = 0, $size = sizeof($post_data); $i < $size; $i++)Functions used by more than one page should be placed in functions.php, functions specific to one page should be placed on that page (at the bottom) or within the relevant sections functions file.
+Functions used by more than one page should be placed in functions.php, functions specific to one page should be placed on that page (at the bottom) or within the relevant sections functions file. Some files in
/includes
are holding functions responsible for special sections, for example uploading files, displaying "things", user related functions and so forth.The following packages are defined, and related new features/functions should be placed within the mentioned files/locations, as well as specifying the correct package name. The package names are bold within this list:
@@ -921,7 +1073,7 @@ trigger_error('NO_FORUM');General things:
-Never trust user input.
-The auth class should be used for all authorisation checking
-No attempt should be made to remove any copyright information (either contained within the source or displayed interactively when the source is run/compiled), neither should the copyright information be altered in any way (it may be added to)
+Never trust user input (this also applies to server variables as well as cookies).
+Try to sanitize values returned from a function.
+Try to sanitize given function variables within your function.
+The auth class should be used for all authorisation checking.
+No attempt should be made to remove any copyright information (either contained within the source or displayed interactively when the source is run/compiled), neither should the copyright information be altered in any way (it may be added to).
Variables:
Make use of the
@@ -868,7 +1000,7 @@ $action_ary = request_var('action', array('' => 0));request_var()
function for anything except for submit or single checking params.Login checks/redirection:
To show a forum login box use
-login_forum_box($forum_data)
, else use thelogin_box()
function.The
+login_box()
function could have a redirect as the first parameter. As a thumb of rule, specify an empty string if you want to redirect to the users current location, else do not add the$SID
to the redirect string (for example within the ucp/login we redirect to the board index because else the user would be redirected to the login screen).The
login_box()
function can have a redirect as the first parameter. As a thumb of rule, specify an empty string if you want to redirect to the users current location, else do not add the$SID
to the redirect string (for example within the ucp/login we redirect to the board index because else the user would be redirected to the login screen).Sensitive Operations:
For sensitive operations always let the user confirm the action. For the confirmation screens, make use of the
@@ -882,22 +1014,36 @@ $auth->acl($user->data); $user->setup(); -confirm_box()
function.The
+$user->setup()
call can be used to pass on additional language definitions and a custom style (used in viewforum).The
$user->setup()
call can be used to pass on additional language definition and a custom style (used in viewforum).Errors and messages:
-All messages/errors should be output by calling
+trigger_error()
using the appropriate message type and language string. Example:All messages/errors should be outputed by calling
trigger_error()
using the appropriate message type and language string. Example:+trigger_error('NO_FORUM');+ ++trigger_error($user->lang['NO_FORUM']); +++trigger_error('NO_APPROPIATE_MODE', E_USER_ERROR); +Url formatting
-All urls pointing to internal files need to be prepended by the
- +$phpbb_root_path
variable. Within the administration control panel all urls pointing to internal files need to be prepended by the$phpbb_admin_path
variable. This makes sure the path is always correct and users being able to just rename the admin folder and the acp still working as intended.All urls pointing to internal files need to be prepended by the
+ +$phpbb_root_path
variable. Within the administration control panel all urls pointing to internal files need to be prepended by the$phpbb_admin_path
variable. This makes sure the path is always correct and users being able to just rename the admin folder and the acp still working as intended (though some links will fail and the code need to be slightly adjusted).The
+ +append_sid()
function from 2.0.x is available too, though does not handle url alterations automatically. Please have a look at the code documentation if you want to get more details on how to use append_sid(). A sample call to append_sid() can look like this:++append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&g=' . $row['group_id']) +General function usage:
-Some of these functions are only chosen over others because of a personal preference and are having no other benefit than to be consistant over the code.
+Some of these functions are only chosen over others because of personal preference and having no other benefit than to be consistant over the code.
- @@ -909,6 +1055,12 @@ trigger_error('NO_FORUM');
- +
Use
else if
instead ofelseif
- +
+Use
+false
(lowercase) instead ofFALSE
- +
Use
+true
(lowercase) instead ofTRUE
Top @@ -986,50 +1138,26 @@ trigger_error('NO_FORUM');General things
-Templates should be produced in a consistent manner. Where appropriate they should be based off an existing copy, e.g. index, viewforum or viewtopic (the combination of which implement a range of conditional and variable forms).
+Templates should be produced in a consistent manner. Where appropriate they should be based off an existing copy, e.g. index, viewforum or viewtopic (the combination of which implement a range of conditional and variable forms). Please also note that the intendation and coding guidelines also apply to templates where possible.
The outer table class
forumline
has gone and is replaced withtablebg
.When writing
@@ -945,33 +1097,33 @@ trigger_error('NO_FORUM');<table>
the order<table class="" cellspacing="" cellpadding="" border="" align="">
creates consistency and allows everyone to easily see which table produces which "look". The same applies to most other tags for which additional parameters can be set, consistency is the major aim here.Use a standard cellpadding of 2 and cellspacing of 0 on outer tables. Inner tables can vary from 0 to 3 or even 4 depending on the need.
-Use div container for styling and table for data representation
+Use div container/css for styling and table for data representation.
The seperate catXXXX and thXXX classes are gone. When defining a header cell just use
-<th>
rather than<th class="thHead">
etc. Similarly for cat, don't use<td class="catLeft">
use<td class="cat">
etc.Try to retain consistency of basic layout and class useage, i.e. _EXPLAIN text should generally be placed below the title it explains, e.g.
+{L_POST_USERNAME}<br /><span class="gensmall">{L_POST_USERNAME_EXPLAIN}</span>
is the typical way of handling this ... there may be exceptions and this isn't a hard and fast ruleTry to retain consistency of basic layout and class useage, i.e. _EXPLAIN text should generally be placed below the title it explains, e.g.
{L_POST_USERNAME}<br /><span class="gensmall">{L_POST_USERNAME_EXPLAIN}</span>
is the typical way of handling this ... there may be exceptions and this isn't a hard and fast rule.Try to keep template conditional and other statements tabbed in line with the block to which they refer.
-this is incorrect
+this is correct
--<!-- BEGIN test --> +<!-- BEGIN test --> <tr> <td>{test.TEXT}</td> </tr> -<!-- END test --> +<!-- END test -->this is correct:
+this is also correct:
--<!-- BEGIN test --> +<!-- BEGIN test --> <tr> <td>{test.TEXT}</td> </tr> -<!-- END test --> +<!-- END test -->it gives immediate feedback on exactly what is looping.
+it gives immediate feedback on exactly what is looping - decide which way to use based on the readability.
Firstly templates now take the suffix ".html" rather than ".tpl". This was done simply to make the lifes of some people easier wrt syntax highlighting, etc.
Variables
-All template variables should be named appropriately (using underscores for spaces), language entries should be prefixed with L_, system data with S_, urls with U_, all other variables should be presented 'as is'.
+All template variables should be named appropriately (using underscores for spaces), language entries should be prefixed with L_, system data with S_, urls with U_, javascript urls with UA_, language to be put in javascript statements with LA_, all other variables should be presented 'as is'.
-Note that unlike 2.0.x most language strings are not assigned from the source. When a language variable is found {L_YYYYYY} phpBB first looks if an assigned variable exists with that name. If it does, it uses that. If not it looks if an exsting string defined in the language file exists. This should reduce the need to assign loads of new lang vars in Mods.
+L_* template variables are automatically tried to be mapped to the corresponding language entry if the code does not set (and therefore overwrite) this variable specifically. For example
-{L_USERNAME}
maps to$user->lang['USERNAME']
. The LA_* template variables are handled within the same way, but properly escaped to be put in javascript code. This should reduce the need to assign loads of new lang vars in Mods. +Blocks
+Blocks/Loops
The basic block level loop remains and takes the form:
--<!-- BEGIN loopname --> -markup, {loopname.X_YYYYY}, etc. -<!-- END loopname --> +<!-- BEGIN loopname --> + markup, {loopname.X_YYYYY}, etc. +<!-- END loopname -->However this has now been extended with the following additions. Firstly you can set the start and end points of the loop. For example:
- -- --<!-- BEGIN loopname(2) --> -markup -<!-- END loopname --> -Will start the loop on the third entry (note that indexes start at zero). Extensions of this are: -
- -
-loopname(2,4)
: Starts loop on third values, ends on fourth
-loopname(-4)
: Starts loop fourth from last value
-loopname(2, -4)
: Starts loop on third value, ends four from end
-
Note that the indexing method may change since it's not really consistent at this time :)A further extension to begin is BEGINELSE:
- -- --<!-- BEGIN loop --> -markup -<!-- BEGINELSE --> -markup -<!-- END loop --> -This will cause the markup between
+BEGINELSE
andEND
to be output if the loop contains no values. This is useful for forums with no topics (for example) ... in some ways it replaces "bits of" the existing "switch_" type control (the rest being replaced by conditionals, see below).A bit later loops will be explained further. To not irretate you we will explain conditionals as well as other statements first.
Including files
Something that existed in 2.0.x which no longer exists in 3.0.x is the ability to assign a template to a variable. This was used (for example) to output the jumpbox. Instead (perhaps better, perhaps not but certainly more flexible) we now have INCLUDE. This takes the simple form:
-<!-- INCLUDE filename --> +<!-- INCLUDE filename -->
You will note in the 3.0 templates the major sources start with
@@ -1038,15 +1166,15 @@ markup<!-- INCLUDE overall_header.html -->
or<!-- INCLUDE simple_header.html -->
, etc. In 2.0.x control of "which" header to use was defined entirely within the code. In 3.0.x the template designer can output what they like. Note that you can introduce new templates (i.e. other than those in the default set) using this system and include them as you wish ... perhaps useful for a common "menu" bar or some such. No need to modify loads of files as with 2.0.x.A contentious decision has seen the ability to include PHP within the template introduced. This is achieved by enclosing the PHP within relevant tags:
-<!-- PHP --> -echo "hello!"; -<!-- ENDPHP --> +<!-- PHP --> + echo "hello!"; +<!-- ENDPHP -->You may also include PHP from an external file using:
-<!-- INCLUDEPHP somefile.php --> +<!-- INCLUDEPHP somefile.php -->
it will be included and executed inline.
@@ -1055,18 +1183,20 @@ echo "hello!";
A note, it is very much encouraged that template designers do not include PHP. The ability to include raw PHP was introduced primarily to allow end users to include banner code, etc. without modifying multiple files (as with 2.0.x). It was not intended for general use ... hence www.phpbb.com will not make available template sets which include PHP. And by default templates will have PHP disabled (the admin will need to specifically activate PHP for a template).The most significant addition to 3.0.x are conditions or control structures, "if something then do this else do that". The system deployed is very similar to Smarty. This may confuse some people at first but it offers great potential and great flexibility with a little imagination. In their most simple form these constructs take the form:
-<!-- IF expr -->
markup
<!-- ENDIF --> +<!-- IF expr --> + markup +<!-- ENDIF -->expr can take many forms, for example:
--<!-- IF loop.S_ROW_COUNT is even --> -markup -<!-- ENDIF --> +<!-- IF loop.S_ROW_COUNT is even --> + markup +<!-- ENDIF -->This will output the markup if the S_ROW_COUNT variable in the current iteration of loop is an even value (i.e. the expr is TRUE). You can use various comparison methods (standard as well as equivalent textual versions noted in square brackets) including:
+This will output the markup if the S_ROW_COUNT variable in the current iteration of loop is an even value (i.e. the expr is TRUE). You can use various comparison methods (standard as well as equivalent textual versions noted in square brackets) including (
not, or, and, eq, neq, is
should be used if possible for better readability):== [eq] @@ -1086,6 +1216,7 @@ markup - * / +, << (bitwise shift left) >> (bitwise shift right) | (bitwise or) @@ -1106,29 +1237,29 @@ divBeyond the simple use of IF you can also do a sequence of comparisons using the following:
-<!-- IF expr1 --> -markup -<!-- ELSEIF expr2 --> -markup -. -. -. -<!-- ELSEIF exprN --> -markup -<!-- ELSE --> -markup -<!-- ENDIF --> +<!-- IF expr1 --> + markup +<!-- ELSEIF expr2 --> + markup + . + . + . +<!-- ELSEIF exprN --> + markup +<!-- ELSE --> + markup +<!-- ENDIF -->Each statement will be tested in turn and the relevant output generated when a match (if a match) is found. It is not necessary to always use ELSEIF, ELSE can be used alone to match "everything else".
So what can you do with all this? Well take for example the colouration of rows in viewforum. In 2.0.x row colours were predefined within the source as either row color1, row color2 or row class1, row class2. In 3.0.x this is moved to the template, it may look a little daunting at first but remember control flows from top to bottom and it's not too difficult:<table> - <!-- IF loop.S_ROW_COUNT is even --> - <tr class="row1"> - <!-- ELSE --> - <tr class="row2"> - <!-- ENDIF --> + <!-- IF loop.S_ROW_COUNT is even --> + <tr class="row1"> + <!-- ELSE --> + <tr class="row2"> + <!-- ENDIF --> <td>HELLO!</td> </tr> </table> @@ -1138,15 +1269,15 @@ markup<table> - <!-- IF loop.S_ROW_COUNT > 10 --> - <tr bgcolor="#FF0000"> - <!-- ELSEIF loop.S_ROW_COUNT > 5 --> - <tr bgcolor="#00FF00"> - <!-- ELSEIF loop.S_ROW_COUNT > 2 --> - <tr bgcolor="#0000FF"> - <!-- ELSE --> - <tr bgcolor="#FF00FF"> - <!-- ENDIF --> + <!-- IF loop.S_ROW_COUNT > 10 --> + <tr bgcolor="#FF0000"> + <!-- ELSEIF loop.S_ROW_COUNT > 5 --> + <tr bgcolor="#00FF00"> + <!-- ELSEIF loop.S_ROW_COUNT > 2 --> + <tr bgcolor="#0000FF"> + <!-- ELSE --> + <tr bgcolor="#FF00FF"> + <!-- ENDIF --> <td>hello!</td> </tr> </table> @@ -1155,13 +1286,229 @@ markupThis will output the row cell in purple for the first two rows, blue for rows 2 to 5, green for rows 5 to 10 and red for remainder. So, you could produce a "nice" gradient effect, for example.
What else can you do? Well, you could use IF to do common checks on for example the login state of a user:-<!-- IF S_USER_LOGGED_IN --> -markup -<!-- ENDIF --> +<!-- IF S_USER_LOGGED_IN --> + markup +<!-- ENDIF -->This replaces the existing (fudged) method in 2.0.x using a zero length array and BEGIN/END.
+Extended syntax for Blocks/Loops
+ +Back to our loops - they had been extended with the following additions. Firstly you can set the start and end points of the loop. For example:
+ ++ ++<!-- BEGIN loopname(2) --> + markup +<!-- END loopname --> +Will start the loop on the third entry (note that indexes start at zero). Extensions of this are: +
+ +
+loopname(2)
: Will start the loop on the 3rd entry
+loopname(-2)
: Will start the loop two entries from the end
+loopname(3,4)
: Will start the loop on the fourth entry and end it on the fifth
+loopname(3,-4)
: Will start the loop on the fourth entry and end it four from last
+A further extension to begin is BEGINELSE:
+ ++ ++<!-- BEGIN loop --> + markup +<!-- BEGINELSE --> + markup +<!-- END loop --> +This will cause the markup between
+ +BEGINELSE
andEND
to be output if the loop contains no values. This is useful for forums with no topics (for example) ... in some ways it replaces "bits of" the existing "switch_" type control (the rest being replaced by conditionals).Another way of checking if a loop contains values is by prefixing the loops name with a dot:
+ ++ ++<!-- IF .loop --> + <!-- BEGIN loop --> + markup + <!-- END loop --> +<!-- ELSE --> + markup +<!-- ENDIF --> +You are even able to check the number of items within a loop by comparing it with values within the IF condition:
+ ++ ++<!-- IF .loop > 2 --> + <!-- BEGIN loop --> + markup + <!-- END loop --> +<!-- ELSE --> + markup +<!-- ENDIF --> +Nesting loops cause the conditionals needing prefixed with all loops from the outer one to the inner most. An illustration of this:
+ ++ ++<!-- BEGIN firstloop --> + {firstloop.MY_VARIABLE_FROM_FIRSTLOOP} + + <!-- BEGIN secondloop --> + {firstloop.secondloop.MY_VARIABLE_FROM_SECONDLOOP} + <!-- END secondloop --> +<!-- END firstloop --> +Sometimes it is necessary to break out of nested loops to be able to call another loop within the current iteration. This sounds a little bit confusing and it is not used very often. The following (rather complex) example shows this quite good - it also shows how you test for the first and last row in a loop (i will explain the example in detail further down):
+ ++ ++<!-- BEGIN l_block1 --> + <!-- IF l_block1.S_SELECTED --> + <strong>{l_block1.L_TITLE}</strong> + <!-- IF S_PRIVMSGS --> + + <!-- the ! at the beginning of the loop name forces the loop to be not a nested one of l_block1 --> + <!-- BEGIN !folder --> + <!-- IF folder.S_FIRST_ROW --> + <ul class="nav"> + <!-- ENDIF --> + + <li><a href="{folder.U_FOLDER}">{folder.FOLDER_NAME}</a></li> + + <!-- IF folder.S_LAST_ROW --> + </ul> + <!-- ENDIF --> + <!-- END !folder --> + + <!-- ENDIF --> + + <ul class="nav"> + <!-- BEGIN l_block2 --> + <li> + <!-- IF l_block1.l_block2.S_SELECTED --> + <strong>{l_block1.l_block2.L_TITLE}</strong> + <!-- ELSE --> + <a href="{l_block1.l_block2.U_TITLE}">{l_block1.l_block2.L_TITLE}</a> + <!-- ENDIF --> + </li> + <!-- END l_block2 --> + </ul> + <!-- ELSE --> + <a class="nav" href="{l_block1.U_TITLE}">{l_block1.L_TITLE}</a> + <!-- ENDIF --> +<!-- END l_block1 --> +Let us first concentrate on this part of the example:
+ ++ ++<!-- BEGIN l_block1 --> + <!-- IF l_block1.S_SELECTED --> + markup + <!-- ELSE --> + <a class="nav" href="{l_block1.U_TITLE}">{l_block1.L_TITLE}</a> + <!-- ENDIF --> +<!-- END l_block1 --> +Here we open the loop l_block1 and doing some things if the value S_SELECTED within the current loop iteration is true, else we write the blocks link and title. Here, you see
+ +{l_block1.L_TITLE}
referenced - you remember that L_* variables get automatically assigned the corresponding language entry? This is true, but not within loops. The L_TITLE variable within the loop l_block1 is assigned within the code itself.Let's have a closer look to the markup:
+ ++ ++<!-- BEGIN l_block1 --> +. +. + <!-- IF S_PRIVMSGS --> + + <!-- BEGIN !folder --> + <!-- IF folder.S_FIRST_ROW --> + <ul class="nav"> + <!-- ENDIF --> + + <li><a href="{folder.U_FOLDER}">{folder.FOLDER_NAME}</a></li> + + <!-- IF folder.S_LAST_ROW --> + </ul> + <!-- ENDIF --> + <!-- END !folder --> + + <!-- ENDIF --> +. +. +<!-- END l_block1 --> +The
+ +<!-- IF S_PRIVMSGS -->
statement clearly checks a global variable and not one within the loop, since the loop is not given here. So, if S_PRIVMSGS is true we execute the shown markup. Now, you see the<!-- BEGIN !folder -->
statement. The exclamation mark is responsible for instructing the template engine to iterate through the main loop folder. So, we are now within the loop folder - with<!-- BEGIN folder -->
we would have been within the loopl_block1.folder
automatically as is the case with l_block2:+ ++<!-- BEGIN l_block1 --> +. +. + <ul class="nav"> + <!-- BEGIN l_block2 --> + <li> + <!-- IF l_block1.l_block2.S_SELECTED --> + <strong>{l_block1.l_block2.L_TITLE}</strong> + <!-- ELSE --> + <a href="{l_block1.l_block2.U_TITLE}">{l_block1.l_block2.L_TITLE}</a> + <!-- ENDIF --> + </li> + <!-- END l_block2 --> + </ul> +. +. +<!-- END l_block1 --> +You see the difference? The loop l_block2 is a member of the loop l_block1 but the loop folder is a main loop.
+ +Now back to our folder loop:
+ ++ ++<!-- IF folder.S_FIRST_ROW --> + <ul class="nav"> +<!-- ENDIF --> + +<li><a href="{folder.U_FOLDER}">{folder.FOLDER_NAME}</a></li> + +<!-- IF folder.S_LAST_ROW --> + </ul> +<!-- ENDIF --> +You may have wondered what the comparison to S_FIRST_ROW and S_LAST_ROW is about. If you haven't guessed already - it is checking for the first iteration of the loop with
+ +S_FIRST_ROW
and the last iteration withS_LAST_ROW
. This can come in handy quite often if you want to open or close design elements, like the above list. Let us imagine a folder loop build with three iterations, it would go this way:+ ++<ul class="nav"> <!-- written on first iteration --> + <li>first element</li> <!-- written on first iteration --> + <li>second element</li> <!-- written on second iteration --> + <li>third element</li> <!-- written on third iteration --> +</ul> <!-- written on third iteration --> +As you can see, all three elements are written down as well as the markup for the first iteration and the last one. Sometimes you want to omit writing the general markup - for example:
+ ++ ++<!-- IF folder.S_FIRST_ROW --> + <ul class="nav"> +<!-- ELSEIF folder.S_LAST_ROW --> + </ul> +<!-- ELSE --> + <li><a href="{folder.U_FOLDER}">{folder.FOLDER_NAME}</a></li> +<!-- ENDIF --> +would result in the following markup:
+ ++ ++<ul class="nav"> <!-- written on first iteration --> + <li>second element</li> <!-- written on second iteration --> +</ul> <!-- written on third iteration --> +Just always remember that processing is taking place from up to down.
+