PHP Class Tale\Jade\Compiler

You can control the output-style via the options passed to the constructor. Different output types are possible (Currently, XML ,HTML and XHTML) The main entry point is the compile method The generated PHTML/PXML should be evaluated, the best method is a simple include of a generated file Usage example: use Tale\Jade\Compiler; $compiler = new Compiler(); $phtml = $compiler->compile($jadeInput); or $phtml = $compiler->compileFile($jadeFilePath); There are different approachs to handle the compiled PHTML. The best and most explaining one is saving the PHTML to a file and including it like this: file_put_contents('rendered.phtml', $phtml); Define some variables to pass to our template $variables = [ 'title' => 'My Page Title!', 'posts' => [] ]; Make sure the variables are accessible by the included template extract($variables); Compiler needs an $__args variables to pass arguments on to mixins $__args = $variables; Include the rendered PHTML directly include('rendered.phtml'); You may fetch the included content with ob_start() and ob_get_clean() and pass it on to anything, e.g. a cache handler or return it as an AJAX response.
Inheritance: use trait Tale\ConfigurableTrait
Afficher le fichier Open project: talesoft/tale-jade Class Usage Examples

Méthodes publiques

Méthode Description
__construct ( array $options = null, Parser $parser = null, Lexer $lexer = null ) Creates a new compiler instance.
addFilter ( string $name, callable $callback ) Adds a filter to the compiler.
addPath ( string $path ) Adds a path to the compiler.
compile ( string $input, string | null $path = null ) : mixed | string Compiles a Jade-string to PHTML.
compileFile ( string $path ) : mixed | string Compiles a file to PHTML.
getLexer ( ) : Lexer Returns the current lexer used.
getParser ( ) : Parser Returns the current parser used.
resolvePath ( string $path, array | string $extensions = null ) : string | false Resolves a path respecting the paths given in the options.

Méthodes protégées

Méthode Description
compileBlock ( Node $node ) : string Compiles a block node into PHTML.
compileCase ( Node $node ) : string Compiles a case-node into PHTML.
compileChildren ( array $nodes, boolean $indent = true, boolean $allowInline = false ) : string Compiles an array of nodes like they are children of some other node.
compileCode ( Node $node ) : string Compiles a code node and it's descending text nodes into a single PHP code block.
compileComment ( Node $node ) : string Compiles a comment-node based on if its rendered or not.
compileConditional ( Node $node ) : string Compiles a conditional, either if, elseif, else if or else into PHTML.
compileDo ( Node $node ) : string Compiles a do-instruction into PHTML.
compileDoctype ( Node $node ) : string Compiles a doctype Node to PHTML.
compileDocument ( Node $node ) : string Compiles a document Node to PHTML.
compileEach ( Node $node ) : string Compiles a each-instruction into a foreach-loop PHTML block.
compileElement ( Node $node ) : string Compiles an element-node containing a tag, attributes and assignments.
compileExpression ( Node $node ) : string Compiles an expression node and into a PHP expression.
compileFilter ( Node $node ) : string Compiles a filter-node into PHTML.
compileFor ( Node $node ) : string Compiles a for-loop into PHTML.
compileMixinCall ( Node $node ) : string Compiles a mixin call referencing the mixins in $_mixins.
compileMixins ( ) : string Compiles found mixins under each other into a single PHTML block.
compileNode ( Node $node ) : string Compiles any Node that has a matching method for its type.
compileScalar ( string $value, boolean | false $inCode = false ) : string Compiles and sanitizes a scalar value.
compileText ( Node $node ) : string Compiles a text-node to PTHML.
compileVariable ( Node $node ) : string Compiles a variable-node into PHTML.
compileWhen ( Node $node ) : string Compiles a when-node into PHTML.
compileWhile ( Node $node ) : string Compiles a while-loop into PHTML.
createCode ( string $code, string $prefix = '<?php ', string $suffix = '?>' ) : string Creates a PHP code expression.
createMarkupComment ( string $text ) : string Creates a XML-style comment ().
createPhpComment ( string $text ) : string Creates a PHP comment surrounded by PHP code tags.
createShortCode ( string $code ) : string Creates a -style PHP expression.
exportArray ( array $array, string $quoteStyle = ''' ) : string Exports an array to a PHP-string recursively.
exportScalar ( mixed $scalar, string $quoteStyle = ''', $inCode = false ) : string Exports a scalar value to the PHP representation.
handleBlock ( Node $node ) Stacks blocks into each other.
handleBlocks ( Node $node ) Collects all blocks and saves them into $_blocks.
handleImport ( Node $node ) Loads an imported file and merges the nodes with the current tree.
handleImports ( Node $node ) Collects all imports and handles them via ->handleImport.
handleMixin ( Node $node ) Pre-compiles a mixin into the $_mixin array.
handleMixins ( Node $node ) Finds all mixins and loops them through handleMixin.
indent ( integer $offset ) : string Returns indentation respecting the current level and the pretty-option.
interpolate ( string $string, boolean | false $inCode = false ) : string Interpolates a string value.
isHtml ( ) : boolean Checks if we're in HTML document mode.
isMode ( integer $mode ) : boolean Checks if the current document mode equals the mode passed.
isPretty ( ) : boolean Returns wether we're allowed to do pretty-printing or not currently
isScalar ( string $value ) : boolean Checks if a variables is scalar (or "not an expression").
isVariable ( string $value ) : boolean Checks if a value is a variables.
isXhtml ( ) : boolean Checks if we're in XHTML document mode.
isXml ( ) : boolean Checks if we're in XML document mode.
newLine ( ) : string Returns a new line character respecting the pretty-option.
throwException ( string $message, Node $relatedNode = null ) Throws a Compiler-Exception.

