Search code examples
jsrenderjsviews

Migrating jsrender to jsviews {{else}}-issue


Issue with jsrender / jsviews. I have built a huge page which utilizes jsrender v.0.9.83 successfully.

Now however when I am trying to incorporate jsviews v.0.9.89 (and also trying to take jsrender v.0.9.89 into use) I am running into the most strange bugs.

The {{else}} block seems to cause errors.

Here is an example of an if else block:

{{if device_comment}}
    {{:device_comment}}
{{else}}
    {{:device_name}}
{{/if}}

This worked nicely in previous version, no issues.

Now however jsviews reports rendering error:

JsViews Error: Syntax error
Compiled template code:

// jsvTmpl
var v,t=j._tag,c=j._cnvt,ret=""

+"\n<tr data-id=\"";
try{
ret+=((v=data.id)!=null?v:"");
}catch(e){ret+=j._err(e,view,undefined);}ret=ret
+"\">\n <td><input type=\"checkbox\" class=\"sel\"/></td>\n <td>";
try{
ret+=((v=data.id)!=null?v:"");
}catch(e){ret+=j._err(e,view,undefined);}ret=ret

+"<br/>(";
try{
ret+=((v=data.id)!=null?v:"");
}catch(e){ret+=j._err(e,view,undefined);}ret=ret
+")</td>\n  <td>\n      ";
try{
ret+=t("if",view,this,[
{view:view,tmpl:1,
    params:{args:['device_comment']},
    args:[data.device_comment],
    props:{}},;
}catch(e){ret+=j._err(e,view,undefined);}ret=ret
{view:view,tmpl:2,
    params:{args:[]},
    args:[],
    props:{}}]);
}catch(e){ret+=j._err(e,view,undefined);}
........ (cut to save space) ...........
return ret;
: "Unexpected token ;"

The only unexpected token I can find is ",;" in this part:

view:view,tmpl:1,
    params:{args:['device_comment']},
    args:[data.device_comment],
    props:{}},;

Now, the funny thing is, if I find and remove all {{else blocks the template will render without errors (but of course not correctly since my page expects formatting based on else blocks).

My question is, how is this possible? What does jsviews want to do differently than jsrender in these cases? Why does the else block fail to render properly?

EDIT: I downloaded an older version of jsviews, v.0.9.83 and then used it with jsrender v.0.9.83 and everything works. I do not get this {{else}}-block issue.

I will leave this question open, because it seems there is an issue in the newer jsviews library. Hopefully other people will notice this post and not have to debug it alot.

I will continue my work with v.0.9.83.


Solution

  • Thanks for calling this out. In fact it was a bug introduced in v0.9.89 (so previous versions are good, up to v0.9.88).

    But the bug only occured if you had set $.views.settings.debugMode(true);. If you left debugMode false, things worked correctly.

    The bug has been fixed in the v0.9.90 release.

    Incidentally, for the specific example you gave:

    {{if device_comment}}
        {{:device_comment}}
    {{else}}
        {{:device_name}}
    {{/if}}
    

    you can replace it with the simpler:

    {{:device_comment||device_name}}
    

    which also avoids hitting that issue...