PHP Class Haanga_Compiler

Show file Open project: crodas/haanga Class Usage Examples

Public Properties

Property Type Description
$safes
$var_is_safe Flag the current variable as safe. This means that escape won't be called if autoescape is activated (which is activated by default)

Protected Properties

Property Type Description
$append
$autoescape * compiler options
$block_var properties {{{
$blocks
$check_function
$context Context at compile time
$debug Debug file
$dot_as_object
$echo_concat
$enable_load
$file
$forid
$forloop
$generator
$global_context
$if_empty
$in_block number of blocks :-)
$is_exec_enabled
$line
$name
$ob_start output buffers :-)
$prepend_op
$strip_whitespace
$sub_template
$var_alias Table which contains all variables aliases defined in the template

Public Methods

Method Description
Error ( $err ) Throw an exception and appends information about the template (the path and the last processed line).
__construct ( ) }}}
compile ( $code, $name = NULL, $file = NULL ) Compile ($code, $name=NULL) {{{
compile_file ( string $file, boolean $safe = FALSE, $context = [] ) : Generated Compile a file
do_filtering ( $name, $args )
do_print ( Haanga_AST $code, $stmt ) Print {{{
expr_call_base_template ( ) Generate code to call base template
generate_op_alias ( $details, &$body )
generate_op_autoescape ( $details, &$body ) autoescape ON|OFF {{{
generate_op_custom_tag ( $details, &$body ) Generate needed code for custom tags (tags that aren't handled by the compiler).
generate_op_filter ( $details, &$body )
generate_op_set ( $details, &$body ) }}}
generate_op_spacefull ( $details, &$body ) {% spacefull %} Set to OFF strip_whitespace for a block (the compiler option) {{{
generate_variable_name ( $variable, $special = true ) Get variable name {{{
getOpCodes ( string $code, string $file ) : Haanga_AST Compile the $code and return the "opcodes" (the Abstract syntax tree).
getOption ( $option ) getOption($option) {{{
getScopeVariable ( $part = NULL, $string = FALSE )
get_base_template ( array $base ) : string Handle {% base "" %} definition. By default only static (string) are supported, but this can be overrided on subclasses.
get_context ( $variable )
get_custom_filter ( $name ) Custom Filters {{{
get_custom_tag ( $name ) Custom Tags {{{
get_filtered_var ( array $variable, &$varname, boolean $accept_string = NULL ) : expr This method handles all the filtered variable (piped_list(X)'s output in the parser.
get_function_name ( $name ) get_function_name(string $name) {{{
get_template_name ( ) get_template_name() {{{
isMethod ( $varname, &$expr ) }}}
is_safe ( $name )
is_var_filter ( $cmd ) is_expr methods {{{
main_cli ( ) * }}}
ob_start ( &$body ) Start a new buffering
reset ( ) reset() {{{
setDebug ( $file ) setDebug($file) {{{
setOption ( $option, $value ) : void Set Compiler option.
set_context ( $varname, $value ) Variables context
set_safe ( $name ) * variable safety {{{
set_template_name ( $path ) Set template name {{{
set_unsafe ( $name )
var_is_object ( array $variable, $default = NULL )

Protected Methods

Method Description
check_expr ( &$expr ) Check the current expr {{{
compile_required_template ( $file ) Override template {{{
generate_op_base ( ) {% base "foo.html" %} {{{
generate_op_block ( $details, &$body ) {% block 'name' %} .
generate_op_code ( $parsed, &$body ) Main Loop {{{
generate_op_comment ( $details, &$body ) {# something #} {{{
generate_op_html ( $details, &$body ) Handle HTML code {{{
generate_op_if ( $details, &$body ) {% if %} HTML {% else %} TWO {% endif $} {{{
generate_op_ifchanged ( $details, &$body ) ifchanged [
generate_op_ifequal ( $details, &$body ) ifequal|ifnot equal .
generate_op_include ( $details, &$body ) include "file.html" | include {{{
generate_op_loop ( $details, &$body ) for [,] in {{{
generate_op_print_var ( $details, &$body ) Generate code to print a variable with its filters, if there is any.
generate_op_regroup ( $details, &$body ) regroup by as {{{

Method Details

Error() public method

Throw an exception and appends information about the template (the path and the last processed line).
public Error ( $err )

__construct() public method

}}}
public __construct ( )

check_expr() protected method

Check the current expr {{{
protected check_expr ( &$expr )

compile() final public method

Compile ($code, $name=NULL) {{{
final public compile ( $code, $name = NULL, $file = NULL )

compile_file() final public method

@param string $file File path
final public compile_file ( string $file, boolean $safe = FALSE, $context = [] ) : Generated
$file string
$safe boolean Whether or not add check if the function is already defined @return Generated PHP code
return Generated

compile_required_template() protected method

Override template {{{
protected compile_required_template ( $file )

do_filtering() public method

public do_filtering ( $name, $args )

do_print() public method

Print {{{
public do_print ( Haanga_AST $code, $stmt )
$code Haanga_AST

expr_call_base_template() public method

Generate code to call base template

generate_op_alias() public method

public generate_op_alias ( $details, &$body )

generate_op_autoescape() public method

autoescape ON|OFF {{{
public generate_op_autoescape ( $details, &$body )

generate_op_base() protected method

{% base "foo.html" %} {{{
protected generate_op_base ( )

generate_op_block() protected method

.. {% endblock %} {{{
protected generate_op_block ( $details, &$body )

generate_op_code() protected method

Main Loop {{{
protected generate_op_code ( $parsed, &$body )

generate_op_comment() protected method

{# something #} {{{
protected generate_op_comment ( $details, &$body )

generate_op_custom_tag() public method

Generate needed code for custom tags (tags that aren't handled by the compiler).
public generate_op_custom_tag ( $details, &$body )

generate_op_filter() public method

public generate_op_filter ( $details, &$body )

generate_op_html() protected method

Handle HTML code {{{
protected generate_op_html ( $details, &$body )

generate_op_if() protected method

{% if %} HTML {% else %} TWO {% endif $} {{{
protected generate_op_if ( $details, &$body )

generate_op_ifchanged() protected method

ifchanged [
protected generate_op_ifchanged ( $details, &$body )

generate_op_ifequal() protected method

.. else ... {{{
protected generate_op_ifequal ( $details, &$body )

generate_op_include() protected method

include "file.html" | include {{{
protected generate_op_include ( $details, &$body )

generate_op_loop() protected method

for [,] in {{{
protected generate_op_loop ( $details, &$body )

generate_op_print_var() protected method

All variable (except those flagged as |safe) are automatically escaped if autoescape is "on".
protected generate_op_print_var ( $details, &$body )

generate_op_regroup() protected method

regroup by as {{{
protected generate_op_regroup ( $details, &$body )

generate_op_set() public method

}}}
public generate_op_set ( $details, &$body )

generate_op_spacefull() public method

{% spacefull %} Set to OFF strip_whitespace for a block (the compiler option) {{{
public generate_op_spacefull ( $details, &$body )

generate_variable_name() public method

Get variable name {{{
public generate_variable_name ( $variable, $special = true )

getOpCodes() public method

@param string $code Template content
public getOpCodes ( string $code, string $file ) : Haanga_AST
$code string
$file string File path (used for erro reporting) @return Haanga_AST
return Haanga_AST

getOption() public static method

getOption($option) {{{
public static getOption ( $option )

getScopeVariable() public method

public getScopeVariable ( $part = NULL, $string = FALSE )

get_base_template() public method

This method load the base class, compile it and return the generated code.
public get_base_template ( array $base ) : string
$base array Base structure @return string Generated source code
return string

get_context() public method

public get_context ( $variable )

get_custom_filter() public method

Custom Filters {{{
public get_custom_filter ( $name )

get_custom_tag() public method

Custom Tags {{{
public get_custom_tag ( $name )

get_filtered_var() public method

@param array $variable (Output of piped_list(B) (parser))
public get_filtered_var ( array $variable, &$varname, boolean $accept_string = NULL ) : expr
$variable array
$accept_string boolean
return expr

get_function_name() public method

get_function_name(string $name) {{{
public get_function_name ( $name )

get_template_name() final public method

get_template_name() {{{
final public get_template_name ( )

isMethod() public method

}}}
public isMethod ( $varname, &$expr )

is_safe() public method

public is_safe ( $name )

is_var_filter() public method

is_expr methods {{{
public is_var_filter ( $cmd )

main_cli() final static public method

* }}}
final static public main_cli ( )

ob_start() public method

Start a new buffering
public ob_start ( &$body )

reset() public method

reset() {{{
public reset ( )

setDebug() public method

setDebug($file) {{{
public setDebug ( $file )

setOption() public static method

@return void
public static setOption ( $option, $value ) : void
return void

set_context() public method

These two functions are useful to detect if a variable separated by dot (foo.bar) is an array or object. To avoid overhead we decide it at compile time, rather than ask over and over at rendering time. foo.bar: + If foo exists at compile time, and it is an array, it would be foo['bar'] otherwise it'd be foo->bar. + If foo don't exists at compile time, it would be foo->bar if the compiler option dot_as_object is TRUE (by default) otherwise it'd be foo['bar']
Author: crodas @author gallir (ideas)
public set_context ( $varname, $value )

set_safe() public method

* variable safety {{{
public set_safe ( $name )

set_template_name() public method

Set template name {{{
public set_template_name ( $path )

set_unsafe() public method

public set_unsafe ( $name )

var_is_object() public method

public var_is_object ( array $variable, $default = NULL )
$variable array

Property Details

$append protected property

protected $append

$autoescape protected static property

* compiler options
protected static $autoescape

$block_var protected static property

properties {{{
protected static $block_var

$blocks protected property

protected $blocks

$check_function protected property

protected $check_function

$context protected property

Context at compile time
protected $context

$debug protected property

Debug file
protected $debug

$dot_as_object protected static property

protected static $dot_as_object

$echo_concat protected static property

protected static $echo_concat

$enable_load protected static property

protected static $enable_load

$file protected property

protected $file

$forid protected property

protected $forid

$forloop protected property

protected $forloop

$generator protected property

protected $generator

$global_context protected static property

protected static $global_context

$if_empty protected static property

protected static $if_empty

$in_block protected property

number of blocks :-)
protected $in_block

$is_exec_enabled protected static property

protected static $is_exec_enabled

$line protected property

protected $line

$name protected property

protected $name

$ob_start protected property

output buffers :-)
protected $ob_start

$prepend_op protected property

protected $prepend_op

$safes public property

public $safes

$strip_whitespace protected static property

protected static $strip_whitespace

$sub_template protected property

protected $sub_template

$var_alias protected property

Table which contains all variables aliases defined in the template
protected $var_alias

$var_is_safe public property

Flag the current variable as safe. This means that escape won't be called if autoescape is activated (which is activated by default)
public $var_is_safe