0
votes

I'm adding Symfony Webpack Encore to an existing project with native php templates, so without the use of Twig. Refactoring the whole project to Twig is not worth the effort now.

The following Twig code is proposed in the Webpack Encore documentation to render the JS script tags and the CSS link tags.

 {% block stylesheets %}
     {{ encore_entry_link_tags('app') }}
 {% endblock %}
 {% block javascripts %}
     {{ encore_entry_script_tags('app') }}
 {% endblock %}

So I would like to implement the same code without using Twig. These Twig functions use the TagRenderer and EntrypointLookup classes. How can I initiate the required models and mimic the functionality of the Twig functions?

1

1 Answers

1
votes

If you have access to the service in your PHP file, then this is the PHP code behind the twig functions:

$service = $container->get('webpack_encore.tag_renderer');

encore_entry_link_tags

$service->renderWebpackLinkTags($entryName, $packageName, $entrypointName);

encore_entry_script_tags

$service->renderWebpackScriptTags($entryName, $packageName, $entrypointName);

The arguments u should pass (with default configuration)

  • $entryName = 'app'
  • $packageName = null
  • $entrypointName = '_default';