Skip to content

Commit 1990989

Browse files
committed
Adding docs around the new "css" Encore functions and disabling file tracking
1 parent 06fff08 commit 1990989

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

frontend.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Guides
6969

7070
* :doc:`Using Bootstrap CSS & JS </frontend/encore/bootstrap>`
7171
* :doc:`Creating Page-Specific CSS/JS </frontend/encore/page-specific-assets>`
72+
* :doc:`Rendering Multiple Templates in a Request: Emails, PDFs </frontend/encore/file-tracking>`
7273
* :doc:`jQuery and Legacy Applications </frontend/encore/legacy-applications>`
7374
* :doc:`Passing Information from Twig to JavaScript </frontend/encore/server-data>`
7475
* :doc:`webpack-dev-server and Hot Module Replacement (HMR) </frontend/encore/dev-server>`

frontend/encore/faq.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ like ``/myAppSubdir``), you will need to configure that when calling ``Encore.se
7878
+ .setManifestKeyPrefix('build')
7979
;
8080
81-
If you're using the ``encore_entry_script_tags()`` and ``encore_entry_link_tags()``
82-
Twig shortcuts (or are :ref:`processing your assets through entrypoints.json <load-manifest-files>`
81+
If you're using one of the ``encore_entry_*()`` Twig shortcuts like
82+
``encore_entry_script_tags()`` (or are :ref:`processing your assets through entrypoints.json <load-manifest-files>`
8383
in some other way) you're done! These shortcut methods read from an
8484
:ref:`entrypoints.json <encore-entrypointsjson-simple-description>` file that will
8585
now contain the subdirectory.

frontend/encore/file-tracking.rst

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

mailer.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,22 @@ called ``css`` that points to the directory where ``email.css`` lives:
464464
# point this wherever your css files live
465465
'%kernel.project_dir%/assets/css': css
466466
467+
Using Encore CSS Files
468+
~~~~~~~~~~~~~~~~~~~~~~
469+
470+
If you're using :ref:`Webpack Encore <frontend-webpack-encore>`, you can also
471+
include the CSS from your built files. Suppose you create an ``email`` entry to
472+
contain your email styles:
473+
474+
.. code-block:: html+twig
475+
476+
{% apply inline_css(encore_entry_css_source('email')) %}
477+
<h1>Welcome {{ username }}!</h1>
478+
{# ... #}
479+
{% endapply %}
480+
481+
Any CSS for your ``email`` entry will now be included and inlined.
482+
467483
.. _mailer-markdown:
468484

469485
Rendering Markdown Content

0 commit comments

Comments
 (0)