HTML Logo by World Wide Web Consortium (www.w3.org). Click to learn more about our commitment to accessibility and standards.

ocPortal Developer's Guide: Tempcode

» Return to Contents



Tempcode objects are attached together as a tree structure as ocPortal progresses through generating a page, and are eventually evaluated to a string. The advantage of our tree structure approach over linear succession approaches are very significant, and allow us to have much cleaner and more intuitive code and templates that 'embed' each other: you can consider a form element in it's own right, for example, without having to consider the context of a form elements input row, or a form input rows input form, or a input rows input page; HTML tags start and close in the same template file, making it a lot more readable and easy to write.

sources/tempcode.php

Global_functions_tempcode.php

Function summary

void init__tempcode ()
string static_evaluate_tempcode (tempcode ob)
string php_addslashes_twice (string in)
string fast_uniqid ()
string output_tempcode_parameter (mixed var, ID_TEXT parameter, ID_TEXT template_name)
tempcode build_closure_tempcode (integer type, ID_TEXT name, ?array parameters, ?array escaping)
tempcode symbol_tempcode (ID_TEXT symbol, ?array parameters, ?array escape)
tempcode directive_tempcode (ID_TEXT directive, tempcode content, ?array parameters)
string closure_while_loop (array args, array control_function, array main_function)
string closure_eval (string code, array parameters)
string closure_loop (array param, array args, array main_function)
tempcode make_string_tempcode (string string)
string apply_tempcode_escaping (array escaped, string value)
string apply_tempcode_escaping_inline (array escaped, string value)
tempcode do_lang_tempcode (ID_TEXT lang_string, ?mixed token1, ?mixed token2, ?mixed token3)
tempcode do_template (ID_TEXT codename, ?array parameters, ?LANGUAGE_NAME lang, boolean light_error, ?ID_TEXT fallback, string suffix, string type, ?ID_TEXT theme)
void handle_symbol_preprocessing (array bit, array children)

void init__tempcode()

Standard code module initialisation function.

Parameters…

(No return value)


Return to the function index for this class Expand: View the source code to this function

string static_evaluate_tempcode(tempcode ob)

Simple function to evaluate some Tempcode. Very rarely to be used, only if you can't call a method (e.g. you are copying direct into an array, such as in block cacheing).

Parameters…

Name ob
Description Tempcode object
Type tempcode

Returns…

Description Evaluated string
Type string

Return to the function index for this class Expand: View the source code to this function

string php_addslashes_twice(string in)

Escape a string to fit within PHP double quotes TWICE. Needed sometimes when generating code. This function exists for performance reasons.

Parameters…

Name in
Description String in
Type string

Returns…

Description Resultant string
Type string

Return to the function index for this class Expand: View the source code to this function

string fast_uniqid()

The PHP uniqid function is slow on some PHP versions. This version is much faster.

Parameters…

Returns…

Description Unique Identifier
Type string

Return to the function index for this class Expand: View the source code to this function

string output_tempcode_parameter(mixed var, ID_TEXT parameter, ID_TEXT template_name)

