Talesoft / tale-jade

A complete and fully-functional implementation of the Jade template language for PHP
http://jade.talesoft.codes
MIT License
88 stars 10 forks source link

Converting a Jade string to xml #59

Closed vdcrea closed 8 years ago

vdcrea commented 8 years ago

Hi, thanks for this huge work. I am trying to integrate tale-jade in a cms extension, the idea is not to convert jade files but directly type jade syntax in a textarea and output xml or xhtml for front end.
But I don't understand how to convert a jade-string (not a jade file), any idea about how to configure? Here is my php:

<?php
use Tale\Jade\Renderer;
include('extensions/jade/vendor/autoload.php');
class FormatterJade extends TextFormatter
{
    private $renderer;
    public function __construct()
    {
        $this->renderer = new Renderer([
            'pretty' => true,
            'mode' => MODE_XML,
            'paths' => null
        ]);
    }
    public function about()
    {
        return [ 'name' => 'Jade' ];
    }
    public function run($string)
    {
        $input = $string;
        $string = $this->renderer->compile($input, $path = null);
        return $string;
    }
}

I tryed other before, but always produce an error:
Parse error: syntax error, unexpected '$token' (T_VARIABLE) in vendor/talesoft/tale-jade/Lexer.php on line 378

TorbenKoehn commented 8 years ago

I think the problem here is your PHP version.

Line 378 of the Lexer.php contains yield $token, so you're probably running a PHP version not supporting yield yet.

Tale Jade makes heavy use of Generators which improve the overall performance of the lexing and parsing process.

Tale Jade is tested for all versions above PHP5.5 including Hack/HHVM. PHP5.5 is the first version with Generators. https://github.com/Talesoft/tale-jade/blob/master/.travis.yml

Do you have a possibility to upgrade your PHP version to at least 5.5?


Also, for the compilation process you don't need a full renderer, you only need a Tale\Jade\Compiler


$compiler = new Tale\Jade\Compiler(['pretty' => true, ...]);

$xml = $compiler->compile($string);
vdcrea commented 8 years ago

One word: awesome! Thanks you very much, it works like a charm. Now I will see how it is with Ace editor on the other side, or leave it as a simple textarea. I will let you know when it s done to show you the result.

TorbenKoehn commented 8 years ago

You may take some impressions from here:

https://github.com/Talesoft/tale-jade-live-compiler/blob/master/scripts/common.coffee Take a look at the whole repository.

The working version can be found here: http://sandbox.jade.talesoft.io

Glad that it works now, have fun with it :)

vdcrea commented 8 years ago

Extension for Symphony CMS published: http://www.getsymphony.com/download/extensions/view/111595/ Thanks for your help!

TorbenKoehn commented 8 years ago

Great, I'll add your extension to our README file today :)