Method Details

__construct() public méthode

You can pass a modified parser or lexer. Notice that if you pass both, the lexer inside the parser will be used. Valid options are: pretty: Use indentation and new-lines or compile everything into a single line indent_style: The character that is used for indentation (Space by default) indent_width: The amount of characters to repeat for indentation (Default 2 for 2-space-indentation) self_closing_tags: he tags that don't need any closing in HTML-style languages self_repeating_attributes: The attributes that repeat their value to set them to true in HTML-style languages force_inlined_tags: The tags that don't allow any pretty-printing inside them Required for e.g. pre-tags doctypes: The different doctypes you can use via the "doctype"-directive [name => doctype-string] mode: Compile in HTML, XML or XHTML mode xhtml_modes: The mode strings that compile XHTML-style filters: The different filters you can use via the ":"-directive [name => callback] filter_map: The extension-to-filter-map for include-filters [extension => filter] escape_sequences: The escape-sequences that are possible in scalar strings compile_uncalled_mixins: Always compile all mixins or leave out those that aren't called? stand_alone: Allows the rendered files to be called without any requirements allow_imports: Set to false to disable imports for this compiler instance. Importing will throw an exception. Great for demo-pages defaultTag: The tag to default to for class/id/attribute-initiated elements (.abc, #abc, (abc)) quote_style: The quote-style in the markup (default: ") replace_mixins: Replaces mixins from top to bottom if they have the same name. Allows duplicated mixin names. echo_xml_doctype: Uses PHP's "echo" to for XML processing instructions This fixes problems with PHP's short open tags paths: The paths to resolve paths in. If none set, it will default to get_include_path() extensions: The extensions for Jade files (default: .jade and .jd) parser_options: The options for the parser if none given lexer_options: The options for the lexer if none given.
public __construct ( array $options = null, Parser $parser = null, Lexer $lexer = null )
$options array an array of options
$parser Parser an existing parser instance
$lexer Lexer an existing lexer instance

addFilter() public méthode

