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
Exibir arquivo Open project: talesoft/tale-jade Class Usage Examples

Public Methods

Method 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.

Protected Methods

Method 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 method

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 method

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 method

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

compile() public method

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
return mixed | string a PHTML string containing HTML and PHP

compileBlock() protected method

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
return string The compiled PHTML

compileCase() protected method

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
return string The compiled PHTML

compileChildren() protected method

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
return string

compileCode() protected method

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
return string

compileComment() protected method

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
return string The compiled PHTML

compileConditional() protected method

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
return string The compiled PHTML

compileDo() protected method

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

compileDoctype() protected method

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

compileDocument() protected method

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

compileEach() protected method

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

compileElement() protected method

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
return string The compiled PHTML

compileExpression() protected method

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

compileFile() public method

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
return mixed | string the compiled PHTML

compileFilter() protected method

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

compileFor() protected method

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

compileMixinCall() protected method

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
return string The compiled PHTML

compileMixins() protected method

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
return string The compile PHTML

compileNode() protected method

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
return string the compiled PHTML

compileScalar() protected method

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
return string

compileText() protected method

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

compileVariable() protected method

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

compileWhen() protected method

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
return string The compiled PHTML

compileWhile() protected method

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
return string The compiled PHTML

createCode() protected method

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
return string the PHP expression

createMarkupComment() protected method

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

createPhpComment() protected method

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
return string the compiled PHP comment

createShortCode() protected method

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

exportArray() protected method

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
return string the exported array

exportScalar() protected method

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
return string the exported scalar value

getLexer() public method

Returns the current lexer used.
public getLexer ( ) : Lexer
return Lexer

getParser() public method

Returns the current parser used.
public getParser ( ) : Parser
return Parser

handleBlock() protected method

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 method

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 method

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 method

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 method

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 method

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 method

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

interpolate() protected method

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
return string the interpolated PHTML

isHtml() protected method

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

isMode() protected method

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
return boolean

isPretty() protected method

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

isScalar() protected method

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
return boolean

isVariable() protected method

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
return boolean

isXhtml() protected method

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

isXml() protected method

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

newLine() protected method

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

resolvePath() public method

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
return string | false the resolved full path or false, if not found

throwException() protected method

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