Conditional Sections (if / else)

Scenario: You are creating a contract for services that will be rendered by your business. In this document you have the general outlines of your services, along with some specific services you will be providing. However, not all contracts are the same. You have a section in the contract for each of your 5 different services you provide and you only want to show the applicable sections.

No problem! You can use conditional sections to only show the selected service sections. Using the built-in "if" statement blocks, you can determine which sections should be displayed based on the data values of the document.

For example:

{if $State == "CA"}

This section will display if the value for "State" is equal to "CA". The document will continue to display any text in between the "if" block.

{elseif $State == "AZ"}

This section will display if the value for "State" is equal to "AZ".

{else}

This section will display if the value for "State" is anything else.

{/if}

 

Here are a few other example "if" statements you can use in WebMerge:

Example Description
{if empty($address)} If $address is empty (does not have a value)
{if !empty($name)} If $name is not empty (has a value)
{if $price > 100 and $shipping > 10} If $price is greater than 100 and $shipping is greater than 10
{if $description != ''} If $description is not a blank string
{if $size == 'M' or $price > 20} If $size is "M" or the $price is greater than 20
{if $price > 10 and ($size == "M" or $size == "L")} If $price is greater than 10 and size is "M" or "L"
{if stristr($Title, "Fun")} If $Title contains (search for) "Fun"
{if is_array($Colors)} If $Colors is an array/list
{if in_array("Green", $Colors)} If $Colors contains (search array/list) "Green"
{if strtotime($ExpirationDate) > strtotime("2017-12-31")} If $ExpirationDate is after 2017-12-31

 

For more information on using "if" statements, please take a look at the Smarty documentation here: http://www.smarty.net/docs/en/language.function.if.tpl

 

Hide Empty Lines

If you'd like to hide lines that do not have a value, you can use an if statement that keeps the line break inside the if statement.  Like this:

{$InformalGreeting} {$LastName}{if !empty($Company)}

{$Company}{/if}

{$BillingStreet}{if !empty($BillingStreet2)}

{$BillingStreet2}{/if}

{$City}, {$State} {$Zip}

 

Hide Table Rows

If you would like to hide rows in a table, you can use our {tableif} tag which looks just like any other {if} statement.  Like this:

Name Quantity Price Total
{tableif !empty($Product1)}{$Product1} {$Quantity1} {$Price1} {$Total1}{/tableif}
{tableif !empty($Product2)}{$Product2} {$Quantity2} {$Price2} {$Total2}{/tableif}
{tableif !empty($Product3)}{$Product3} {$Quantity3} {$Price3} {$Total3}{/tableif}

 

Hide List Items

If you would like to hide an item in a list, you can use our {listif} tag which looks just like any other {if} statement.  Like this:

  • {listif !empty($Product1)}Product 1: {$Product1}{/listif}
  • {listif !empty($Product2)}Product 2: {$Product2}{/listif}
Have more questions? Submit a request