This filter can then be used inside jade with the : directive The callback should have the following signature: (\Tale\Jade\Parser\Node $node, $indent, $newLine) where $node is the filter-Node found, $indent is the current indentation respecting level and pretty-option and newLine is a new-line respecting the pretty-option It should return either a PHTML string or a Node-instance
public addFilter ( string $name, callable $callback )
$name string the name of the filter
$callback callable the filter handler callback

addPath() public méthode

Files will be loaded from this path (or other paths you added before)
public addPath ( string $path )
$path string the directory path

compile() public méthode

The result can then be evaluated, the best method is a simple PHP include Look at Renderer to get this done for you If you give it a path, the directory of that path will be used for relative includes.
public compile ( string $input, string | null $path = null ) : mixed | string
$input string the jade input string
$path string | null the path for relative includes
Résultat mixed | string a PHTML string containing HTML and PHP

compileBlock() protected méthode

A single block node without a name or mode will act as a wrapper for blocks inside mixins
protected compileBlock ( Node $node ) : string
$node Tale\Jade\Parser\Node the block node to compile
Résultat string The compiled PHTML

compileCase() protected méthode

This also checks if all sub-nodes of the case are when-children, nothing else is allowed compileCase interacts with compileWhen to skip ?>
protected compileCase ( Node $node ) : string
$node Tale\Jade\Parser\Node the case node to compile
Résultat string The compiled PHTML

compileChildren() protected méthode

if $indent is true, the level will be increased
protected compileChildren ( array $nodes, boolean $indent = true, boolean $allowInline = false ) : string
$nodes array
$indent boolean
$allowInline boolean
Résultat string

compileCode() protected méthode

Compiles a code node and it's descending text nodes into a single PHP code block.
protected compileCode ( Node $node ) : string
$node Tale\Jade\Parser\Node the code node to compile
Résultat string

compileComment() protected méthode

If it's rendered, it will be compiled as a HTML-comment, if not it will be compiled as a hidden PHP comment
protected compileComment ( Node $node ) : string
$node Tale\Jade\Parser\Node the comment-node to compile
Résultat string The compiled PHTML

compileConditional() protected méthode

Compiles a conditional, either if, elseif, else if or else into PHTML.
protected compileConditional ( Node $node ) : string
$node Tale\Jade\Parser\Node the conditional node to compile
Résultat string The compiled PHTML

compileDo() protected méthode

Compiles a do-instruction into PHTML.
protected compileDo ( Node $node ) : string
$node Tale\Jade\Parser\Node the do-node to compile
Résultat string The compiled PHTML

compileDoctype() protected méthode

Compiles a doctype Node to PHTML.
protected compileDoctype ( Node $node ) : string
$node Tale\Jade\Parser\Node the doctype-type node
Résultat string the compiled PHTML

compileDocument() protected méthode

Compiles a document Node to PHTML.
protected compileDocument ( Node $node ) : string
$node Tale\Jade\Parser\Node The document-type node
Résultat string the compiled PHTML

compileEach() protected méthode

the $ in the variables names are optional
protected compileEach ( Node $node ) : string
$node Tale\Jade\Parser\Node the each-node to compile
Résultat string The compiled PHTML

compileElement() protected méthode

Compiles an element-node containing a tag, attributes and assignments.
protected compileElement ( Node $node ) : string
$node Tale\Jade\Parser\Node the element node to compile
Résultat string The compiled PHTML

compileExpression() protected méthode

Compiles an expression node and into a PHP expression.
protected compileExpression ( Node $node ) : string
$node Tale\Jade\Parser\Node the expression node to compile
Résultat string

compileFile() public méthode

The given path will automatically passed as compile()'s $path argument The path should always be relative to the paths-option paths
public compileFile ( string $path ) : mixed | string
$path string the path to the jade file
Résultat mixed | string the compiled PHTML

compileFilter() protected méthode

The filters are drawn from the filters-option
protected compileFilter ( Node $node ) : string
$node Tale\Jade\Parser\Node the filter node to compile
Résultat string The compiled PHTML

compileFor() protected méthode

