|
| 1 | +Rendering Multiple Templates in a Request: Emails, PDFs |
| 2 | +======================================================= |
| 3 | + |
| 4 | +When you render your script or link tags with the Twig helper functions |
| 5 | +(e.g. ``encore_entry_link_tags()``), WebpackEncoreBundle is smart enough |
| 6 | +not to repeat the same JavaScript or CSS file within the same request. |
| 7 | +This prevents you from having duplicate ``<link>`` or ``<script>`` tags |
| 8 | +if you render multiple entries that rely on the same file. |
| 9 | + |
| 10 | +But if you're purposely rendering multiple templates in the same |
| 11 | +request - e.g. rendering a template for a PDF or to send an email - |
| 12 | +then this can cause problems: the later templates won't include any |
| 13 | +``<link>`` or ``<script>`` tags that were rendered in an earlier template. |
| 14 | + |
| 15 | +The easiest solution is to render the raw CSS and JavaScript using |
| 16 | +a special function that *always* returns the full source, even for files |
| 17 | +that were already rendered. |
| 18 | + |
| 19 | +This works especially well in emails thanks to the |
| 20 | +`inline_css`_ filter: |
| 21 | + |
| 22 | +.. code-block:: html+twig |
| 23 | + |
| 24 | + {% apply inline_css(encore_entry_css_source('my_entry')) %} |
| 25 | + <div> |
| 26 | + Hi! The CSS from my_entry will be converted into |
| 27 | + inline styles on any HTML elements inside. |
| 28 | + </div> |
| 29 | + {% endapply %} |
| 30 | + |
| 31 | +Or you can just render the source directly. |
| 32 | + |
| 33 | +.. code-block:: html+twig |
| 34 | + |
| 35 | + <style> |
| 36 | + {{ encore_entry_css_source('my_entry')|raw }} |
| 37 | + </style> |
| 38 | + |
| 39 | + <script> |
| 40 | + {{ encore_entry_js_source('my_entry')|raw }} |
| 41 | + </script> |
| 42 | + |
| 43 | +If you can't use these `encore_entry_*_source` functions, you can instead |
| 44 | +manually disable and enable "file tracking": |
| 45 | + |
| 46 | +.. code-block:: html+twig |
| 47 | + |
| 48 | + {% do encore_disable_file_tracking() %} |
| 49 | + {{ encore_entry_link_tags('entry1') }} |
| 50 | + {{ encore_entry_script_tags('entry1') }} |
| 51 | + {% do encore_enable_file_tracking() %} |
| 52 | + |
| 53 | +With this, *all* JS and CSS files for `entry1` will be rendered and |
| 54 | +this won't affect any other Twig templates rendered in the request. |
| 55 | + |
| 56 | +Resetting the File Tracking |
| 57 | +--------------------------- |
| 58 | + |
| 59 | +If using ``encore_disable_file_tracking()`` won't work for you for some |
| 60 | +reason, you can also "reset" EncoreBundle's internal cache so that the |
| 61 | +bundle re-renders CSS or JS files that it previously rendered. For |
| 62 | +example, in a controller:: |
| 63 | + |
| 64 | + |
| 65 | + // src/Controller/SomeController.php |
| 66 | + |
| 67 | + use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface; |
| 68 | + |
| 69 | + class SomeController |
| 70 | + { |
| 71 | + public function index(EntrypointLookupInterface $entrypointLookup) |
| 72 | + { |
| 73 | + $entrypointLookup->reset(); |
| 74 | + // render a template |
| 75 | + |
| 76 | + $entrypointLookup->reset(); |
| 77 | + // render another template |
| 78 | + |
| 79 | + // ... |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +If you have multiple builds, you can also autowire |
| 84 | +``Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface`` |
| 85 | +and use it to get the ``EntrypointLookupInterface`` object for any build. |
| 86 | + |
| 87 | +.. _`inline_css`: https://github.com/twigphp/cssinliner-extra |
0 commit comments