# Built-in String.{format,template}

**URL:** <https://es.discourse.group/t/built-in-string-format-template/887>\
**Category:** 💡 Ideas\
**Created:** [July 25, 2021, 4:15pm UTC](https://es.discourse.group/t/built-in-string-format-template/887 "2021-07-25T16:15:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![septs](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/septs/32/395_2.png) [@septs](https://es.discourse.group/u/septs)\
**Post date:** [July 25, 2021, 4:15pm UTC](https://es.discourse.group/t/built-in-string-format-template/887/1 "2021-07-25T16:15:18Z")

</div>

improve string class add format and template method  
provide built-in format string function

```javascript
const userDefined = "foo${bar}" // the syntax is consistent with `template string`

const formatted = String.format(userDefined, { bar: "123" })
formatted // returns "foo123"

const tpl = String.template(userDefined)
tpl({ bar: "123" }) // returns "foo123"

```

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [July 25, 2021, 4:53pm UTC](https://es.discourse.group/t/built-in-string-format-template/887/2 "2021-07-25T16:53:43Z")

</div>

Template literals already solve that use case:

```javascript
const userDefined = ({bar}) => `foo${bar}`;

const formatted = userDefined({bar: "123"});

```

Sure, `userDefined` doesn't come from a string, and cannot be transported as data, but there are many templating libraries with different features available in userland already to solve this with either `eval` (very powerful, but also a security issue) or fixed pattern grammars (more secure, but more restricted, and very heterogenous). I do not believe there is one golden solution that would fit all requirements and be useful as a builtin.
