BorisMoore / jquery-tmpl

The original official jQuery Templates plugin. This project was maintained by the jQuery team as an official jQuery plugin. It is no longer in active development, and has been superseded by JsRender.
3.23k stars 1.01k forks source link

Allow template variable without HTML tags to be used in {{tmpl}} #72

Closed garann closed 13 years ago

garann commented 13 years ago

Trying to fix the problem here: https://github.com/jquery/jquery-tmpl/issues#issue/69

In jQuery.template, changed the tests of the name parameter to check whether it can be used as a selector first, and if not, use it as-is. Previously it appeared that the htmlExpr test would only accept it if it contained HTML tags, and didn't work for short snippets of text.

Before: return name ? (typeof name !== "string" ? jQuery.template( null, name ): (jQuery.template[name] || // If not in map, treat as a selector. (If integrated with core, use quickExpr.exec) jQuery.template( null, htmlExpr.test( name ) ? name : jQuery( name )))) : null;

After: return name ? (typeof name !== "string" ? jQuery.template( null, name ): (jQuery.template[name] || // If not in map, treat as a selector. (If integrated with core, use quickExpr.exec) jQuery.template( null, jQuery( name )[0] ? jQuery( name ) : name))) : null;

BorisMoore commented 13 years ago

Thanks Garann, But your fix is inverting the logic if name contains HTML tags. At that point, it should be treated as markup for a template (case b) in the issue). In your modified code, jQuery( name )[0] will not be null, so jQuery( name ) will be passed to jQuery.template. This is wrong. In that scenario, you want to pass the HTML string, name, to jQuery.template... Also I am uneasy about using jQuery( name )[0] as a test. If name is "#myTemplate" then if currently the script element with id "myTemplate" is not found in the DOM, then jQuery( name )[0] will be null, and "#myTemplate" will be treated as markup. Then if you include the script element, "myTemplate", suddenly it will be treated as a selector...