Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In handlebars, how can I escape special characters in field names?

For example I have an object like this:

{ '#SKU_NBR': '123' }

And I want to print out the value but the simple syntax for this doesn't work:

<div>{{#SKU_NBR}}<div>

How can I escape the name of the field such that I can use it in a template expression in handlebars?

like image 839
justin.m.chase Avatar asked Oct 23 '25 18:10

justin.m.chase


2 Answers

To elaborate on the answer from bigless, you can use any of these:

  • {{ "#SKU_NBR" }}
  • {{ '#SKU_NBR' }}
  • {{ [#SKU_NBR] }}

These resolve the variable named within the escape characters (as opposed to just including the String #SKU_NBR).

I had a similar problem because I had variables named this and with. I found {{[this]}} worked as expected, but {{[with]}} did not. I was able to work around this by using {{this.with}}

like image 151
Matt Sgarlata Avatar answered Oct 26 '25 09:10

Matt Sgarlata


You can use {{[@special-chars]}}.

Example: {{[#sku_no]}}

Refer to working example in handlebar tryit editor: https://handlebarsjs.com/playground.html#format=1&currentExample=%7B%22template%22%3A%22%7B%7B%5B%23SKU_NBR%5D%7D%7D%22%2C%22partials%22%3A%5B%5D%2C%22input%22%3A%22%7B%5Cn%20%20%5C%22%23SKU_NBR%5C%22%3A%20123456%5Cn%7D%5Cn%22%2C%22output%22%3A%22123456%22%2C%22preparationScript%22%3A%22%5Cn%22%2C%22handlebarsVersion%22%3A%224.7.8%22%7D

like image 33
satyajit nayak Avatar answered Oct 26 '25 08:10

satyajit nayak



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!