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"

 

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}

 

Have more questions? Submit a request

36 Comments

  • Avatar
    Darius Grimes

    Would this have to be inserted into the HTML code or is there a method of  inserting it  in a field conditional statement within the document builder?

  • Avatar
    Jeremy Clarke

    Hi Darius,

    You can add (type) it directly into the document builder.  You can also use the html view if you need more advanced formatting to go along with it.

    Thanks!

  • Avatar
    Darius Grimes

    okay what is the secret to getting tables to display correctly, they just jumble up all on the left side of the document and loose formatting from the document builder

  • Avatar
    Jeremy Clarke

    Hi Darius,

    Please submit a support request for your specific issue.

    Thanks!

  • Avatar
    Nur Husnidha Hussain

    When you mentioned Smarty, is it mean all syntax in Smarty can be applied in WebMerge?

  • Avatar
    Jeremy Clarke

    Yes, "most" of the Smarty syntax can be applied. We block a few functions for security reasons.

  • Avatar
    Alliance Theatre Staff

    How do I create an "if" statement for a check box field? ie It has several choices, but I only want to return the value if a specific one is chosen. Equal doesn't work, because multiple values could be chosen.

  • Avatar
    Jeremy Clarke

    @Alliance - that depends on how the checkbox data is being sent over to WebMerge. If it is an array, then you can use {if in_array('Red', $FavoriteColor)}, but if it is a comma separated list, then you can search for the string like: {if strstr($FavoriteColor, 'Red')}

    Please submit a support ticket for more specific assistance :)

  • Avatar
    Han Zuyderwijk

    Will conditional sections work with the Word Docx Documents?

  • Avatar
    Jeremy Clarke

    Hi Han,

    Yes, you can use the if statements in Word templates as well :)

  • Avatar
    Maciej Ratajczak

    Hi Jeremy, could you please tell me how to get rid of line breaks? I have an list in Word document and I don't want to have white spaces. Zapier makes a list for me in the document using Webmerge but if some elements are missing there are white spaces between list elements. I need one element right underneath another.

  • Avatar
    Jeremy Clarke

    Hi Maciej,

    You'll need to start/end the if statements on the same line so that the new line characters are on the inside of the if statements. Like this:

    {if !empty($Name)}
    Name: {$Name}{/if}{if !empty($Phone)}
    Phone: {$Phone}{/if}{if !empty($Address)}
    Address: {$Address}{/if}

  • Avatar
    Maciej Ratajczak

    Wow! that was easy! :) Thanks man!

  • Avatar
    Chameleon Support

    Hi Jeremy,

    how are the if statements handled with in excel?
    basically i have a sumary page (worksheet 1) in excel that i need to template from another worksheet (worksheet 2)
    In my head its quite complex
    i have in this worksheet 2, a total of 51 rows( row 4 to row 54), with 58 columns (H to BM)

    Do i need to format each row on the worksheet 1? i didnt want 45 empty rows visible if only 5 contain data.

  • Avatar
    Jeremy Clarke

    Hello,

    Unfortunately, there isn't a way to "hide" rows in excel if the data is empty :(

    Could you use a Word document instead with a table? You could then hide the empty rows in the table. Please create a ticket for more specific help.

    Thanks!

  • Avatar
    Aaron Shapiro

    Is there a way to check the length of an array or to see if a field is an array? Working with Podio, some of the fields can be arrays or strings, it depends on how much data is coming back. If I have 1 single related entity, it looks like it comes back as a string. But if I have multiple related entities it looks like it's coming back as an array.

  • Avatar
    Jeremy Clarke

    Hi Aaron,

    You could do something like this to check if it is an array:

    {if is_array($emails)}
    {implode(', ', $emails)}
    {else}
    {$emails}
    {/if}

  • Avatar
    Aaron Shapiro

    Jeremy,
    Thanks for the timely reply. Unfortunately this didn't solve my problem because one way or another it's returning an array. I'm trying to create a "brief" that has a bunch of relationships in it. Essentially that's all the brief really is. I have it working just fine if there are multiple selections for each relationship, but if a relationship only has a single reference, it looks like a mess.

    {if !empty($stakeholders)}
    Project Stakeholders
    {foreach from=$stakeholders item=_row}
    {$_row.title}, {$_row.role}, {$_row.email}
    {/foreach}
    {/if}

    This works fine if there are 0 or more than 1 stakeholder but if there is only one stakeholder, it only shows the first letter of each of those fields. I really don't know if this is an issue with my code, with Podio's API or with WebMerge... But maybe you can help me out.

  • Avatar
    Jeremy Clarke

    Try this (it looks to see if $stakeholders is a list of items, if not then it creates a list):

    {if !empty($stakeholders)}
    Project Stakeholders
    {if empty($stakeholders[0])}{$stakeholders = array($stakeholders)}{/if}
    {foreach from=$stakeholders item=_row}
    {$_row.title}, {$_row.role}, {$_row.email}
    {/foreach}
    {/if}

  • Avatar
    Aaron Shapiro

    That worked! Thank you!

  • Avatar
    Rick Davis

    Is there a way to include a "contains" in the "if statement. For example, {If $Court CONTAINS "Kansas"} {$text} {/if}. Then the conditional text would appear anytime "Kansas" appears in the $Court filed, but not if it contains something different e.g. Missouri. This would be useful because I create documents for various courts in Kansas and Missouri, but certain rules apply to each state. I currently have a separate state field that serves no other purpose than for the document automation.

  • Avatar
    Jeremy Clarke

    Hi Rick,

    Yes! Like this: {if strstr($Court, "Kansas")}{$text}{/if}

  • Avatar
    Rick Davis

    That is perfect. Thank you!

  • Avatar
    Gerard Groarke

    I've created a multi page document but only want certain pages to show depending on customer selections. "If customer selects A, use the submitted data in Page 1 and omit Page 2. If customer selects B, use the submitted data in Page 2 and omit Page 1."

    I have tried the following method:-
    (i) At the beginning of Page 1 - {if $Selection == 'A'}
    (ii) Page 1 then follows (and includes many 'if' clauses)
    (iii) At end of Page 1 - {/if} (to close the opening statement)

    I keep getting an XML parsing error, and I cannot figure it out. Details of the parsing error are:-

    Xml parsing error

    Location: Part: /word/document.xml, Line: 3, Column: 3

    Any help would be much appreciated

  • Avatar
    Jeremy Clarke

    Hi Gerard,

    Please create a support ticket and send us your template so we can take a look!

    Thanks,

  • Avatar
    Chris Lusk

    Absolutely perfect solution! I am simply in awe of how flexible this program is. Your service is amazing!

    My only question is how do I ADD a hard return? For example, we use number lists in our contracts. Number 1 to 8 are hard coded, nothing variable. But for line item 9 and 10, we might have info. If the paragraph field that would populate number 9 does not exist, how would I write it to show number 10 taking its place.

    Sorry if this makes no sense, I am sleep deprived and still ecstatic about finding your service!

     

    EDIT: I re-read this and don't think it makes sense :)  Where would I put the code for line item 9?  In the same line as number 8?  If yes, how do I generate the hard return to produce the number "9" in the list?

    Edited by Chris Lusk
  • Avatar
    Jeremy Clarke

    Hi Chris,

    You would do something like this:

    7. Red blah blah
    8. Green blah blah {if !empty($FavoriteColor)}
    9. Fav Color: {$FavoriteColor}{/if}{if !empty($FavColor2)}
    10. Fav Color 2: {$FavColor2}{/if}

    Thanks!

  • Avatar
    Rodrigo Rodríguez

    Hi I want to show a table within a multipage document if a {$...} it's not empty, what I'm doing is to use this code:

    {if !empty($name)}
    Table
    {/if}

    The thing is that when I make the test within webmerge the test works fine but as I'm merging the data from a form of Formstack something happens that doesn't work.

    Can you give me advice on this?

  • Avatar
    Jeremy Clarke

    @Rodrigo,

    It sounds like $name isn't being sent over from Formstack. Please turn on Debug Mode (https://support.webmerge.me/hc/en-us/articles/206526126-Debug-Mode) and make sure all the data is coming over.

    If you still have troubles, please submit a support ticket and we'll help you from there.

    Thanks!

  • Avatar
    Eve Cross

    Howdy,

    I'm having some trouble with text display within {/if} statement blocks.

    I'm attempting to do something along the lines of:

    {if $Question == ’Mum Then Children’ && $Name_of_Mum != ” “ && $Name_Child1 != ” “ && $Name_Child2 == ” “}
    {$Name_of_Mum} Descriptive text
    {$Name_Child1} Descriptive text

    {elseif $Question == ’Mum Then Children’ && $Name_of_Mum != ” “ && $Name_Child1 != ” “ && $Name_Child2 != ” “}
    {$Name_of_Mum} Descriptive text
    {$Name_Child1} Descriptive text
    {$Name_Child2} Descriptive text {/if}

    I've been trying a few things but i'm either getting no output at all or just the corresponding variables but no descriptive text.

Please sign in to leave a comment.
Powered by Zendesk