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".


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



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:


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)}


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


{$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}
Was this article helpful?
14 out of 19 found this helpful


  • I would be happy if all the opportunity products were listed in the table, and then subsequent to or outside of that, there would be paragraphs of text with headers for each Product Family/Type.

    Right now, we have 25 templates for each product or series of products and if sales needs to create an opportunity with more than one product, which is often, they have to clone the opportunity to create as many as there are products. The previous template creator just used text fields on the opportunity to do this and a pick list field for the product selected. People aren't using Webmerge as a result.

    So this won't be fancy, but as a first iteration, the goal is to bring all products into one one template. I tried the above, and I'm obviously doing something wrong, because I'm getting a 'Syntax error in template "string:{" - Unexpected "

    The first Cell is: 

    {tablerow from {$_families[] = $_product.Product2Id.Family} |replace:$OppName:””}

    Then each section begins with a variation of the following unless it's all the subsequent tags, which start with ifelse:

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


  • Hi Karen,

    In the first cell, it should be something like this:

    {tablerow from=$Products item=_product}{$_families[] = $_product.Product2Id.Family}{$_product.ProductId2.Name|replace:$OppName:””}

    Then your other parts need to be:

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

  • Thanks, Jeremy. I think I'll stick with the previous way to do it. This way isn't listing any products, only prices.And only one family's text.

  • All of the if/else examples in the documentation are about showing / hiding specific fields or lines in a document.

    I have two completely different versions of the same HTML that I want to send to people depending upon the value in one field.

    I wrote what I think is the correct if/else statements for this, but it is not working correctly. I'm not sure if this is because my syntax is wrong, or if it's because the functionality doesn't support multi-paragraph HTML.

    Below is what I tried. The first seven paragraphs are the email that we normally send out. I wrapped one if statement around that email, telling it to use that text in cases where a specific field is blank. The last five paragraphs are the new email that I would like to send only in cases where a specific field is populated, and I put an if statement around them, too.

    I tested this for both conditions (with the field blank, and the field populated), and the html email that I received in both cases displays the full text of both the first seven paragraphs and the last five paragraphs.

    Is the problem with the syntax? Or is the problem that you can't put large blocks of html inside of an if statement?

    {if !empty($tfa_4745)}

    {if $tfa_4074=="Teen"}Thank you for registering for the {$tfa_4334} weekend retreat!{elseif $tfa_4074=="KSF"}Thank you for registering as a {$tfa_4334} weekend retreat intern!{else}Thank you for registering as a {$tfa_4334} weekend retreat staff member!{/if}

    We can't wait to see you at {$tfa_4344} in {$tfa_4380}!

    Your {if $tfa_4074=="Teen" && $tfa_2711!="0"}registration receipt and {/if}signed permissions are attached. Please save them for your records.

    {if $tfa_4074=="Teen" && $tfa_2711=="0"}You have opted out of the registration fee. We strive to make the weekend retreat as accessible as possible, and we are grateful to be able to accept a limited number of teens for whom the weekend might otherwise be cost prohibitive.

    {/if} To finalize your registration, please review the Travel Guidelines and complete our travel form no later than {$tfa_4390} It is important you fill out this form promptly so we can make necessary arrangements for the weekend, including group travel from the airports. Your travel form link is unique to you. Please do not forward this message.

    If you have questions, visit our Frequently Asked Questions and packing list pages or email

    For recent news, follow us on Facebook (Sample and Sample Teens), on Twitter, and on Instagram!


    {if $tfa_4745 != ''}

    We have received your waitlist submission for the Post High School Weekend Retreat. We are currently at capacity for this program, but additional spots may open up soon.

    We will contact waitlist applicants on a first come basis if and as additional spots open up. It is our goal to offer the weekend retreat experience to as many participants as possible and will do our best to provide additional spots. However, we are currently at capacity and cannot guarantee additional space at this time.

    If you have questions, please email, or contact our Youth Programs Associate, at (555) 555-5555.

    For recent news, follow us on Facebook (Sample and Sample Teens), on Twitter, and on Instagram!

    We look forward to being in touch soon.


  • Note: it looks like the html code that I put in place in my last post got rendered in the post instead of showing up as code.

  • Hi Leah,

    The issue is that your if statements actually say the same thing. This first one:

    {if !empty($tfa_4745)}

    Is read as "if not empty"

    And your 2nd one:

    {if $tfa_4745 != ''}

    Is read as "if not equal to blank"

    So they do the exact same thing. I recommend this instead:

    {if !empty($tfa_4745)}
    HTML 1
    HTML 2

    That should work!

  • want to hide only the values in a table that are the number Zero. i have tried other functions that end up blanking the entire row or column. thanks!!


    Name day 1 hours day 2 hours day 3 hours
    davie 3 7
    gene 6
    marcy 5 10

  • Hi Jonathan,

    Thank you for reaching out!
    This should work for you: {if !empty($FieldName)}{$FieldName}{/if}. That way if the field is blank/ zero, it should leave it out versus an entire line. Could you please try that in your template? Thank you!

  • hi Christina, i thought through what is happening in my cell and i believe i need to use the string function.
    in plain english, if the string in cell ~~ is "0.0 / 0.0" then display blank.

  • Hello Jonathan,

    Thank you for the response!
    I suggest either using {if $field != "0.0"} or {if $field > 0}.
    Please let me know if this solution works for you!

  • Hello all - Just wanted to follow up and thank Christina for giving the gauge that worked for me. Exactly like Christina said on Tuesday, the following syntax worked to show only non-zero data in my repeatable / loop table:

    {if !empty($fieldname)}{$fieldname}{/if}

    I found the reason it wasn't working before - I was passing data from Knack from fields that were (in Knack jargon), a Text Formula. In other words, it's neither a string nor arithmetic. So i went back and used a table I had that was ALL in actual arithmetic, and the WebMerge syntax worked there.

  • Is it possible to hide a column in a table if its empty?

  • Hi Jonathan,

    Unfortunately, the only option is to use multiple tables that have different columns, then you can show/hide the correct table based on if/else logic.

  • I've got a weird one that has me ripping out my hair. I have a form in which I want to show a "see attached sheet" instead of the fourth kid if they have a fifth kid. Seems like just a {if !empty} or {if empty} or {if !="}... but none of those are working. And then I'm thinking... the field isn't coming through at all from my data set if the kid doesn't exist. In my dataset coming in, it's not that I have a fifth kid that's empty, it's that I'm not even passing the variable that would contain the fifth kid. It's passing data from a custom conditional in Drupal than could create infinite kids, but only passes the ones the user has actually created.

    Is it not recognizing the field empty because it doesn't exist? Is there some way around that?

  • Hi Laura,

    If the variable doesn't exist, it treats is as empty. So you would want to do something like:

    {if !empty($Kid5)}

  • Hi Jeremy, I am trying to create a summary list at the end of my pdf document. I wanted to avoid having empty lines if an item doesn't apply. But my i keep getting a message that I have an unclosed if tag but i cannot spot it ;-). Can you?

    {if $EffectiveCommunication1 == "Needs work"} IMPROVE YOUR COMMUNICATION EFFECTIVENESS{/if} {if !empty($TimeManagement1)}
    {if $TimeManagement1 == "Needs work"} IMPROVE YOUR TIME MANAGEMENT{/if} {if !empty($Delegation1)}
    {if $Delegation1 == "Needs work"} MASTER THE ART OF DELEGATION{/if} {if !empty($Motivate1)}
    {if $Motivate1 == "Needs work"} LEARN HOW TO MOTIVATE & ENGAGE YOUR TEAM{/if}

  • Hi!

    I'm trying to add multiple conditions to a document:
    {if $kitchen != “NA”}
    ● {$kitchen}
    ● Notes: {$kitchenother}{/if}
    {if $bathroom != “NA”}
    ● {$bathroom}
    ● Notes: {$bathroomother}{/if}
    {if $bedroom != “NA”}

    However, I'm getting a bunch of blank lines in between sections. Is there a way to avoid this?

  • Hello Conor,
    I apologize for the delayed reply.
    You can do this one quick trick to help remove any blank lines in your document. You'll first need to wrap each conditional line in an IF statement. Also, you'll want to start/end the IF statements on the same line, like the example below, and put the "new line" inside the IF statement (so it only prints when the IF statement is true).

    {$Phone_Number}{if $Cell_Phone}


    We also have a helpful article on including Conditional Sections that I'll include below for reference.

    If you are still having issues with your template, please let us know and we can open a ticket to explore your issue further.

    Thank you!

  • Thanks for sharing information



Please sign in to leave a comment.