Get a string (natural for Tempcode's stream-based processing-model) representation of a bound Tempcode variable

Parameters…

Name var
Description Variable (or NULL if not set)
Type mixed

Name parameter
Description The name of the parameter
Type ID_TEXT

Name template_name
Description The name of the template
Type ID_TEXT

Returns…

Description Value
Type string

Return to the function index for this class Expand: View the source code to this function

tempcode build_closure_tempcode(integer type, ID_TEXT name, ?array parameters, ?array escaping)

Build a conventional tempcode object

Parameters…

Name type
Description The type of symbol this is (TC_SYMBOL, TC_LANGUAGE_REFERENCE)
Type integer
Values restricted to 0 2

Name name
Description The name of the symbol
Type ID_TEXT

Name parameters
Description Parameters to the symbol (NULL: none). In same format as expected by ecv.
Type ?array

Name escaping
Description Escaping for the symbol (NULL: none)
Default value
Type ?array

Returns…

Description Tempcode object.
Type tempcode

Return to the function index for this class Expand: View the source code to this function

tempcode symbol_tempcode(ID_TEXT symbol, ?array parameters, ?array escape)

This will create a new tempcode object that is containing a single specifed symbol

Parameters…

Name symbol
Description The ID of the symbol to use
Type ID_TEXT

Name parameters
Description Symbol parameters (NULL: none)
Default value
Type ?array

Name escape
Description Escaping (NULL: none)
Default value
Type ?array

Returns…

Description A symbol tempcode object
Type tempcode

Return to the function index for this class Expand: View the source code to this function

tempcode directive_tempcode(ID_TEXT directive, tempcode content, ?array parameters)

This will create a new tempcode object that is containing a single specifed directive

Parameters…

Name directive
Description The ID of the directive to use
Type ID_TEXT

Name content
Description The contents
Type tempcode

Name parameters
Description Directive parameters (NULL: none)
Default value
Type ?array

Returns…

Description A directive tempcode object
Type tempcode

Return to the function index for this class Expand: View the source code to this function

string closure_while_loop(array args, array control_function, array main_function)

Perform a simple loop, that can be inlined in an expression.

Parameters…

Name args
Description The template bound parameters
Type array

Name control_function
Description The loop control function
Type array

Name main_function
Description The loop execution function
Type array

Returns…

Description Result
Type string

Return to the function index for this class Expand: View the source code to this function

string closure_eval(string code, array parameters)

Evaluate some PHP code to put the result into an expression (code is allowed to have side effects).

Parameters…

Name code
Description The code
Type string

Name parameters
Description Template parameters
Type array

Returns…

Description Result
Type string

Return to the function index for this class Expand: View the source code to this function

string closure_loop(array param, array args, array main_function)

Perform a simple loop, that can be inlined in an expression.

Parameters…

Name param
Description The template bound parameters
Type array

Name args
Description The loop directive parameters
Type array

Name main_function
Description The loop execution function
Type array

Returns…

Description Result
Type string

Return to the function index for this class Expand: View the source code to this function

tempcode make_string_tempcode(string string)

Convert a string to tempcode.

Parameters…

Name string
Description String
Type string

Returns…

Description Tempcode
Type tempcode

Return to the function index for this class Expand: View the source code to this function

string apply_tempcode_escaping(array escaped, string value)

Apply whatever escaping is requested to the given value.

Parameters…

Name escaped
Description A list of escaping to do
Type array

Name value
Description The string to apply the escapings to
Type string

Returns…

Description Output string (you do not need to collect this, as $value is pass-by-reference – but this is useful for chaining)
Type string

Return to the function index for this class Expand: View the source code to this function

string apply_tempcode_escaping_inline(array escaped, string value)

Apply whatever escaping is requested to the given value.

Parameters…

Name escaped
Description A list of escaping to do
Type array

Name value
Description The string to apply the escapings to
Type string

Returns…

Description Output string
Type string

Return to the function index for this class Expand: View the source code to this function

tempcode do_lang_tempcode(ID_TEXT lang_string, ?mixed token1, ?mixed token2, ?mixed token3)

This will create a new tempcode object that is containing a single specifed language code

Parameters…

Name lang_string
Description The ID of the language string to use
Type ID_TEXT

Name token1
Description The first token [string or tempcode] (replaces {1}) (NULL: none)
Default value
Type ?mixed

Name token2
Description The second token [string or tempcode] (replaces {2}) (NULL: none)
Default value
Type ?mixed

Name token3
Description The third token (replaces {3}). May be an array of [of string], to allow any number of additional args (NULL: none)
Default value
Type ?mixed

Returns…

Description A language tempcode object
Type tempcode

Return to the function index for this class Expand: View the source code to this function

tempcode do_template(ID_TEXT codename, ?array parameters, ?LANGUAGE_NAME lang, boolean light_error, ?ID_TEXT fallback, string suffix, string type, ?ID_TEXT theme)

Get a tempcoded version of a normal XHTML template. It is perhaps the most common ocPortal function to load up templates using do_template, and then attach them together either as parameters to each other, or via the tempcode attach method.

Parameters…

Name codename
Description The codename of the template being loaded
Type ID_TEXT

Name parameters
Description A map of parameters for the template (key to value) (NULL: no parameters)
Default value
Type ?array

Name lang
Description The language to load the template in (templates can embed language references) (NULL: users own language)
Default value
Type ?LANGUAGE_NAME

Name light_error
Description Whether to not produce a stack dump if the template is missing
Default value boolean-false
Type boolean

Name fallback
Description Alternate template to use if the primary one does not exist (NULL: none)
Default value
Type ?ID_TEXT

Name suffix
Description File type suffix of template file (e.g. .tpl)
Default value .tpl
Type string

Name type
Description Subdirectory type to look in
Default value templates
Type string
Values restricted to templates css

Name theme
Description Theme to use (NULL: current theme)
Default value
Type ?ID_TEXT

Returns…

Description The tempcode for this template
Type tempcode

Return to the function index for this class Expand: View the source code to this function

void handle_symbol_preprocessing(array bit, array children)

Certain symbols need preprocessing, before the output stream is made.

Parameters…

Name bit
Description Symbol details
Type array

Name children
Description Where we store children stuff
Type array

(No return value)


Return to the function index for this class Expand: View the source code to this function

ocp_tempcode

Function summary

void ocp_tempcode (?array details)
array __sleep ()
void decache ()
void handle_symbol_preprocessing ()
boolean parameterless (integer at)
void parse_from (string code, integer pos, integer len)
void attach (mixed attach, boolean avoid_child_merge)
boolean is_definitely_empty ()
boolean is_empty ()
boolean is_really_empty ()
string to_assembly ()
boolean from_assembly_executed (PATH file, array forced_reload_details)
void _mark_all_as_escaped ()
boolean from_assembly (string raw_data, boolean allow_failure)
void singular_bind (string parameter, tempcode value)
tempcode bind (array parameters, ID_TEXT codename)
string evaluate_echo (?LANGUAGE_NAME current_lang, mixed _escape)
string evaluate (?LANGUAGE_NAME current_lang, mixed _escape, ?integer up_to)

void ocp_tempcode(?array details)

Constructor of tempcode

Parameters…

Name details
Description Pair: Code to preexecute, Initialisation seq-parts (NULL: start as empty)
Default value
Type ?array

(No return value)


Return to the function index for this class Expand: View the source code to this function

array __sleep()

PHP magic function to handle serialisation.

Parameters…

Returns…

Description What is to be serialised
Type array

Return to the function index for this class Expand: View the source code to this function

void decache()

Decache the object.

Parameters…

(No return value)


Return to the function index for this class Expand: View the source code to this function

void handle_symbol_preprocessing()

Scan this Tempcode for anything that needs to be symbol-preprocessed

Parameters…

(No return value)


Return to the function index for this class Expand: View the source code to this function

boolean parameterless(integer at)

Find whether a variable within this Tempcode is parameterless.

Parameters…

Name at
Description Offset to the variable
Type integer

Returns…

Description Whether it is parameterless
Type boolean

Return to the function index for this class Expand: View the source code to this function

void parse_from(string code, integer pos, integer len)

Parse a single symbol from an input stream and append it.

Parameters…

Name code
Description Code string (input stream)
Type string

Name pos
Description Start position of input string
Type integer

Name len
Description End position of input string
Type integer

(No return value)


Return to the function index for this class Expand: View the source code to this function

void attach(mixed attach, boolean avoid_child_merge)

Attach the specified tempcode to the right of the current tempcode object.

Parameters…

Name attach
Description The tempcode/string to attach
Type mixed

Name avoid_child_merge
Description If we've already merged the children from what we're attaching into the child tree (at bind stage)
Default value boolean-false
Type boolean

(No return value)


Return to the function index for this class Expand: View the source code to this function

boolean is_definitely_empty()

Find whether the current tempcode object is definitely blank, by doing a very quick check.

Parameters…

Returns…

Description Whether the tempcode object is empty
Type boolean

Return to the function index for this class Expand: View the source code to this function

boolean is_empty()

Find whether the current tempcode object is blank or not.

Parameters…

Returns…

Description Whether the tempcode object is empty
Type boolean

Return to the function index for this class Expand: View the source code to this function

boolean is_really_empty()

Tests to see if something would evaluate to blank or not

Parameters…

Returns…

Description Whether it is really empty
Type boolean

Return to the function index for this class Expand: View the source code to this function

string to_assembly()

Assemble the current tempcode object into a single serialised (compiled) tempcode storage representation (parameters and certain symbols and not evaluated). The output of the function is language-tied.

Parameters…

Returns…

Description The assembly result
Type string

Return to the function index for this class Expand: View the source code to this function

boolean from_assembly_executed(PATH file, array forced_reload_details)

The opposite of to_assembly - it decodes a tempcode storage representation and turns it into a proper tempcode object. This version handles the result of evaled code.

Parameters…

Name file
Description The file to load
Type PATH

Name forced_reload_details
Description List of parameters for a forced reload if required
Type array

Returns…

Description Success status (it can fail, if the compiled cache file is corrupt)
Type boolean

Return to the function index for this class Expand: View the source code to this function

void _mark_all_as_escaped()

Recursively mark all parameters in this Tempcode as escaped. This is needed when loading from cache, as escape tainting data would have been lost.

Parameters…

(No return value)


Return to the function index for this class Expand: View the source code to this function

boolean from_assembly(string raw_data, boolean allow_failure)

The opposite of to_assembly - it decodes a tempcode storage representation and turns it into a proper tempcode object.

Parameters…

Name raw_data
Description The assembled tempcode
Type string

Name allow_failure
Description Return error code on failure, rather than exiting
Default value boolean-false
Type boolean

Returns…

Description Success status (it can fail, if the compiled cache file is corrupt)
Type boolean

Return to the function index for this class Expand: View the source code to this function

void singular_bind(string parameter, tempcode value)

Replace the named parameter with a specific value. Hardly used, but still important. Note that this will bind to all kinds of things that might not normally take named parameters, like symbols; this should not cause problems though.

Parameters…

Name parameter
Description Named parameter
Type string

Name value
Description Specific value
Type tempcode

(No return value)


Return to the function index for this class Expand: View the source code to this function

tempcode bind(array parameters, ID_TEXT codename)

Bind the parameter bits, or recursively bind children (doesn't change self, returns a bound tempcode object)

Parameters…

Name parameters
Description Map of parameters to bind parameter bits to
Type array

Name codename
Description The codename of the template this tempcode is from
Type ID_TEXT

Returns…

Description The new bound tempcode object
Type tempcode

Return to the function index for this class Expand: View the source code to this function

string evaluate_echo(?LANGUAGE_NAME current_lang, mixed _escape)

Parse the current tempcode object, then echo it to the browser.

Parameters…

Name current_lang
Description The language to evaluate with (NULL: current users language)
Default value
Type ?LANGUAGE_NAME

Name _escape
Description Whether to escape the tempcode object (children may be recursively escaped regardless if those children/parents are marked to be)
Default value boolean-false
Type mixed

Returns…

Description Blank string. Allows chaining within echo statements
Type string

Return to the function index for this class Expand: View the source code to this function

string evaluate(?LANGUAGE_NAME current_lang, mixed _escape, ?integer up_to)

Parses the current tempcode object, then return the parsed string

Parameters…

Name current_lang
Description The language to evaluate with (NULL: current user's language)
Default value
Type ?LANGUAGE_NAME

Name _escape
Description Whether to escape the tempcode object (children may be recursively escaped regardless if those children/parents are marked to be)
Default value boolean-false
Type mixed

Name up_to
Description Evaluate at least this much (NULL: evaluate all)
Default value
Type ?integer

Returns…

Description The evaluated thing. Voila, it's all over!
Type string

Return to the function index for this class Expand: View the source code to this function

sources/symbols.php

Global_functions_symbols.php

Function summary

void init__symbols ()
mixed evaluate_conventional_variable (LANGUAGE_NAME lang, array escaped, integer type, ID_TEXT name, array param)
mixed ecv (LANGUAGE_NAME lang, array escaped, integer type, ID_TEXT name, array param)
string symbol_truncator (array param, string type, ?mixed tooltip_if_truncated)
string keep_symbol (array param)

void init__symbols()

Standard code module initialisation function.

Parameters…

(No return value)


Return to the function index for this class Expand: View the source code to this function

mixed evaluate_conventional_variable(LANGUAGE_NAME lang, array escaped, integer type, ID_TEXT name, array param)

Evaluate a conventional tempcode variable, handling escaping. Long named one, for compatibility (we've moved to short one for shorter compiled Tempcode). DEPRECATED.

Parameters…

Name lang
Description The language to evaluate this symbol in (some symbols refer to language elements)
Type LANGUAGE_NAME

Name escaped
Description Array of escaping operations
Type array

Name type
Description The type of symbol this is (TC_SYMBOL, TC_LANGUAGE_REFERENCE)
Type integer
Values restricted to 0 2

Name name
Description The name of the symbol
Type ID_TEXT

Name param
Description Parameters to the symbol. For all but directive it is an array of strings. For directives it is an array of Tempcode objects. Actually there may be template-style parameters in here, as an influence of singular_bind and these may be Tempcode, but we ignore them.
Type array

Returns…

Description The result. Either tempcode, or a string.
Type mixed

Return to the function index for this class Expand: View the source code to this function

mixed ecv(LANGUAGE_NAME lang, array escaped, integer type, ID_TEXT name, array param)

Evaluate a conventional tempcode variable, handling escaping

Parameters…

Name lang
Description The language to evaluate this symbol in (some symbols refer to language elements)
Type LANGUAGE_NAME

Name escaped
Description Array of escaping operations
Type array

Name type
Description The type of symbol this is (TC_SYMBOL, TC_LANGUAGE_REFERENCE)
Type integer
Values restricted to 0 2

Name name
Description The name of the symbol
Type ID_TEXT

Name param
Description Parameters to the symbol. For all but directive it is an array of strings. For directives it is an array of Tempcode objects. Actually there may be template-style parameters in here, as an influence of singular_bind and these may be Tempcode, but we ignore them.
Type array

Returns…

Description The result. Either tempcode, or a string.
Type mixed

Return to the function index for this class Expand: View the source code to this function

string symbol_truncator(array param, string type, ?mixed tooltip_if_truncated)

Handle truncation symbols in all their complexity

Parameters…

Name param
Description Parameters passed to the symbol (0=text, 1=amount, 2=tooltip?, 3=is_html?, 4=use as grammatical length rather than HTML byte length, 5=fractional-deviation-tolerance for grammar-preservation)
Type array

Name type
Description The type of truncation to do
Type string
Values restricted to left right spread

Name tooltip_if_truncated
Description Tooltip to add on, but only if we end up creating our own tooltip (NULL: none)
Default value
Type ?mixed

Returns…

Description The result.
Type string

Return to the function index for this class Expand: View the source code to this function

string keep_symbol(array param)

String to tack onto URL to keep 'keep_' parameters

Parameters…

Name param
Description Parameters passed to the symbol (0=whether this starts off the query string, 1=force session append even if it's also available a session cookie e.g. when put into download manager)
Type array

Returns…

Description The result.
Type string

Return to the function index for this class Expand: View the source code to this function