101 Comments

  • Avatar
    Jeremy Clarke

    Hi Darius,

    Yes, you can do that :)

  • Avatar
    Darius Grimes

    I'm building tables with if statements which work very nicely but is there a way to keep the table rows from breaking across pages?

  • Avatar
    Jeremy Clarke

    HI Darius,

    If you use a Word template, then you can use the functionality that exists in the Table Properties (under Row).

    If you're using the document builder, you can add CSS to your table rows like this:

    <tr style="page-break-inside:avoid">
      <td>Test</td>
    </tr>

    Edited by Jeremy Clarke
  • Avatar
    Darius Grimes

    Hmm that did not work

  • Avatar
    Darius Grimes

    Jeremy,

    I did find and interesting work around. I was using a 2x2 row/column table and the issue was the 1st and 2nd rows were breaking over pages separating my captions in row 1 from the photos below in row 2. I added a 3rd column to the left but merged the 1st and 2nd rows to make a single row column the height of the table. Now it cant break the rows apart on pages because its treats the table as a single row. Works like a charm and stupidly simple.

  • Avatar
    Dee Huss

    Can you have nested if statements?

  • Avatar
    Jeremy Clarke

    Hi Dee,

    Yes, you can! Like this:

    {if $FavoriteColor == "Blue}
    {if $Gender == "Female"}
    TEXT HERE
    {else}
    OTHER TEXT HERE
    {/if}
    {/if}

  • Avatar
    Service Fplus

    Hi Jeremy,

    {
    "status" : "{if $OFFICER_NAME != 'JOHN'}created{else}sent{/if}",
    "sender": "{if $OFFICER_NAME != 'JOHN'}{$OFFICER_EMAIL}{else}abc@gmail.com{/if}",
    "signer1": {
    "embeddedRecipientStartURL": "SIGN_AT_DOCUSIGN",
    "clientUserId": "{$SF_PERSON_ID}",
    "recipientId": "{$LOS_PERSON_ID}"
    }{if !empty($CO_APP_LOS_PERSON_ID)},
    "signer2": {
    "embeddedRecipientStartURL": "SIGN_AT_DOCUSIGN",
    "clientUserId": "{$CO_APP_SF_PERSON_ID}",
    "recipientId": "{$CO_APP_LOS_PERSON_ID}"
    }{/if}
    }

    I've added this code in Customize extra options/settings for delivery. Because of adding {if !empty($CO_APP_LOS_PERSON_ID)} I am receiving the status as "sent" even if OFFICER_NAME is not John. I am receiving "created" status only if I remove {if !empty($CO_APP_LOS_PERSON_ID)}. Can you explain me why?

  • Avatar
    Jeremy Clarke

    Hello,

    The "!=" is a case-sensitive comparison, so this will only return true when the value is "JOHN" and not "John". I suggest something like this instead:

    {if strtoupper($OFFICER_NAME) != 'JOHN'}created{else}sent{/if}

  • Avatar
    Service Fplus

    Jeremy,
    Sorry for the typo. I haven't used either JOHN or John as OFFICER_NAME. So expecting to see "created" status but I am seeing "sent" status instead of "created". I can see "created" status only if I remove this {if !empty($CO_APP_LOS_PERSON_ID)} condition in the above code. Can you help me why {if !empty($CO_APP_LOS_PERSON_ID)} is changing the result of this {if $OFFICER_NAME != 'JOHN'}created{else}sent{/if} block?

  • Avatar
    Jeremy Clarke

    Ahh, I see the issue. We don't allow you to use the if statements outside of JSON fields. We try to parse the JSON first, then we process the merge fields. So your if statement around signer2 is causing invalid JSON so it's not processing any of the settings.

    Can you remove that if statement so that it is valid JSON?

  • Avatar
    Service Fplus

    Jeremy,
    We have configured applicant as signee1 and co-applicant as signee2 and OFFICER_EMAIL as CC. If I remove {if !empty($CO_APP_LOS_PERSON_ID)} like you suggested, I am getting the following error when co-applicant is not present AND CC in the configuration. If I remove that if statement contract is generating either when both applicant and co-applicant are present OR removing the CC.

    Error: There was an error sending the DocuSign invites: INVALID_RECIPIENT_ID: A recipient ID is missing or invalid. Invalid format for recipientId. Int or Guid expected.

    Can you provide us the solution to generate contract when co-applicant is NOT present with CC configuration in the delivery?

  • Avatar
    Jeremy Clarke

    Hello,

    In that case, I suggest you setup 2 DocuSign deliveries on your document then use logic to determine which to use (under More Options). This will allow you to setup the JSON accordingly (if there is a signer 2 or not)

  • Avatar
    Service Fplus

    Jeremy,

    That will have 2 merges and it'll cost us more. Is there any alternative for this problem?

  • Avatar
    Jeremy Clarke

    No, it will not be 2 merges. The logic you setup on the deliveries will only cause 1 of them to be triggered each time.

  • Avatar
    Karen Spence

    This whole page is GREAT! It's bookmarked. I still have a question. My goal is to get as close to having one proposal template for all product proposals. So, I have inserted a couple of if statements to test displaying paragraphs of text for each product family that are added to the Opportunity and appear in the array. Each paragraph of text is surrounded by the appropriate IF statement and close tag.

    My tests show that the proposal for an Opportunity with only products from one Family displays the right text. Same for a test with an Opportunity with only the other Family Products.

    It's when products from both families are added to the opportunity that the problem occurs. It only displays the first product family's text, and not both. Is there an AND required somewhere? What am I missing? Thank you.

    The Salespeople and Sales Support are going to be so excited about this, FYI. :)

  • Avatar
    Darius Grimes

    If you have multiple variables you may need to run through each one. Also, AND is one choice but for variables OR may work better.

  • Avatar
    Jeremy Clarke

    Hi Karen,

    Yes, here are a few examples:

    {if $Color == "Red" and $Size == "Large"}
    TEXT HERE
    {elseif $Color == "Red" and $Size == "Small"}
    OTHER TEXT HERE
    {/if}

  • Avatar
    Karen Spence

    Thank you, Darius and Jeremy.

    So if I have 6 Product Families, each with its own series of text, it would be like this, and there is an implied AND/OR here? I have a feeling that what I have here is too simple.

    {if $_product.Product2Id.Family == "Family1"}

    TEXT

    {elseif $_product.Product2Id.Family == "Family2"}

    TEXT

    {elseif $_product.Product2Id.Family == "Family3"}

    TEXT

    {elseif $_product.Product2Id.Family == "Family4"}

    TEXT

    {elseif $_product.Product2Id.Family == "Family5"}

    TEXT

    {elseif $_product.Product2Id.Family == "Family6"}

    TEXT

    {/if}

    Edited by Karen Spence
  • Avatar
    Darius Grimes

    You need an {/if} for each instance. That can work as long as you don't have multiples e.g. {if $_product.Product2Id.Family == "Family1 Family2"}

    In that case you need to include every iteration. You might want to split the question from a multiple choice to one that allows the user to answer the question in more than one step. Then you can test for each type.

    i.e.
    Is there more than one Family Type?
    Family Type1= Y/N-N/A
    Family Type 2= Y/N-N/A
    etc.

    {if $_product.Product2Id.Family_type1 == "YES"}

    TEXT

    {/if}

  • Avatar
    Karen Spence

    I am combining all product templates into one by request. We have 25 different products but I'm using the Product Family to keep it relatively simple. So if an Opportunity has several products from multiple Product Families, then each family text needs to display. .

    Is that simple to do? What I have above isn't working, and it's not clear why it selected and displayed the Product Family text that it did.

  • Avatar
    Darius Grimes

    If you have "Save Data to Re-Merge" turned on you can look at the data set to see whats coming over from your database. From what I understand from your description, you are going to have to write the {if} statement to include every combination, that is a lot of writing.
    I have several routines that are similar. To increase the speed and reduce the amount of HTML I need to write,I break it down into smaller steps and then just combine the parts in the report. Jeremy would have a better idea of solutions from this point forward.

  • Avatar
    Darius Grimes

    Also, make sure you close each {if} statement

    {if $_product.Product2Id.Family == "Family1"}
    TEXT {/if}
    {elseif $_product.Product2Id.Family == "Family2"}
    TEXT {/if}

    {elseif $_product.Product2Id.Family == "Family3"}
    TEXT {/if}
    {elseif $_product.Product2Id.Family == "Family4"}
    TEXT {/if}
    {elseif $_product.Product2Id.Family == "Family5"}
    TEXT {/if}
    {elseif $_product.Product2Id.Family == "Family6"}
    TEXT
    {/if}

    OR

    {if $_product.Product2Id.Family == "Family1"}
    TEXT
    {elseif $_product.Product2Id.Family == "Family2"}
    TEXT

    {elseif $_product.Product2Id.Family == "Family3"}
    TEXT
    {elseif $_product.Product2Id.Family == "Family4"}
    TEXT
    {elseif $_product.Product2Id.Family == "Family5"}
    TEXT
    {elseif $_product.Product2Id.Family == "Family6"}
    TEXT
    {/if}{/if}{/if}{/if}{/if}{/if}

  • Avatar
    Karen Spence

    Hi Darius - thank you. OK, this is why I only had one close tag initially. Every time I upload the document with as many close tags as I have open tags, it tells me there are too many, so I am repeatedly instructed to remove one until there is only one. Something must be wrong with this format. I'm afraid it's too simple.

    Edited by Karen Spence
  • Avatar
    Darius Grimes

    They both do the same thing just depends on how you want to format the finished product. CORRECTION: You can use just one {/if} for the {elseif} statements but you may want each to be an independent {if} statement then just combine the results. I should have added that my example would not use the {elseif}

    {if $_product.Product2Id.Family == "Family1"}
    TEXT {/if}
    {if $_product.Product2Id.Family == "Family2"}
    TEXT {/if}
    {if $_product.Product2Id.Family == "Family3"}
    TEXT {/if}
    {if $_product.Product2Id.Family == "Family4"}
    TEXT {/if}
    {if $_product.Product2Id.Family == "Family5"}
    TEXT {/if}
    {if $_product.Product2Id.Family == "Family6"}
    TEXT
    {/if}

  • Avatar
    Darius Grimes

    Also, {if $_product.Product2Id.Family ==" Family1 Family2 Family6"} this will not work since == is literal. You would need to define every possible combination in your statement in order for it to work.

  • Avatar
    Karen Spence

    I see. Ouch! Oh, well. I'm determined to make this work, so here goes. Thank you all!

  • Avatar
    Jeremy Clarke

    @Darius - Karen's first example was actually correct. You don't need {/if} for every one when you're using {elseif}. This is correct:

    {if $_product.Product2Id.Family == "Family1"}

    TEXT

    {elseif $_product.Product2Id.Family == "Family2"}

    TEXT

    {elseif $_product.Product2Id.Family == "Family3"}

    TEXT

    {elseif $_product.Product2Id.Family == "Family4"}

    TEXT

    {elseif $_product.Product2Id.Family == "Family5"}

    TEXT

    {elseif $_product.Product2Id.Family == "Family6"}

    TEXT

    {/if}

    -----------------------------------------

    If you want to search for multiple families you can do this:

    {if $_product.Product2Id.Family == "Family1" or $_product.Product2Id.Family == "Family2" or $_product.Product2Id.Family == "Family6"}
    ....
    {/if}

  • Avatar
    Karen Spence

    Thank you, Jeremy. So the above syntax will return multiple paragraphs of text if there are products from multiple families, is that correct? My test document isn't working that way. I have 5 products in my test opportunity, one for each Product Family minus 1, but my result displays only text from one of the Product Families, the last product family text listed in my template.

    So, for testing purposes, I removed that product from the opportunity, and now it's only displaying the previous product family text. I think I do need to consider every scenario, or something more than what I have.

    Edited by Karen Spence
  • Avatar
    Jeremy Clarke

    Hi Karen,

    Is the family text going inside the loop of your products? If not, we'll have to take a different approach like this:

    Inside your loop put this: {$_families[] = $_product.Product2Id.Family}

    Then your if statements would look like this:

    {if in_array("Family1", $_families)}

    TEXT

    {elseif in_array("Family2", $_families)}

    TEXT

    {elseif in_array("Family3", $_families)}

    TEXT

    {elseif in_array("Family4", $_families)}

    TEXT

    {elseif in_array("Family5", $_families)}

    TEXT

    {elseif in_array("Family6", $_families)}

    TEXT

    {/if}

Please sign in to leave a comment.
Powered by Zendesk