Compiles a for-loop into PHTML.
protected compileFor ( Node $node ) : string
$node Tale\Jade\Parser\Node the while-node to compile
Résultat string The compiled PHTML

compileMixinCall() protected méthode

Compiles a mixin call referencing the mixins in $_mixins.
protected compileMixinCall ( Node $node ) : string
$node Tale\Jade\Parser\Node the mixin call node to compile
Résultat string The compiled PHTML

compileMixins() protected méthode

Mixins will be anonymous functions inside a $__mixins array The mixins also pass the global $__args variables on (so that it _is_ global)
protected compileMixins ( ) : string
Résultat string The compile PHTML

compileNode() protected méthode

e.g. type: document, method: compileDocument type: element, method: compileElement The result will be PHTML
protected compileNode ( Node $node ) : string
$node Tale\Jade\Parser\Node the Node to compile
Résultat string the compiled PHTML

compileScalar() protected méthode

Compiles and sanitizes a scalar value.
protected compileScalar ( string $value, boolean | false $inCode = false ) : string
$value string the scalar value
$inCode boolean | false is this an attribute value or not
Résultat string

compileText() protected méthode

Texts get interpolated
protected compileText ( Node $node ) : string
$node Tale\Jade\Parser\Node the text-node to compile
Résultat string The compiled PHTML

compileVariable() protected méthode

Compiles a variable-node into PHTML.
protected compileVariable ( Node $node ) : string
$node Tale\Jade\Parser\Node the variable node to compile
Résultat string The compiled PHTML

compileWhen() protected méthode

This also checks, if the when node is defined on a case-parent When interacts with compileCase to skip the first ?>
protected compileWhen ( Node $node ) : string
$node Tale\Jade\Parser\Node the when-node to compile
Résultat string The compiled PHTML

compileWhile() protected méthode

Notice that if it has no children, we assume it's a do/while loop and don't print brackets
protected compileWhile ( Node $node ) : string
$node Tale\Jade\Parser\Node the while-node to compile
Résultat string The compiled PHTML

createCode() protected méthode

By default it will have -style
protected createCode ( string $code, string $prefix = '<?php ', string $suffix = '?>' ) : string
$code string the PHP code
$prefix string the PHP start tag
$suffix string the PHP end tag
Résultat string the PHP expression

createMarkupComment() protected méthode

Creates a XML-style comment ().
protected createMarkupComment ( string $text ) : string
$text string the text to wrap into a comment
Résultat string the compiled XML comment

createPhpComment() protected méthode

This creates a "hidden" comment thats still visible in the PHTML
protected createPhpComment ( string $text ) : string
$text string the text to wrap into a comment
Résultat string the compiled PHP comment

createShortCode() protected méthode

Creates a -style PHP expression.
protected createShortCode ( string $code ) : string
$code string the PHP expression to output
Résultat string The PHP expression

exportArray() protected méthode

This works similar to var_export in PHP, with the difference that it won't try to convert PHP-expression-style strings, e.g. variables, scalar values like null, false, true and expressions like arrays or function calls
protected exportArray ( array $array, string $quoteStyle = ''' ) : string
$array array the array to export
$quoteStyle string the quote-style used, ' by default
Résultat string the exported array

exportScalar() protected méthode

This also takes into account the PHP constants null, false and true, makes sure that numeric-values aren't string enclosed and utilizes interpolation for string values.
protected exportScalar ( mixed $scalar, string $quoteStyle = ''', $inCode = false ) : string
$scalar mixed the scalar value to export
$quoteStyle string the quote-style used, ' by default
Résultat string the exported scalar value

getLexer() public méthode

Returns the current lexer used.
public getLexer ( ) : Lexer
Résultat Lexer

getParser() public méthode

Returns the current parser used.
public getParser ( ) : Parser
Résultat Parser

handleBlock() protected méthode

The first block found is always the container, all other blocks either to append, replace or prepend to/the first block.
protected handleBlock ( Node $node )
$node Tale\Jade\Parser\Node the block node to handle

