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.
Author: Torben Koehn (tk@talesoft.io)
Author: Talesoft (info@talesoft.io)
Inheritance: use trait Tale\ConfigurableTrait
示例#1
0
    public function testComplexNestedTag()
    {
        $jade = <<<JADE
div
    nav
        ul
            li
                a
            li
                a
            li
                a
    nav
        ul
            li
                a
            li
                a
            li
                a
    nav
        ul
            li
                a
            li
                a
            li
                a
JADE;
        $this->assertEquals('<div><nav><ul><li><a></a></li><li><a></a></li><li><a></a></li></ul></nav><nav><ul><li><a></a></li><li><a></a></li><li><a></a></li></ul></nav><nav><ul><li><a></a></li><li><a></a></li><li><a></a></li></ul></nav></div>', $this->_compiler->compile($jade));
    }
    public function testComplexTagClassCombination()
    {
        $jade = <<<JADE
.test-container
    nav.test-nav
        ul.test-menu
            li.test-item
                a.test-link
            li.test-item
                a.test-link
            li.test-item
                a.test-link
    nav.test-nav
        ul.test-menu
            li.test-item
                a.test-link
            li.test-item
                a.test-link
            li.test-item
                a.test-link
    nav.test-nav
        ul.test-menu
            li.test-item
                a.test-link
            li.test-item
                a.test-link
            li.test-item
                a.test-link
JADE;
        $this->assertEquals('<div class="test-container"><nav class="test-nav"><ul class="test-menu"><li class="test-item"><a class="test-link"></a></li><li class="test-item"><a class="test-link"></a></li><li class="test-item"><a class="test-link"></a></li></ul></nav><nav class="test-nav"><ul class="test-menu"><li class="test-item"><a class="test-link"></a></li><li class="test-item"><a class="test-link"></a></li><li class="test-item"><a class="test-link"></a></li></ul></nav><nav class="test-nav"><ul class="test-menu"><li class="test-item"><a class="test-link"></a></li><li class="test-item"><a class="test-link"></a></li><li class="test-item"><a class="test-link"></a></li></ul></nav></div>', $this->_compiler->compile($jade));
    }
示例#3
0
    public function testComplexTagIdCombination()
    {
        $jade = <<<JADE
#testContainer
    nav#testNav1
        ul#testMenu1
            li#testItem1
                a#testLink1
            li#testItem2
                a#testLink2
            li#testItem3
                a#testLink3
    nav#testNav2
        ul#testMenu2
            li#testItem4
                a#testLink4
            li#testItem5
                a#testLink5
            li#testItem6
                a#testLink6
    nav#testNav3
        ul#testMenu3
            li#testItem7
                a#testLink7
            li#testItem8
                a#testLink8
            li#testItem9
                a#testLink9
JADE;
        $this->assertEquals('<div id="testContainer"><nav id="testNav1"><ul id="testMenu1"><li id="testItem1"><a id="testLink1"></a></li><li id="testItem2"><a id="testLink2"></a></li><li id="testItem3"><a id="testLink3"></a></li></ul></nav><nav id="testNav2"><ul id="testMenu2"><li id="testItem4"><a id="testLink4"></a></li><li id="testItem5"><a id="testLink5"></a></li><li id="testItem6"><a id="testLink6"></a></li></ul></nav><nav id="testNav3"><ul id="testMenu3"><li id="testItem7"><a id="testLink7"></a></li><li id="testItem8"><a id="testLink8"></a></li><li id="testItem9"><a id="testLink9"></a></li></ul></nav></div>', $this->_compiler->compile($jade));
    }
示例#4
0
 public function convert($inputPath)
 {
     $this->prependOptions(['paths' => []]);
     $parser = new Parser(file_get_contents($inputPath), ['filename' => $inputPath, 'includes' => $this->getOption('paths')]);
     $compiler = new Compiler(!$this->getManager()->getOption('minify'));
     return $compiler->compile($parser->parse());
 }
示例#5
0
    public function testMultiLineCssFilter()
    {
        $jade = <<<JADE
:css
    body, html {
        can-you: imagine;
        that: this;
        works: perfectly;
    }
JADE;
        $this->assertEquals('<style>body, html { can-you: imagine; that: this; works: perfectly; }</style>', $this->_compiler->compile($jade));
    }
    public function tesComplexExpansion()
    {
        $jade = <<<JADE
a: b: c
    d: .e: #f
a: b: c(aa='bb'): d
    d: e:
        f: g: h
    d: e
        f: g: h
JADE;
        $this->assertEquals("<input value=\"Line 1\rLine 2\">", $this->_compiler->compile($jade));
    }
示例#7
0
    public function testComplexExpansion()
    {
        $jade = <<<JADE
a: b: c
    d: .e: #f
a: b: c(aa='bb'): d
    d: e
        f: g: h
    d: e
        f: g: h
JADE;
        $this->assertEquals('<a><b><c><d><div class="e"><div id="f"></div></div></d></c></b></a><a><b><c aa="bb"><d><d><e><f><g><h></h></g></f></e></d><d><e><f><g><h></h></g></f></e></d></d></c></b></a>', $this->compiler->compile($jade));
    }
     $id = null;
     $path = null;
     do {
         $id = uniqid();
         $path = SAVE_PATH . '/' . implode('/', str_split($id)) . '.jade';
     } while (file_exists($path));
     $dir = dirname($path);
     if (!is_dir($dir)) {
         mkdir($dir, 0755, true);
     }
     file_put_contents($path, $jade);
     echo json_encode(['success' => true, 'id' => $id]);
     exit;
 case 'compile':
     $compilerOptions = ['pretty' => isset($_POST['pretty']) ? $_POST['pretty'] === 'true' : false, 'standAlone' => isset($_POST['standAlone']) ? $_POST['standAlone'] === 'true' : false, 'allowImports' => false];
     $compiler = new Compiler($compilerOptions);
     $result = null;
     try {
         $result = $compiler->compile($jade);
     } catch (\Exception $e) {
         echo json_encode(['success' => false, 'message' => "\n" . get_class($e) . "\n\n" . $e->getMessage()]);
         exit;
     }
     echo json_encode(['success' => true, 'output' => $result]);
     exit;
 case 'lex':
     $lexer = new Lexer();
     $result = null;
     try {
         ob_start();
         $lexer->dump($jade);
示例#9
0
 public function testClassNestedExpansion()
 {
     $this->assertEquals('<div class="a b"><div class="c d">Hello, World!</div></div>', $this->compiler->compile(".a.b: .c.d\n\t| Hello, World!"));
 }
示例#10
0
<?php

include '../../vendor/autoload.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('error_reporting', E_ALL | E_NOTICE);
use Tale\Jade\Compiler;
use Tale\Jade\Renderer;
$minify = isset($_GET['minify']) ? true : false;
$handleErrors = isset($_GET['withErrorHandler']) ? true : false;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
    $compiler = new Compiler(['pretty' => !$minify, 'handleErrors' => $handleErrors, 'allowImports' => false]);
    $jade = isset($_POST['jade']) ? $_POST['jade'] : '';
    header('Content-Type: text/plain; encoding=utf-8');
    try {
        echo json_encode($compiler->compile(str_replace("\t", '    ', $jade)));
    } catch (Exception $e) {
        echo json_encode($e->getMessage());
    }
    exit;
}
$renderer = new Renderer(['compiler' => ['pretty' => true], 'adapter' => 'file', 'adapterOptions' => ['lifeTime' => 0]]);
$example = isset($_GET['example']) ? $_GET['example'] : 'welcome';
$exampleJade = '';
$example = preg_replace('/[^a-z0-9\\-]+/i', '', $example);
if (file_exists(__DIR__ . '/examples/' . $example . '.jade')) {
    $exampleJade = file_get_contents(__DIR__ . '/examples/' . $example . '.jade');
}
$action = isset($_GET['page']) ? preg_replace('/^[a-z0-9\\-]+$/i', '', $_GET['example']) : 'index';
$exampleJade = json_encode($exampleJade);
$url = $_SERVER['PHP_SELF'];
示例#11
0
 public function testCarriageReturnEscaping()
 {
     $this->assertEquals("<input value=\"Line 1\rLine 2\">", $this->_compiler->compile('input(value="Line 1\\rLine 2")'));
 }
示例#12
0
 public function testStyleRepeation()
 {
     $this->assertEquals('<a style="first: first-value; second: second-value"></a>', $this->_compiler->compile('a(style="first: first-value", style=\'second: second-value\')'));
 }
 public function testIssue21()
 {
     $this->assertEquals('<?=$view->render(\'_search\', [\'model\' => $searchModel])?>', $this->_compiler->compile('!=$view->render(\'_search\', [\'model\' => $searchModel])'));
 }
示例#14
0
 public function testBlock()
 {
     $this->assertEquals('<h2>Article 1</h2><p><strong>Block Content 1</strong> Awesome, isn\'t it?</p><h2>Article 2</h2><p><strong>Block Content 2</strong> And another block content</p>', $this->_renderer->render('block'));
 }
 /**
  * @dataProvider compilerOptionProvider
  */
 public function testCompilerGetsOptions($option, $value)
 {
     $compiler = new Compiler([$option => $value]);
     $this->assertEquals($value, $compiler->getOption($option));
 }
示例#16
0
 public function testReplace()
 {
     $this->assertEquals('<p>Element 4</p><p>Element 5</p><p>Element 6</p>', $this->_renderer->render('replace'));
 }
示例#17
0
 public function testDoWhileWithWhileChildren()
 {
     $this->setExpectedException(Compiler\Exception::class);
     $this->compiler->compile("do\n\tp Something\nwhile \$something\n\tp Anything");
 }
示例#18
0
 /**
  * Compiles a file to PHTML.
  *
  * The given path will automatically passed as
  * compile()'s $path argument
  *
  * The path should always be relative to the paths-option paths
  *
  * @see Renderer->compile
  *
  * @param string $path The path to the jade file
  *
  * @return mixed|string the compiled PHTML
  * @throws \Exception when the file wasnt found or the compilation,
  *                    lexing or parsing failed
  */
 public function compileFile($path)
 {
     return $this->_compiler->compileFile($path);
 }
 public function testFileNotFound()
 {
     $this->setExpectedException(Compiler\Exception::class);
     $this->_renderer->compile('include non-existent-file');
     $this->_renderer->compile('extends non-existent-file');
 }
示例#20
0
 public function testUnclosedAttributeBlockOnMixinCall()
 {
     $this->setExpectedException(Lexer\Exception::class);
     $this->_compiler->compile('+some-mixin(abc, def');
 }