Skip to content

Add custom function to twig loader #10

@tacman

Description

@tacman

I'm trying to reuse some server-side twig files in twigjs. I'd like to add a path function that basically maps FosJsRouting bundle generate function, since obviously a client-side renderer can't render server paths without that data.

I see the examples on how to extend Twig with a custom function, but I never actually get a Twig variable, I simply load the templates via this terrific loader.

What I have now is:

const routes = require('../../public/js/fos_js_routes.json');
import Routing from '../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';
Routing.setRoutingData(routes);
let template = require('../templates/headlines_by_group.html.twig'); 
let html = template({Routing: Routing, project: project});

and the template contains

<a href="{{ Routing.generate('project_show', {id: project.id}) }}">{{ project.name }}</a>

What I want I for the template to simply contain the server-side twig:

<a href="{{ path('project_show', {id: project.id}) }}">{{ project.name }}</a>

And have the path function be mapped from the Routing above, from https://github.com/FriendsOfSymfony/FOSJsRoutingBundle


Twig.extendFunction("path", (route, params) =>
{
    return Routing.generate(route, params);
});

Is it even possible? Having path() in twigjs templates would be amazing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions