Reputation: 7362
I have a Meteor Collections called Projects. Each project has a title, and description.
I want to pull out the rich text data from the description field in my Template helper.
I am trying something like this to get the description for a specific project:
Template.projectItem.helpers({
description: function () {
descriptionHTML = Projects.findOne(this._id, {description: {}}); // Get description field for project
descriptionString = descriptionHTML.text(); // Strip html tags
return descriptionString; // return description string
}
});
But it is not working. The reason I want to pull out the rich text data before having it render on the template is because I want to strip the data from HTML formatting tags before displaying it on the template.
1) How do I get the description field for a specific document in the collection only? I want to be able to access the description field from the js file.
2) One I get the description, how can I easily strip the html tags? Can I use jQuery on a string variable? Like
description.text();
Upvotes: 1
Views: 897
Reputation: 7362
Specifically what I did to solve this issue was add this into the template helper js file:
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
Handlebars.registerHelper('stripContent', function(content){
return strip(content);
});
Then in the template html file I used
{{stripContent description}}
Worked like magic! They key is using handlebars register helper which is extremely useful!
Upvotes: 0
Reputation: 2485
register a global helper for your current Projects
Handlebars.registerHelper('project', function() {
projectId = Session.get('projectId');
return Projects.findOne( projectId );
});
In your template you can use
{{#with project}}
{{description}}
{{/with}}
or
{{project.description}}
Handlebars.registerHelper('escapeContent', function(content){
return _.escape(content);
});
{{escapeContent description}}
Template.postItem.description = function() {
return _.escape(this.description)
}
@Projects = new Meteor.Collection 'projects'
Projects._transform = (project) ->
project.description = _.escape project.desscription
and you don't have to do it in template.
Upvotes: 3