Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Guides

* :doc:`Using Bootstrap CSS & JS </frontend/encore/bootstrap>`
* :doc:`Creating Page-Specific CSS/JS </frontend/encore/page-specific-assets>`
* :doc:`Rendering Multiple Templates in a Request: Emails, PDFs </frontend/encore/file-tracking>`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't we doing :doc:`/frontend/encore/file-tracking` here? :)

* :doc:`jQuery and Legacy Applications </frontend/encore/legacy-applications>`
* :doc:`Passing Information from Twig to JavaScript </frontend/encore/server-data>`
* :doc:`webpack-dev-server and Hot Module Replacement (HMR) </frontend/encore/dev-server>`
Expand Down
4 changes: 2 additions & 2 deletions frontend/encore/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ like ``/myAppSubdir``), you will need to configure that when calling ``Encore.se
+ .setManifestKeyPrefix('build')
;

If you're using the ``encore_entry_script_tags()`` and ``encore_entry_link_tags()``
Twig shortcuts (or are :ref:`processing your assets through entrypoints.json <load-manifest-files>`
If you're using one of the ``encore_entry_*()`` Twig shortcuts like
``encore_entry_script_tags()`` (or are :ref:`processing your assets through entrypoints.json <load-manifest-files>`
in some other way) you're done! These shortcut methods read from an
:ref:`entrypoints.json <encore-entrypointsjson-simple-description>` file that will
now contain the subdirectory.
Expand Down
87 changes: 87 additions & 0 deletions frontend/encore/file-tracking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Rendering Multiple Templates in a Request: Emails, PDFs
=======================================================

When you render your script or link tags with the Twig helper functions
(e.g. ``encore_entry_link_tags()``), WebpackEncoreBundle is smart enough
not to repeat the same JavaScript or CSS file within the same request.
This prevents you from having duplicate ``<link>`` or ``<script>`` tags
if you render multiple entries that rely on the same file.

But if you're purposely rendering multiple templates in the same
request - e.g. rendering a template for a PDF or to send an email -
then this can cause problems: the later templates won't include any
``<link>`` or ``<script>`` tags that were rendered in an earlier template.

The easiest solution is to render the raw CSS and JavaScript using
a special function that *always* returns the full source, even for files
that were already rendered.

This works especially well in emails thanks to the
`inline_css`_ filter:

.. code-block:: html+twig

{% apply inline_css(encore_entry_css_source('my_entry')) %}
<div>
Hi! The CSS from my_entry will be converted into
inline styles on any HTML elements inside.
</div>
{% endapply %}

Or you can just render the source directly.

.. code-block:: html+twig

<style>
{{ encore_entry_css_source('my_entry')|raw }}
</style>

<script>
{{ encore_entry_js_source('my_entry')|raw }}
</script>

If you can't use these `encore_entry_*_source` functions, you can instead
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double backticks please

manually disable and enable "file tracking":

.. code-block:: html+twig

{% do encore_disable_file_tracking() %}
{{ encore_entry_link_tags('entry1') }}
{{ encore_entry_script_tags('entry1') }}
{% do encore_enable_file_tracking() %}

With this, *all* JS and CSS files for `entry1` will be rendered and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
With this, *all* JS and CSS files for `entry1` will be rendered and
With this, *all* JS and CSS files for ``entry1`` will be rendered and

this won't affect any other Twig templates rendered in the request.

Resetting the File Tracking
---------------------------

If using ``encore_disable_file_tracking()`` won't work for you for some
reason, you can also "reset" EncoreBundle's internal cache so that the
bundle re-renders CSS or JS files that it previously rendered. For
example, in a controller::


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

// src/Controller/SomeController.php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
namespace App\Controller;

use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;

class SomeController
{
public function index(EntrypointLookupInterface $entrypointLookup)
{
$entrypointLookup->reset();
// render a template
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// render a template
// ... render a template


$entrypointLookup->reset();
// render another template
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// render another template
// ... render another template


// ...
}
}

If you have multiple builds, you can also autowire
``Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface``
and use it to get the ``EntrypointLookupInterface`` object for any build.

.. _`inline_css`: https://github.com/twigphp/cssinliner-extra
16 changes: 16 additions & 0 deletions mailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,22 @@ called ``css`` that points to the directory where ``email.css`` lives:
# point this wherever your css files live
'%kernel.project_dir%/assets/css': css

Using Encore CSS Files
~~~~~~~~~~~~~~~~~~~~~~

If you're using :ref:`Webpack Encore <frontend-webpack-encore>`, you can also
include the CSS from your built files. Suppose you create an ``email`` entry to
contain your email styles:

.. code-block:: html+twig

{% apply inline_css(encore_entry_css_source('email')) %}
<h1>Welcome {{ username }}!</h1>
{# ... #}
{% endapply %}

Any CSS for your ``email`` entry will now be included and inlined.

.. _mailer-markdown:

Rendering Markdown Content
Expand Down