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

How can I get substring from parsed html in jquery.tmpl #187

Closed ajay-verma8245 closed 8 years ago

ajay-verma8245 commented 8 years ago

My jquery template is very simple which is <div id="pageArticleTemplate" class="hide-me"> <article class="post-item"> <div class="wrapper row"> <div class="meta"> ${getArticleDateFormat(createdDate)} </div> <h2>${title}</h2> <div class="excerpt text-justify"> {{html description}} </div> </div> </article> </div>

I am using this template in my js file as: var markup = $("#pageArticleTemplate").html(); $.template("pageTemplate", markup); $.tmpl("pageTemplate", data).appendTo("#articles");

where data is the actual data I want to repeat. and article is some another div element on page. It's working fine, but problem is sometimes description is too long and I want to display only first 500 characters. As you can see description is html content and I have already tried using function like I used for createdDate(function getArticleDateFormat{})

Please provide some solution. Also, My Stackoverflow question : How can I get substring from parsed html in jquery.tmpl

BorisMoore commented 8 years ago

You problem is not a jQuery Templates bug/issue - so I will close this. In fact what you are looking for is a an issue with HTML in general - namely that you cannot simply truncate HTML markup to make it display with fewer characters. Obviously if you truncate it it will likely no longer be well formed HTML. Either you have to simply display the text content and truncate that, or you have to parse, or step through the HTML nodes, and remove or truncate the text nodes at the end.

ajay-verma8245 commented 8 years ago

Thanks a lot sir. I am just looking for my solution. It't not a template issue but also I am not able to apply any function like i used in createdDate. Can I use some function after whole parsing of html?