Skip to content

Add embedData() twig function to the bundle #1

Description

@pscheit
    /**
     * @return array<TwigFunction>
     */
    public function getFunctions(): array
    {
        return [
            new TwigFunction('embedData', [$this, 'embedData'], ['is_safe' => ['html']]),
            //new TwigFunction('exportStyles', [$this, 'exportStyles'], ['is_safe' => ['html']]),
        ];
    }

    /**
     * @param mixed $data
     */
    public function embedData($data, string $name): string
    {
        $ret = '<script type="application/json" id="' . $name . '">';
        $ret .= json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
        $ret .= '</script>';

        return $ret;
    }

and explain how to use this:

{% block body %}
    {{ embedData(vue, 'app-data') }}

    <div id="app" class="hide-on-load bg-white">
        <checkout-frame language="de"></checkout-frame>
    </div>
{% endblock %}
const appData = JSON.parse(document.getElementById('app-data').innerHTML)

for (const key in appData) {
    app.config.globalProperties[key] = appData[key]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions