jquery-tmpl icon indicating copy to clipboard operation
jquery-tmpl copied to clipboard

API to return html as string

Open markusweb opened this issue 14 years ago • 4 comments

is it possible to have a function call to return the resulting html just as a plain string? at the moment i have to use somehting like

   myHtml = $.tmpl($("#templateList").html(), data)
   data = $('<div />').append(myHtml).html();

i would be able to call something like

  myHtml = $.tmplString($("#templateList").html(), data);

markusweb avatar Jan 14 '11 09:01 markusweb

you are referring outerHTML feature, which is not natively available in jQuery (and this is not tmpl problem). Although you could use this workaround:

$( .... ).wrapAll( '

' ).parent().html()

Gedis avatar Feb 01 '11 13:02 Gedis

Yes, this is a feature we hope to provide in Beta2. See also the following, copied from http://github.com/nje/jquery-tmpl/issues/16

This feature is already available, but not very discoverable:

<script type="text/javascript">
    var somedata = [
        { name: "John", condition:"a>b" },
        { name: "Pete", condition:"b<a" }
    ];

    var tmplFn = $( "#personTmpl" ).template();

    var xmlResult = $.map( somedata, function( item ) {
        return tmplFn( $, { data: item } );
    }).join("");

</script>

Of course, although the above is reasonably easy to use, it is not at all discoverable. We should consider providing a discoverable API for this.

Note that calling tmpl() does indeed create DOM elements, and even adds jQuery data expandos to them, so even if you get the HTML string of the result, it will not be clean.

The above approach does not use the DOM at all, and so can also run on the server, etc. For attribute strings you may have characters that would be HTML encoded (as in my example above), so the {{html foo}} tag is appropriate in the template, rather than ${foo}.

BorisMoore avatar Mar 01 '11 18:03 BorisMoore

Thanks for taking the time to submit this issue. Just wanted to let you know this plugin is no longer being actively developed or maintained by the jQuery team. See README for more info.

rdworth avatar Oct 08 '11 06:10 rdworth

See issue 159.

BorisMoore avatar Oct 13 '11 19:10 BorisMoore