handleBlocks() protected méthode

After that it calls handleBlock on each $block
protected handleBlocks ( Node $node )
$node Tale\Jade\Parser\Node the node to search blocks in

handleImport() protected méthode

Loads an imported file and merges the nodes with the current tree.
protected handleImport ( Node $node )
$node Tale\Jade\Parser\Node the node to import

handleImports() protected méthode

Collects all imports and handles them via ->handleImport.
protected handleImports ( Node $node )
$node Tale\Jade\Parser\Node the root Node to search imports in

handleMixin() protected méthode

Only the block content of the mixin will be compiled, not the mixin itself The actual mixins get compiled in compileMixins
protected handleMixin ( Node $node )
$node Tale\Jade\Parser\Node the mixin node to compile

handleMixins() protected méthode

Duplicated mixins will throw an exception if the replaceMixins-options is false
protected handleMixins ( Node $node )
$node Tale\Jade\Parser\Node the node to search mixins in

indent() protected méthode

The $offset will be added to the current level
protected indent ( integer $offset ) : string
$offset integer an offset added to the level
Résultat string

interpolate() protected méthode

Interpolation is initialized with # (escaped) or ! (not escaped) After that use either } brackets for variables expressions or [] for Jade-expressions e.g. #{$someVariable} !{$someObj->someProperty} #[p This is some paragraph] If the second paragraph is true, the result will act like it is inside a string respecting the quoteStyle-option
protected interpolate ( string $string, boolean | false $inCode = false ) : string
$string string The string to interpolate
$inCode boolean | false Is this an attribute value or not
Résultat string the interpolated PHTML

isHtml() protected méthode

Checks if we're in HTML document mode.
protected isHtml ( ) : boolean
Résultat boolean

isMode() protected méthode

Take a look at the Compiler::MODE_* constants to see the possible modes
protected isMode ( integer $mode ) : boolean
$mode integer the mode to check against
Résultat boolean

isPretty() protected méthode

Returns wether we're allowed to do pretty-printing or not currently
protected isPretty ( ) : boolean
Résultat boolean

isScalar() protected méthode

These values don't get much special handling, they are mostly simple attributes values like type="button" or method='post' A scalar value is either a closed string containing only a-z, A-Z, 0-9, _ and -, e.g. Some-Static_Value or a quote-enclosed string that can contain anything except the quote style it used e.g. "Some Random String", 'This can" contain quotes"'
protected isScalar ( string $value ) : boolean
$value string the value to be checked
Résultat boolean

isVariable() protected méthode

A variables needs to start with $. After that only a-z, A-Z and _ can follow After that you can use any character of a-z, A-Z, 0-9, _, [, ], -, >, ' and " This will match all of the following: $__someVar $obj->someProperty $arr['someKey'] $arr[0] $obj->someArray['someKey'] etc.
protected isVariable ( string $value ) : boolean
$value string the value to be checked
Résultat boolean

isXhtml() protected méthode

Checks if we're in XHTML document mode.
protected isXhtml ( ) : boolean
Résultat boolean

isXml() protected méthode

Checks if we're in XML document mode.
protected isXml ( ) : boolean
Résultat boolean

newLine() protected méthode

Returns a new line character respecting the pretty-option.
protected newLine ( ) : string
Résultat string

resolvePath() public méthode

The final paths for resolving are put together as follows: when paths options not empty => Add paths of paths-option when paths option empty => Add paths of get_include_path() when current file stack not empty => Add directory of last file we were compiling We then look for a path with the given extension inside all paths we work on currently
public resolvePath ( string $path, array | string $extensions = null ) : string | false
$path string the relative path to resolve
$extensions array | string the extensions to resolve with
Résultat string | false the resolved full path or false, if not found

throwException() protected méthode

Throws a Compiler-Exception.
protected throwException ( string $message, Node $relatedNode = null )
$message string A meaningful exception message
$relatedNode Tale\Jade\Parser\Node The node the exception occured on