Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a string contains substring with Smarty

I'm having a bit of a problem with my email-templates. Shopware for some reason uses Smarty as a template language for their emails. My Problem now is that I have this email-template


Tracking: 

  {if $sDispatch.name == "DHL national" }
      https://sampleurl.com={$sOrder.trackingcode}
  {else if $sDispatch.name == "DPD"}
      https://sampleurl.com={$sOrder.trackingcode}
  {else if $Dispatch.name == "Deutsche Post"}
      http://sampleurl.com
  {else}
      Your order can not be tracked.
  {/if}

And there are many dispatch options for the parcels (small parcel, with insurance ...). Now I want to check whether the String in $sDispatch.name includes "DHL", "DPD" or "Deutsche Post" to determine which tracking link should be sent to the customer.

I could not find a reasonable solution to check if a String contains these pieces of given substrings. Does anyone have a neat little solution that will do the trick? Anything is appreciated! :-)

like image 292
tombjarne Avatar asked Oct 17 '25 17:10

tombjarne


1 Answers

You can use the modifier |strstr to search for a certain string in a string.

https://www.php.net/manual/de/function.strstr.php

Example:

{if $sDispatch.name|strstr:"DHL"}This is a dispatch that contains the string DHL{/if}
like image 117
mnaczenski Avatar answered Oct 20 '25 06:10

mnaczenski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!