rickitup
rickitup

Reputation: 3

Using Jsoup (the Java html parser) with Handlebarsjs

So, I'm using jsoup and when I display the content returned I Get:

{{#ifcond="" pagetitle="" this.name}}

I'm doing this like local.htmlObj.Body().Html()

When I need it to be return like:

{{#ifCond PAGETITLE this.NAME}}

Here what I'm doing

<cfset paths    = [] />
<cfset paths[1] = expandPath("/javaloader/lib/jsoup-1.7.1.jar") />
<cfset loader   = createObject("component", "javaloader.JavaLoader").init( paths ) />
<cfset obj      = loader.create( "org.jsoup.Jsoup" ) />

<cfset local.htmlObj = local.jsoupObj.parse( local.template ) />

<cfloop array="#local.htmlObj.select('.sidebar_left')#" index="element">
        <cfif element.attr('section') EQ "test">
            <cfset element.append('HTML HERE') />
        </cfif>
</cfloop>

local.template is my template that is made up of a ton of different handlebar files That im pulling for different places. I'm constructing one handlebar file that gets returned.

Upvotes: 0

Views: 536

Answers (1)

Jason Sperske
Jason Sperske

Reputation: 30446

The problem is that JSoup is trying to parse invalid HTML before it lets you access it. A slight easier to understand example of this behavior can be seen if you fetch the following HTML (seen in this question):

<p>
<table>[...]</table>
</p>

It will return:

<p></p>
<table>[...]</table>

In your case the Handelbars code is seen as attributes which in valid html always have a value (think checked="checked"). As far as I can tell there is no way to disable this behavior. It's really the wrong tool for the job you are trying to do. A cleaner approach would be to just fetch the document as a stream and save it to a string.

Upvotes: 2

Related Questions