Skip to content

Add "tokenDelimeter" option (because single curlies conflict with JS blocks) #4

@pzhine

Description

@pzhine

Current behavior

const template = `
<meta name="description" content="{META_DESCRIPTION}">
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'UA-112910279-1');
</script>
`
const tokens = { META_DESCRIPTION: 'This is a website about dogs' }
const rendered = renderTemplate(template, tokens)

rendrered would then be:

<meta name="description" content="This is a website about dogs">
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag()undefined
    gtag('js', new Date());
    gtag('config', 'UA-112910279-1');
</script>

Note: undefined where function block should be is undesirable.

Desired behavior

Add an options parameter with a single option key: tokenDelimeter. Possible values are module constants SINGLE_CURLY (default) and DOUBLE_CURYLY.
Then we can do this:

import renderTemplate, { DOUBLE_CURLY } from 'react-text-templates'
const template = `
<meta name="description" content="{{META_DESCRIPTION}}">
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'UA-112910279-1');
</script>
`
const tokens = { META_DESCRIPTION: 'This is a website about dogs' }
const rendered = renderTemplate(template, tokens, { tokenDelimiter: DOUBLE_CURLY })

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions