LS_Twig_Extension

Extends \Twig_Extension

This extension is needed to add complex functions to twig, needing specific process (like accessing config datas).

Most of the calls to internal functions don't need to be set here, but can be directly added to the internal config file. For example, the calls to encode, gT and eT don't need any extra parameters or process, so they are added as filters in the congif/internal.php:

'filters' => array( 'jencode' => 'CJSON::encode', 't' => 'eT', 'gT' => 'gT', ),

So you only add functions here when they need a specific process while called via Twig. To add an advanced function to twig:

  1. Add it here as a static public function eg: static public function foo($bar) { return procces($bar); }

  2. Add it in config/internal.php as a function, and as an allowed function in the sandbox eg: twigRenderer' => array( ... 'functions' => array( ... 'foo' => 'LS_Twig_Extension::foo', ...), ... 'sandboxConfig' => array( ... 'functions' => array('include', ..., 'foo') ),

Now you access this function in any twig file via: {{ foo($bar) }}, it will show the result of process($bar). If LS_Twig_Extension::foo() returns some HTML, by default the HTML will be escaped and shows as text. To get the pure HTML, just do: {{ foo($bar) | raw }}

package

Default

Methods

assetPublish

assetPublish(string $sRessource) 
static

Arguments

$sRessource

string

createUrl

createUrl( $url,  $params = array()) 
static

Arguments

$url

$params

get flat and ellipsize string

ellipsizeString(string $string, integer $maxlength, float $position = 1, string $ellipsis = '…') : string
static

Arguments

$string

string

to ellipsize

$maxlength

integer

of the final string

$position

float

of the ellipsis in string (between 0 and 1)

$ellipsis

string

string to shown in place of removed part

Response

string

flat and ellipsize text, for template compatibility

flatEllipsizeText(string $sString, boolean $bFlat = true, integer $iAbbreviated, string $sEllipsis = '...', integer $fPosition = 1) : string
static
deprecated

(4.0)

Arguments

$sString

string

:the string

$bFlat

boolean

: flattenText or not : completely flat (not like flattenText from common_helper)

$iAbbreviated

integer

: max string text (if true : allways flat), 0 or false : don't abbreviated

$sEllipsis

string

if abbreviated : the char to put at end (or middle)

$fPosition

integer

if abbreviated position to split (in % : 0 to 1)

Response

string

Get html text and remove whole not clean string

flatString(string $string, boolean $encode = false) : string
static

Arguments

$string

string

to flatten

$encode

boolean

html entities

Response

string

Retreive the question classes for a given question id Use in survey template question.twig file.

getAllQuestionClasses(integer $iQid) : string
static

TODO: we'd rather provide a oQuestion object to the twig view with a method getAllQuestion(). But for now, this public static function respect the old way of doing

deprecated

must be removed when allow to broke template. Since it was in 3.0 , it was in API (and question.twig are surely be updated).

Arguments

$iQid

integer

the question id

Response

string

the classes

getParam

getParam( $sName,  $sDefaultValue = null) 
static

Arguments

$sName

$sDefaultValue

getPosition

getPosition( $position) : string
static

Arguments

$position

Response

string

getPost

getPost( $sName,  $sDefaultValue = null) 
static

Arguments

$sName

$sDefaultValue

getQuery

getQuery( $sName,  $sDefaultValue = null) 
static

Arguments

$sName

$sDefaultValue

getTemplateForRessource

getTemplateForRessource(string $sRessource) 
static

Arguments

$sRessource

string

image

image( $sImagePath,  $alt = '',  $htmlOptions = array()) : string
static
var

string the image path relative to the template root

string the alternative text display

array additional HTML attribute

Arguments

$sImagePath

$alt

$htmlOptions

Response

string

imageSrc

imageSrc( $sImagePath,  $default = './files/pattern.png') 
static

Arguments

$sImagePath

$default

Convert a json object to a PHP array (so no troubles with object method in sandbox)

json_decode( $json) 
static

Arguments

$json

listCoreScripts

listCoreScripts() 
static

listScriptFiles

listScriptFiles() 
static

Process any string with current page

processString( $string, boolean $static = false, integer $numRecursionLevels = 3, array $aReplacement = array()) : string
static

Arguments

$string

$static

boolean

return static string (or not)

$numRecursionLevels

integer

recursion (max) level to do

$aReplacement

array

replacement out of EM

Response

string

registerCssFile

registerCssFile( $path) 
static

Arguments

$path

Publish a script file from general script directory, using or not the asset manager (depending on configuration) In any twig file, you can register a general script file doing: {{ registerGeneralScript($sGeneralScriptFileName) }}

registerGeneralScript(string $sGeneralScriptFileName, string $position = null, array $htmlOptions = array()) 
static

Arguments

$sGeneralScriptFileName

string

name of the script file to publish in general script directory (it should contains the subdirectories)

$position

string

$htmlOptions

array

registerPackage

registerPackage( $name) 
static

Arguments

$name

Publish a css file from public style directory, using or not the asset manager (depending on configuration) In any twig file, you can register a public css file doing: {{ registerPublicCssFile($sPublicCssFileName) }}

registerPublicCssFile(string $sPublicCssFileName) 
static

Arguments

$sPublicCssFileName

string

name of the CSS file to publish in public style directory

Publish a script In any twig file, you can register a script doing: {{ registerScript($sId, $sScript) }}

registerScript( $id,  $script,  $position = null, array $htmlOptions = array()) 
static

Arguments

$id

$script

$position

$htmlOptions

array

registerScriptFile

registerScriptFile( $path,  $position = null) 
static

Arguments

$path

$position

Publish a css file from template directory, using or not the asset manager (depending on configuration) In any twig file, you can register a template css file doing: {{ registerTemplateCssFile($sTemplateCssFileName) }}

registerTemplateCssFile(string $sTemplateCssFileName) 
static

Arguments

$sTemplateCssFileName

string

name of the CSS file to publish in template directory (it should contains the subdirectories)

Publish a script file from template directory, using or not the asset manager (depending on configuration) In any twig file, you can register a template script file doing: {{ registerTemplateScript($sTemplateScriptFileName) }}

registerTemplateScript(string $sTemplateScriptFileName, string $position = null, array $htmlOptions = array()) 
static

Arguments

$sTemplateScriptFileName

string

name of the script file to publish in general script directory (it should contains the subdirectories)

$position

string

$htmlOptions

array

renderCaptcha

renderCaptcha() 
static

unregisterPackage

unregisterPackage(string $name) 
static

Arguments

$name

string

unregisterScriptFile

unregisterScriptFile(string $name) 
static

Arguments

$name

string

Unregister all packages/script files for AJAX rendering

unregisterScriptForAjax() 
static