1

I want to add if condition in email. I want to check if field value is != empty then display data

I used below code but not working

{{if data.comment_data != ''}}
<tr>
<td><b>{{trans "Message"}}</b></td>
        <td>{{var data.comment_data}}</td>
</tr>
{{/if}} 

2 Answers 2

1

You can not use if condition like that in email template. You can only use like below:

{{if data.comment_data}}
    <tr>
        <td><b>{{trans "Message"}}</b></td>
        <td>{{var data.comment_data}}</td>
    </tr>
{{/if}}

There is an alternative way availalble to include condition base html in the email template. You can insert your custom phtml file in email template like below:

<tr>
     <td>
          {{block class='Magento\\Framework\\View\\Element\\Template' area='frontend' template='Vendor_Module::email/modulename/comment.phtml' order=$order}}
     </td>
</tr>

In this phtml file you can write condition base html and that will be populated in the email.

Magento2 has also done like this in below email template file that you can take as a reference:

vendor/magento/module-sales/view/frontend/email/shipment_new.html

Hope this help to you. Let me know if you have any other queries.

Thanks,

0

You missed $ sign with data variable.

Replace your code with below code:

{{if $data.comment_data != ''}}
<tr>
<td><b>{{trans "Message"}}</b></td>
        <td>{{var data.comment_data}}</td>
</tr>
{{/if}} 

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.