Skip to contents

Replaces placeholders of the form `{{key}}` in a template string with corresponding values from a named list.

Usage

replace_placeholders(template_string, values_list)

Arguments

template_string

A character string containing placeholders.

values_list

A named list where names correspond to placeholders in the template string (without the curly braces).

Value

A character string with placeholders replaced by their values. If a placeholder is not found in `values_list` or its value is `NULL`, it's replaced with an empty string.

Examples

replace_placeholders("Hello, {{name}}! Today is {{day}}.",
                     list(name = "Alice", day = "Monday"))
#> [1] "Hello, Alice! Today is Monday."
replace_placeholders("Topic: {{topic}}, Question: {{question}}",
                     list(topic = "AI Ethics")) # {{question}} becomes ""
#> [1] "Topic: AI Ethics, Question: "