+ Start a Discussion
sdudasduda 

Print Anything Equals Code (Add-on)

While using Print Anything, I extended the driver to include support for an equals tag. If anyone is using print anything, I found this very helpful when creating more advanced templates. It also supports nested equal statements.

Here's how it works:

<prtany:equal Contact1.FirstName="some value" Contact1.LastName="some other value" ........ >
  
    Displays this text only if:
    Contact1.FirstName="some value" and
    Contact1.LastName="some other value"

    <prtany:equal Contact1.LastName="some other value 2">
  
        Displays this text only if:
        Contact1.LastName="some other value 2" and
        parent equals tag is true

    </prtany:equal>

</prtany:equal>

<prtany:equal Contact1.FirstName!="some value" >
  
    Displays this text only if:
    Contact1.FirstName does not equal "some value"

</prtany:equal>



(Put new code inside the merge function after the last while statement)

Code:
 // remove any conditional content
while (result.indexOf("<prtany:equal") != -1) {
var level = 1;

// Find next start tag (<prtany:equal ... >)
var startTagStartPos = result.indexOf("<prtany:equal");
var startTagEndPos = result.indexOf(">", startTagStartPos);

var parsePos = startTagEndPos;

var nextStartTagPos;
var nextEndTagPos;

// Find corresponding end tag (</prtany:equal>)
do {
nextStartTagPos = result.indexOf("<prtany:equal",parsePos);
nextEndTagPos = result.indexOf("</prtany:equal", parsePos);
if( nextStartTagPos < nextEndTagPos && nextStartTagPos != -1 ) {
level = level + 1;
parsePos = nextStartTagPos + 1;
} else {
level = level - 1;
parsePos = nextEndTagPos + 1;
}
} while ( level > 0 )

if (nextEndTagPos == -1) {
throw "No equal end tag was found";
}

var endTagEndPos = result.indexOf(">", nextEndTagPos);

var conditionalString = result.substring(startTagEndPos+1,nextEndTagPos);

// Parse start tag conditions (<prtany:equal var="value" var2!="value" ... >
var statement = result.substring(startTagStartPos+1,startTagEndPos);
var reg = /(\S+?)\s*(\!?=)\s*\"(.+?)\"/g;

var match = reg.exec( statement );
while( match != null ) {
var conditionalValue = mergeData[match[1]];
if( match[2] == "!=" ) {
if (conditionalValue == match[3]) {
conditionalString = "";
}
} else {
if (conditionalValue != match[3]) {
conditionalString = "";
}
}
match = reg.exec( statement );
}

result = result.substring(0,startTagStartPos) + conditionalString + result.substring(endTagEndPos+1)
}

 





Message Edited by sduda on 03-29-2007 02:23 PM

Message Edited by sduda on 04-09-2007 09:28 AM

Message Edited by sduda on 04-09-2007 09:30 AM

SethSeth
This works great, except the != doesn't seem to work for me.  I'm trying to do something like an if/then statement:

<prtany:equal Contact1.Education__c="01-20 - Highest Grade Completed">
{Contact1.Highest_Grade__c}
</prtany:equal>
<prtany:equal Contact1.Education__c!="01-20 - Highest Grade Completed">
{Contact1.Education__c}
</prtany:equal>

It ends up printing both.  Any ideas?

Otherwise, great work!

Seth
sdudasduda
For some reason it replaced the question marks with dashes in my code. I've fixed the problem. Thanks!
-- Seth