Search code examples
pythonstringcoffeescriptstring-interpolationlanguage-comparisons

python-like interpolation of string in coffeescript


I have such namespace with errors

errorMessages=
  error404Find: "Hosts not found"
  error404FindByHost: "Host - #{hostName} - not found"
  error400: "No host"

hostName should be assigned to null and overrided in function like:

this.hostName = 'smth'

or can I use something like in python:

errorMessages=
  {'error404Find': "Hosts not found"
  'error404FindByHost': "Host - {hostName} - not found"
  'error400': "No host"}
errorMessages['error404FindByHost'].format(hostName='smth')

Solution

  • You can make all those entries a functions, then apply a dictionary to them with arguments:

    errorMessages=
      error404Find:-> "Hosts not found"
      error404FindByHost:-> "Host - #{@hostName} - not found"
      error400:-> "No host"
    
    alert errorMessages["error404FindByHost"].apply hostName:"host1"