# Using Aurelia for string templating?

**URL:** <https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179>\
**Category:** Help Requests\
**Created:** [January 22, 2020, 2:37pm UTC](https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179 "2020-01-22T14:37:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [January 22, 2020, 2:37pm UTC](https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179/1 "2020-01-22T14:37:10Z")

</div>

I’d like to create a value converter that would work something like this.

```
<span textcontent.bind="'My name is ${placeholder}.' | template:{ placeholder: 'Dan' }"></span>

```

Results in:

```
<span>My name is Dan</span>

```

Obviously both constants `'My name is ${placeholder}.'` and `'Dan'` could be dynamic values, while the name `placeholder` is known.

Are there APIs available in Aurelia that I could use within the ValueConverter to perform the string replacement logic?

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [January 23, 2020, 5:08am UTC](https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179/2 "2020-01-23T05:08:34Z")

</div>

How about this [https://codesandbox.io/s/aurelia-sandbox-btcxw](https://codesandbox.io/s/aurelia-sandbox-btcxw)? (Look into app.js)

You might as well look into the Aurelia i18n plugin since it seems to be somewhat the thing you want to achieve

---

<div class="post-metadata">

**Author:** ![Sayan751](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sayan751/32/1861_2.png) [@Sayan751](https://discourse.aurelia.io/u/Sayan751)\
**Post date:** [January 23, 2020, 5:26am UTC](https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179/3 "2020-01-23T05:26:15Z")

</div>

Obviously I am biased. This seems to me a perfect use case for i18n plugin. If there is a key describing the template, the placeholder in the template can be replaced by providing parameters. Check this [here](https://aurelia.io/docs/plugins/i18n#using-the-plugin), search for ‘Passing parameters to the attribute’.

In v2 I believe you can evaluate any random expression. There i18n wouldn’t be needed, and a simple value converter should suffice.

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [January 23, 2020, 10:50am UTC](https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179/4 "2020-01-23T10:50:49Z")

</div>

You can achieve it with a little bit of plumbing, example here [https://codesandbox.io/s/httpsdiscourseaureliaiotusing-aurelia-for-string-templating3179-tb3gj](https://codesandbox.io/s/httpsdiscourseaureliaiotusing-aurelia-for-string-templating3179-tb3gj)

Note:

- we use default binding language implementation to reuse its interpolation parser
- we create the scope to evaluate the text on the fly

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [January 23, 2020, 10:57am UTC](https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179/5 "2020-01-23T10:57:51Z")

</div>

lol thats insane … you know what you can do with that?

```auto
<span textcontent.bind="'My name is ${placeholder | upperCase }.' | template:{ placeholder: 'Dan' }"></span>

```

```auto
export class UpperCaseValueConverter {
  toView(value: string) {
    return value.toUpperCase();
  }
}

```

[![](https://i.imgflip.com/3my7nw.jpg) ](https://i.imgflip.com/3my7nw.jpg)

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [January 23, 2020, 11:15am UTC](https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179/6 "2020-01-23T11:15:27Z")

</div>

That’s so nice 😄  
But all those value converter needs to be global though, since the `template` value converter is global

Edit: updated the sandbox to accommodate the fun part 😛

---

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [January 25, 2020, 7:30pm UTC](https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179/7 "2020-01-25T19:30:11Z")

</div>

I. Love. This. 🤯

Thanks!

---

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [January 25, 2020, 7:58pm UTC](https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179/8 "2020-01-25T19:58:29Z")

</div>

@bigopon @zewa666

I made the “message part” dynamic as well and added some fail safes (null checks and try/catch) to ensure page doesn’t break while editing (this is not the use case, but the fail safes are probably a good idea to cater for any developer mistakes).

> **[https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179 -...](https://codesandbox.io/s/httpsdiscourseaureliaiotusing-aurelia-for-string-templating3179-s9i3s)**
>
> A CodeSandbox.io template for Aurelia apps that uses TypeScript.

Anything better than using try/catch for this…?

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [January 25, 2020, 9:21pm UTC](https://discourse.aurelia.io/t/using-aurelia-for-string-templating/3179/9 "2020-01-25T21:21:03Z")

</div>

Aurelia has been smashing all the super ultra dynamic form requirements for long time, we just sometimes forgot it 😄
