Template

Is a function that replace all the template expressions with the corresponding values from the data object.

Usage

<script>
    import {template} from "@sveu/shared"

    const str = `Hi {{name}},

    Welcome to {{platform}}
`
    const result = template(str, {"name": "Beatrice", "platform": "svelte"})
</script>

<h1>{result}!</h1>
<script>
    import {template} from "@sveu/shared"

    const str = `Hi {{name}},

    Welcome to {{platform}}
`
    const result = template(str, {"name": "Beatrice", "platform": "svelte"})
</script>

<h1>{result}!</h1>
svelte

Example

Hi Beatrice, Welcome to svelte !

<script>
    import { template } from "@sveu/shared"

    const str = `Hi %name%,
    Welcome to %platfrom%
    `
    const result = template(
        str,
        { name: "Beatrice", platfrom: "svelte" },
        /\%(.+?)\%/g
    )
</script>

<h1 class="text-center">{result}!</h1>
<script>
    import { template } from "@sveu/shared"

    const str = `Hi %name%,
    Welcome to %platfrom%
    `
    const result = template(
        str,
        { name: "Beatrice", platfrom: "svelte" },
        /\%(.+?)\%/g
    )
</script>

<h1 class="text-center">{result}!</h1>
svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Click fold/expand code

API

Arguments

Name Description Type Required
str The string to be searched string Yes
data The data object to be used to replace the search expression Record<string, any> Yes
regex The search expression RegExp No