zordius / lightncandy

An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ),
https://zordius.github.io/HandlebarsCookbook/
MIT License
608 stars 76 forks source link

RAW {{{page.title}}}} results in array. #39

Closed reno1979 closed 10 years ago

reno1979 commented 10 years ago

When using {{{ var }}} the compiled code shows only "array." At that point of the template.

I have objects that are used as input (typecasting for now, suggestions appreciated) but even a test with a simple array value fails.

And is there a way to a change the extension the loader looks for?

reno1979 commented 10 years ago

OW the extension part is all ready solved

zordius commented 10 years ago

More information required.... May you provide sample code of this issue? Thanks. And, the input object is subclass of array? does it have the magic method __toString() ?

reno1979 commented 10 years ago

Mmm none of the above, I'm afraid I'll have to add it to all objects if I want to be able to use them.

We are currently using mustache.php but are missing some handlebar helpers and and sometimes the rendering of loops acts strangely.

zordius commented 10 years ago

Well, if you mean {{page.title}} should output objectPage->title() , then it is not supported now. It's on road map but may reduce the performance of lightncandy , I may add a flag to support this in this week. (Then , maybe a performance test for this feature)

reno1979 commented 10 years ago

Thank you for the quick replies. We are already working with many objects so additional support for objects in lightncandy would be great.

Still it surprises me dat typecasting the object to an array before feeding it to the renderer causes these errors. (When I look at the array it seems ok) (array(page=>(array(title=>"test",x=>"b"))) I'll do some simple tests and let you know more.

reno1979 commented 10 years ago

Ok did some tests

$tpl    = isset($this->opts['footer']['tpl'])     ? $this->opts['footer']['tpl']      : 'include/footer';
$view = isset($this->opts['footer']['view'])    ? $this->opts['footer']['view']     : new Footer;

require APP_PATH.'class/lightncandy/src/lightncandy.php';
$template               = file_get_contents(DIR_NAME.'themes/'.THEME.'/templates/'.$tpl.'.mustache');
$compiledTemplateDir    = APP_PATH.'templates/';
$php_inc                = $compiledTemplateDir.$tpl.'.php';
$oLightnCandySettings   = Array(
           'flags' => LightnCandy::FLAG_STANDALONE || LightnCandy::FLAG_HANDLEBARSJS ,
           'basedir' => Array(DIR_NAME.'themes/'.THEME.'/templates'),
           'fileext' => Array(
                '.mustache',
                '.handlebars'
            )
);
/* Can the LightNCandy be set once, or should the settings be passed each time?? */
$compiledTemplate       = LightnCandy::compile($template, $oLightnCandySettings);

file_put_contents($php_inc, $compiledTemplate);
$renderer = include($php_inc);

if($tpl && $view){
                $oArray = (array)$view; // var_dump gives array(1) { ["cookieBanner"]=> bool(false) } 
                $oFooter = $renderer($oArray);
 } 
 return $oFooter;

The Footer class

<?php 
class Footer{

    public $cookieBanner; 

    function __construct(){
        $this->cookieBanner = $this->cookieBanner();
    }

    private function cookieBanner(){
        return isset($_COOKIE['accept_cookies']);
    }
}

The result is :

Fatal error: Function name must be a string

Update: The compiled template was empty, the template didn't render (valid Mustache). I'll use the debug options, to see what is says.

Solved The template used

{{#links.length}}
<h3>LINKS:</h3>
<ul>
{{#links.list}}
    <li>{{val}}</li>
{{/links.list}}
</ul>
{{/links.length}}

Replacing it by this template and simplifying the object, solved the issue:

{{#if links}}
<h3>LINKS:</h3>
<ul>
{{#each links}}
    <li>{{val}}</li>
{{/each links}}
</ul>
{{/if}}
reno1979 commented 10 years ago

Ok problem with RAW {{{ var }}} is still here :(

$compiledTemplate       = LightnCandy::compile("{{{tt}}}", $oLightnCandySettings);
file_put_contents($php_inc, $compiledTemplate);
$renderer = include($php_inc);
echo $renderer(array('tt'=>"bla bla bla "));

Gives us :

Notice: Array to string conversion in /mnt/htdocs/inbouwWerkmap3.0/application/class/lightncandy/src/lightncandy.php on line 1399

Parse error: syntax error, unexpected '.', expecting '(' in /mnt/htdocs/inbouwWerkmap3.0/application/templates/include/footer.php on line 16

The compiled footer.php:

<?php return function ($in, $debugopt = 1) {
    $cx = Array(
        'flags' => Array(
            'jstrue' => false,
            'jsobj' => false,
            'spvar' => false,
            'debug' => $debugopt,
        ),
        'helpers' => Array(),
        'blockhelpers' => Array(),
        'scopes' => Array($in),
        'sp_vars' => Array(),
        'path' => Array(),

    );
    return ''.Array.'';
}
?>
zordius commented 10 years ago
  1. It is a bug that only happened when FLAG_JSTRUE and FLAG_JSOBJECT and FLAG_RENDER_DEBUG not used. I fixed it.
  2. There is one problem in your code ......you should fix it.
'flags' => LightnCandy::FLAG_STANDALONE || LightnCandy::FLAG_HANDLEBARSJS ,

should be

'flags' => LightnCandy::FLAG_STANDALONE | LightnCandy::FLAG_HANDLEBARSJS ,
reno1979 commented 10 years ago

awesome!