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.
显示文件
Open project: talesoft/tale-jade
Class Usage Examples
Method | Description | |
---|---|---|
__construct ( array $options = 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 ( ) : |
Returns the current lexer used. | |
getParser ( ) : |
Returns the current parser used. | |
resolvePath ( string $path, array | string $extensions = null ) : string | false | Resolves a path respecting the paths given in the options. |
Method | Description | |
---|---|---|
compileBlock ( |
Compiles a block node into PHTML. | |
compileCase ( |
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 ( |
Compiles a code node and it's descending text nodes into a single PHP code block. | |
compileComment ( |
Compiles a comment-node based on if its rendered or not. | |
compileConditional ( |
Compiles a conditional, either if, elseif, else if or else into PHTML. | |
compileDo ( |
Compiles a do-instruction into PHTML. | |
compileDoctype ( |
Compiles a doctype Node to PHTML. | |
compileDocument ( |
Compiles a document Node to PHTML. | |
compileEach ( |
Compiles a each-instruction into a foreach-loop PHTML block. | |
compileElement ( |
Compiles an element-node containing a tag, attributes and assignments. | |
compileExpression ( |
Compiles an expression node and into a PHP expression. | |
compileFilter ( |
Compiles a filter-node into PHTML. | |
compileFor ( |
Compiles a for-loop into PHTML. | |
compileMixinCall ( |
Compiles a mixin call referencing the mixins in $_mixins. | |
compileMixins ( ) : string | Compiles found mixins under each other into a single PHTML block. | |
compileNode ( |
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 ( |
Compiles a text-node to PTHML. | |
compileVariable ( |
Compiles a variable-node into PHTML. | |
compileWhen ( |
Compiles a when-node into PHTML. | |
compileWhile ( |
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 ( |
Stacks blocks into each other. | |
handleBlocks ( |
Collects all blocks and saves them into $_blocks. | |
handleImport ( |
Loads an imported file and merges the nodes with the current tree. | |
handleImports ( |
Collects all imports and handles them via ->handleImport. | |
handleMixin ( |
Pre-compiles a mixin into the $_mixin array. | |
handleMixins ( |
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, |
Throws a Compiler-Exception. |
public __construct ( array $options = null, |
||
$options | array | an array of options |
$parser | an existing parser instance | |
$lexer | an existing lexer instance |
protected compileBlock ( |
||
$node | the block node to compile | |
return | string | The compiled PHTML |
protected compileCase ( |
||
$node | the case node to compile | |
return | string | The compiled PHTML |
protected compileCode ( |
||
$node | the code node to compile | |
return | string |
protected compileComment ( |
||
$node | the comment-node to compile | |
return | string | The compiled PHTML |
protected compileConditional ( |
||
$node | the conditional node to compile | |
return | string | The compiled PHTML |
protected compileDoctype ( |
||
$node | the doctype-type node | |
return | string | the compiled PHTML |
protected compileDocument ( |
||
$node | The document-type node | |
return | string | the compiled PHTML |
protected compileEach ( |
||
$node | the each-node to compile | |
return | string | The compiled PHTML |
protected compileElement ( |
||
$node | the element node to compile | |
return | string | The compiled PHTML |
protected compileExpression ( |
||
$node | the expression node to compile | |
return | string |
protected compileFilter ( |
||
$node | the filter node to compile | |
return | string | The compiled PHTML |
protected compileFor ( |
||
$node | the while-node to compile | |
return | string | The compiled PHTML |
protected compileMixinCall ( |
||
$node | the mixin call node to compile | |
return | string | The compiled PHTML |
protected compileMixins ( ) : string | ||
return | string | The compile PHTML |
protected compileNode ( |
||
$node | the Node to compile | |
return | string | the compiled PHTML |
protected compileText ( |
||
$node | the text-node to compile | |
return | string | The compiled PHTML |
protected compileVariable ( |
||
$node | the variable node to compile | |
return | string | The compiled PHTML |
protected compileWhen ( |
||
$node | the when-node to compile | |
return | string | The compiled PHTML |
protected compileWhile ( |
||
$node | the while-node to compile | |
return | string | The compiled PHTML |
protected createMarkupComment ( string $text ) : string | ||
$text | string | the text to wrap into a comment |
return | string | the compiled XML comment |
protected createPhpComment ( string $text ) : string | ||
$text | string | the text to wrap into a comment |
return | string | the compiled PHP comment |
protected createShortCode ( string $code ) : string | ||
$code | string | the PHP expression to output |
return | string | The PHP expression |
public getParser ( ) : |
||
return |
protected handleBlock ( |
||
$node | the block node to handle |
protected handleBlocks ( |
||
$node | the node to search blocks in |
protected handleImport ( |
||
$node | the node to import |
protected handleImports ( |
||
$node | the root Node to search imports in |
protected handleMixin ( |
||
$node | the mixin node to compile |
protected handleMixins ( |
||
$node | the node to search mixins in |
protected isVariable ( string $value ) : boolean | ||
$value | string | the value to be checked |
return | boolean |
protected throwException ( string $message, |
||
$message | string | A meaningful exception message |
$relatedNode | The node the exception occured on |