0

I am using jquery templates which I populate using an ajax request. However, one of the values returned is encoded HTML. How can I encode it?

I have tried using ${$.mobile.html(Body).text()} but that didn't work for me.

My code:

Domain.Discussion.ListView = Domain.Discussion.ListView || {
    DiscussionPage: (function () {
        var onGetDiscussionSuccess = function (data) {
            $("#discussionsList ul").remove();
            $("#discussionListItem").tmpl(data.DiscussionsResult).appendTo("#discussionsList", function () {
                reloadAndFixPanelContent()
            });
        }

        var onGetDiscussionError = function () {
            console.log("Error occured when retrieving discussions");
        }

        $.ajax({
            url: absolutePath + "Discussions",
            headers: { "Accept": "application/json; odata=verbose" },
            success: onGetDiscussionSuccess,
            error: onGetDiscussionError
        });
    }())
};

Html:

<!-- Discussion replies -->
<script id="replies" type="text/x-jquery-tmpl">
<div class="message message-first">
    <div class="message-header">
        <div class="message-header-user">
            <h1>${Author}</h1>
            <h2>Role not set yet</h2>
        </div>
        <div class="message-header-date">${Created}</div>
    </div>
    <div class="message-content">
        <span>${$.mobile.html(Body).text()}</span>
        <hr />
    </div>
</div>
</script>
<!-- /Discussion replies -->
5
  • Can you show us your ajax response? Commented May 2, 2013 at 10:12
  • The ajax call returns, json formatted, a few items. Including an item called Body with value (HTML Encoded) <div class="ExternalClassC23D0863D23B4421A039D700D6AECF79">Hejsan hoppsan!<br><p>​</p></div>. Commented May 2, 2013 at 10:25
  • What I don't understand is what do you mean by "encoded HTML"? Commented May 2, 2013 at 10:36
  • e.g. <p>Hello, world!</p> becomes &lt;p&gt;Hello, world!&lt;/p&gt; when encoded. I found the solution now. Commented May 2, 2013 at 10:42
  • Ahaaaaaaaaaa, but because you fond an answer we will stop our conversation. :) Commented May 2, 2013 at 10:46

1 Answer 1

1

After searching and searching I finally found the solution here: jQuery tmpl: How do I render Html?

Using {{html Body}} in my template worked like a charm.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.