Jump to content

Gender Based front and back pages


Mike

Recommended Posts

This community is amazing and has helped me in so many ways so a really big thanks to eric and others who have helped me. I need a hand to turn on and off pages based on a title field.

 

Below is my code attempt:

 

 
// OnRecordStart

if (Field("Title")) contains (Mr, Mr., Master, Master.)

{
  FusionPro.Composition.SetBodyPageUsage("Male-Front",true);    
  FusionPro.Composition.SetBodyPageUsage("Male-Back",true);
}
else {
  FusionPro.Composition.SetBodyPageUsage("Female-Front",true);    
  FusionPro.Composition.SetBodyPageUsage("Female-Front",true);

 

Any help is greatly appreciated.

Link to comment
Share on other sites

What are all the possible values Field("Title") may contain? I'm thinking it might be easier to search for the female values instead. Searching for "Mr" will return a true value for "Mr" and "Mrs" but searching for "Mrs" would ignore a "Mr". There are ways to deal with this using regular expressions, but I always prefer the easiest solution. ;)

 

To search for a portion of a string in a value you would use the indexOf() function. This replaces your "contain" above. So your IF statement might look something like:

if (Field("Title").indexOf(/Mr(?!\w)|Mr\.|Master/gi > -1)

That long line of gobbledy-gook in the indexOf brackets is the regular expression search. It breaks down as follows:

/ -- initiates and ends a regular expression

(?!\w) -- the parenthesis "()" group a condition "?" saying not "!" another alphanumeric character "\w". This is what allows "Mr" to return true, but not "Mrs".

| -- means OR; this regular expression has 3 scenarios separated by the pipe character "|"; any of the 3 can match to return a value greater than -1

\. -- the backslash escapes the period so that it is treated as a character rather than a code

gi -- the letters outside the forward slashes indicate a global "g", case-insensitive "i" search of the preceding terms

> -1 -- if the search values are not found, the function returns a "-1". If any of the values are present, the function returns the index of the first position where a string begins which means that if the string exists, the return value will be greater than "-1"

 

Now having said all that, I'm still a bit green when it comes to regular expressions so no promises on my code above! Oh, and although the code may be forgiving, technically you also need a closing curly bracket following your ELSE loop. ;)

Link to comment
Share on other sites

Thanks for your help Eric. I like your suggestions. The possible salutaions for women are:

- Mrs

- Miss

- Ms

- Prof

- Dr

 

Your help is great Eric. Im about as green as they come so your help has really saved my bacon. Hope you can help with the female values.

Link to comment
Share on other sites

To check for female titles, change the code snippet above to the following:

if (Field("Title").indexOf(/Mrs|Miss|Ms|Prof|Dr/gi > -1)

(Although I'm surprised that a title of "Dr" and "Prof" always infer a female recipient.) If you use this line of code, you do not need the one posted previously -- you would just use an ELSE loop to instruct what to do when the title is not female-oriented.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...