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

75 Comments

  • Avatar
    Jeremy Clarke

    Hi Eve,

    Try using empty() instead of == "" -- like this:

    {if $Question == ’Mum Then Children’ && !empty($Name_of_Mum) && !empty($Name_Child1) && empty($Name_Child2)}
    ....
    ....
    ....
    {/if}

  • Avatar
    Conor Malloy

    Can you have it display text if a field is not empty? For example - I have placeholders for 5 people. But if the fifth field is empty (or 2 through 5 for that matter), then don't display or do display a comma between the names.

  • Avatar
    Jeremy Clarke

    Hi Conor,

    Yes, like this:

    {$person1}{if !empty($person2)}, {$person2}{/if}{if !empty($person3)}, {$person3}{/if}{if !empty($person4)}, {$person4}{/if}{if !empty($person5)}, {$person5}{/if}

  • Avatar
    Eve Cross

    Can I assign a conditional logic value?

    E.g
    {assign var=$Fullname_Client Value=$Firstname_Client, {if !empty($Middlename_Client){/if}, $Lastname_Client}

    input variable of {$Fullname_Client}

    or am I better off using {$Firstname_Client}{if !empty($Middlename_Client)} {$Middlename_Client} {$Lastname_Client}{/if} in each instance?

    Names repeat a lot in my current document.
    - Cheers, Eve

  • Avatar
    Conor Malloy

    Thanks Jeremy!

  • Avatar
    Jeremy Clarke

    Hi Eve,

    I would actually use the Field Map (https://support.webmerge.me/hc/en-us/articles/206526086-Field-Map), then do this under your Fullname_Client field:

    {$Firstname_Client}{if !empty($Middlename_Client)} {$Middlename_Client} {$Lastname_Client}{/if}

    The Field Map will create that variable before the data is sent to the document, then you can use it everywhere without having an extra code in your doc.

    Thanks!

  • Avatar
    Israel Zeevi

    Hello,

    How can I use the conditional logic to a PDF to include or exclude specific pages?
    It's completely clear for me how to use with a word document, but with a PDF I'm not sure how to do it.

    Best,
    Israel

    Edited by Israel Zeevi
  • Avatar
    Jeremy Clarke

    Hi Israel,

    Unfortunately, the only option with fillable PDFs it to break them up into separate templates, then use Data Routing (https://support.webmerge.me/hc/en-us/articles/206527136-Combine-Multiple-PDF-Documents) to combine them into a single PDF (with logic)

    Thanks!

  • Avatar
    Mohammad Irfan Khan

    Hello!
    I want to print certificates where the name has 3 parts:
    {$FirstName} {$MiddleName} {$LastName}.
    Some students don't have any middle name. In that case there is an extra space between the first and last name. How can I solve that issue?
    Please help me out!

  • Avatar
    Jeremy Clarke

    Hi Mohammad,

    You can do this: {$FirstName}{if !empty($MiddleName)} {$MiddleName}{/if} {$LastName}

  • Avatar
    Rajasekhar VK

    Hi Jeremy

    How do I compare two or three dates to check if they are the same, and then use them only once if they are the same?

    {$date1}, {$date2}, and {$date3}
    For example, in respect of three companies, if there are three different dates for board meetings, I want all three dates to appear. If not, I should be able to say, "at the respective meetings of the boards of the three companies held on the same day, i.e., any one date, say, date1.

    Kindly help.

  • Avatar
    Jeremy Clarke

    Hi Rajesekhar,

    You can do something like this:

    {if $date2 != $date1}, meeting #2: {$date2}{/if}{if $date3 != $date2}, meeting #3: {$date3}{/if}

  • Avatar
    Tech Flads

    I want to put in a conditional for a range of numbers, something like:

    [If $Answer1 = 0-3 then ABC]
    [If $Answer1 = 4-6 then DEF]
    [If $Answer1 = 7-8 then GHI]
    [If $Answer1 = 9-10 then JKL]
    [If empty($Question1) then N/A]

    What would be the best way to go about this?

  • Avatar
    Jeremy Clarke

    Hi Tech,

    You'll want to do something like this:

    {if $Answer1 >= 0 && $Answer <= 3}ABC{elseif $Answer1 >= 4 && $Answer <= 6}DEF{elseif $Answer1 >= 7 && $Answer <= 8}GHI{elseif $Answer1 >= 9 && $Answer <= 10}JKL{elseif empty($Question1)}N/A{/if}

  • Avatar
    Alan Hoffman

    Excellent feature... I've been using WebMerge for a while and stumbled across this - nice work...

  • Avatar
    Andres Cuestas

    Can i use this in a field map? i need if the field is empty write "No Aplica" what will be the sintaxis.

  • Avatar
    Darius Grimes

    one questions, if you use the conditional logic in the field map the text area is small and to proof what you have written you have to use the cursor to scroll through it. Wouldn't it be better in these cases to use the source code area to do this? Or, is there a future enhancement that allows the filed map text area to be expanded like this comment field?

  • Avatar
    Jeremy Clarke

    @Andres - Yes, you can use conditional logic in the field map

    @Darius - We'll look at improving this :)

  • Avatar
    Darius Grimes

    I knew you could do this is the field map it just presents challenges on debugging it. I generally design it in excel then copy and paste it into the field map. Allowing the field to be expanded would be a great enhancement though, thanks Jeremy!

    Edited by Darius Grimes
  • Avatar
    Pieter Van Der Hoeven

    Hi Jeremy, I'm trying to do the following, but for some reason, I'm getting random results:
    {if $InstallType == "6" or "20"}X ({$Currency} X)
    {elseif $InstallType == "5"}Y ({$Currency} Y)
    {elseif $InstallType == "11" or "21"}Z ({$Currency} Z)
    {else}XX ({$Currency} XX){/if}.

    What am I doing wrong?
    Thanks!
    Pieter

  • Avatar
    Jeremy Clarke

    Hi Pieter,

    When using the "or" you need to add the merge field again like this:

    {if $InstallType == "6" or $InstallType == "20"}X ({$Currency} X)
    {elseif $InstallType == "5"}Y ({$Currency} Y)
    {elseif $InstallType == "11" or $InstallType == "21"}Z ({$Currency} Z)
    {else}XX ({$Currency} XX){/if}.

  • Avatar
    Pieter Van Der Hoeven

    Ah, I see. Thanks, very helpful!

  • Avatar
    Prue McCutcheon

    Hi Jeremy,
    Is there a way to hide blank page that would otherwise be populated if logic called for it?
    Example If an additional clause on a contract is optional, how do I avoid the blank page it leaves behind and warping the formatting of the document?
    I am writing it as below example

    {if $approved_info_tech==YES}}

    "appoved tech info terms"

    {/if}

  • Avatar
    Jeremy Clarke

    Hi Prue,

    Yes, you'll start the if statement on the first page, then add a page break like this:

    PAGE 1
    {if $approved_info_tech == "YES"}
    PAGE BREAK
    PAGE 2
    {/if}
    PAGE BREAK
    PAGE 3

  • Avatar
    Prue McCutcheon

    Hey Jeremy
    Thankyou!
    How would this work for removing table rows with the same statement, if statement isn't met.
    {if $approved_info_tech == "YES"}

  • Avatar
    Jeremy Clarke

    Hi Prue,

    You will use {tableif} instead like this:

    {tableif $approved_info_tech == "YES"} ........ {/tableif}

  • Avatar
    Prue McCutcheon

    Hello!

    Is there a way to DELETE the blank pages this will leave behind?

    I currently have multiple clauses that are optional, but if they aren't identified as applicable to the contract they don't appear, but they leave a blank page. This means their could be multiple blank pages between clauses.

    I have included the formatting as follows

    {if $approved_info_tech==YES}
    INSERT PAGE BREAK
    "info text clause info"
    INSERT PAGE BREAK
    {/if}

    can you please advise if deleting the blank pages is possible?

  • Avatar
    Jeremy Clarke

    Hi Prue,

    As long as your page breaks are on the inside of your if statements, it will not show the blank pages. If you're having trouble with a specific template, please create a support ticket and we'll help you there.

    Thanks!

  • Avatar
    Darius Grimes

    How about {if} "And" "Or" arguments?

  • Avatar
    Darius Grimes

    Could I use this to show a photo if true? Where 0 = yes and 1 = no

    '{if $_record.poolslide_yn == 0 and $_record.App_poolslide == 1} Pool Slide Present {/if}'

    Edited by Darius Grimes
Please sign in to leave a comment.
Powered by Zendesk