How to replace/substitute placeholders with values in a string using either reduce or forEach?

Certified Senior Developer

Hi All,

We have small requirement like need to replace/substitute the places holders in string with values.

a!localVariables(
  local!template: "Hello, {name}! Welcome to {place}.",
  local!placeholders: {
    "name": "John",
    "place": "Appian"
  },

  a!forEach(
    items: a!keys(local!placeholders),
    expression: a!localVariables(
      local!placeholder: fv!item,
      local!value: local!placeholders[local!placeholder],
      local!template: substitute(
        local!template,
        "{" & local!placeholder & "}",
        local!value
      ),
      local!template
    )
  )
)

which returns the following out put like 

List of Text String - 2 items

    • "Hello, John! Welcome to {place}."(Text)
        • "Hello, {name}! Welcome to Appian."(Text)

        Need to replaced all of them in single string with some sequential manner.

        Your expertise is helpful to solve.

        Thanks in advance.

          Discussion posts and replies are publicly visible

        Parents Reply
        • 0
          Certified Senior Developer
          in reply to Konduru Chaitanya

          In one of our use case we are having this requirement. like one of the interface will provide the paragraph with some place holders while generating the report interface we have to replace those place holders with actuals. So he are trying to write one expression rule with Input as placeholders string and outcome is replaced content/string like.

          Input String :"Hello, {name}! Welcome to {place}.",
          Placeholders: {
          "name": "John",
          "place": "Appian"
          },

          Output result is :"Hello, John! Welcome to Appian."

        Children
        No Data