Repository: wagtail/wagtail
File: wagtail/admin/templatetags/wagtailadmin_tags.py (Python)

Code before:

    def render(self, context):
        fragment = self.nodelist.render(context) if self.nodelist else ""
        # Only strip the leading and trailing spaces, unlike
        # {% blocktrans trimmed %} that also does line-by-line stripping.
        # Then, use mark_safe because the SafeString returned by
        # NodeList.render() is lost after stripping.
        if self.stripped:
            fragment = mark_safe(fragment.strip())
        context[self.target_var] = fragment
        return ""


@register.tag(name="fragment")
def fragment(parser, token):
    """
    Store a template fragment as a variable.

    Usage:

    .. code-block:: html+django

        {% fragment as header_title %}
            {% blocktrans trimmed %}Welcome to the {{ site_name }} Wagtail CMS{% endblocktrans %}
        {% endfragment %}
        {% include "my/custom/header.html" with title=header_title %}

    Adopted from `slippers' fragment template tag <https://github.com/mixxorz/slippers/blob/254c720e6bb02eb46ae07d104863fce41d4d3164/slippers/templatetags/slippers.py#L173>`_ with a few tweaks.

    To strip leading and trailing whitespace produced in the fragment, use the
    ``stripped`` option. This is useful if you need to check if the resulting
    fragment is empty (after leading and trailing spaces are removed):

    .. code-block:: html+django

        {% fragment stripped as recipient %}
            {{ title }} {{ first_name }} {{ last_name }}
        {% endfragment %}
        {% if recipient %}
            Recipient: {{ recipient }}
        {% endif %}

    Note that the stripped option only strips leading and trailing spaces, unlike
    ``{% blocktrans trimmed %}`` that also does line-by-line stripping. This is because
    the fragment may contain HTML tags that are sensitive to whitespace, such as
    ``<pre>`` and ``<code>``.
    """
    error_message = "The syntax for fragment is {% fragment as variable_name %}"

    try:
        tag_name, *options, target_var = token.split_contents()

Review comment (suggestion):
Add an `include` example so it's clearer why you might need the `fragment` over just adding `asvar` to `blocktrans`.

Edit: actually, now that I think about it, this use case can still be covered by `blocktrans trimmed asvar`...

Code after:

    def render(self, context):
        fragment = self.nodelist.render(context) if self.nodelist else ""
        # Only strip the leading and trailing spaces, unlike
        # {% blocktrans trimmed %} that also does line-by-line stripping.
        # Then, use mark_safe because the SafeString returned by
        # NodeList.render() is lost after stripping.
        if self.stripped:
            fragment = mark_safe(fragment.strip())
        context[self.target_var] = fragment
        return ""


@register.tag(name="fragment")
def fragment(parser, token):
    """
    Store a template fragment as a variable.

    Usage:

    .. code-block:: html+django

        {% fragment as header_title %}
            {% blocktrans trimmed %}Welcome to the {{ site_name }} Wagtail CMS{% endblocktrans %}
        {% endfragment %}
        {% include "my/custom/header.html" with title=header_title %}

    Adopted from `slippers' fragment template tag <https://mitchel.me/slippers/docs/template-tags-filters/#fragment>`_ with a few tweaks.

    To strip leading and trailing whitespace produced in the fragment, use the
    ``stripped`` option. This is useful if you need to check if the resulting
    fragment is empty (after leading and trailing spaces are removed):

    .. code-block:: html+django

        {% fragment stripped as recipient %}
            {{ title }} {{ first_name }} {{ last_name }}
        {% endfragment %}
        {% if recipient %}
            Recipient: {{ recipient }}
        {% endif %}

    Note that the stripped option only strips leading and trailing spaces, unlike
    ``{% blocktrans trimmed %}`` that also does line-by-line stripping. This is because
    the fragment may contain HTML tags that are sensitive to whitespace, such as
    ``<pre>`` and ``<code>``.
    """
    error_message = "The syntax for fragment is {% fragment as variable_name %}"

    try:
        tag_name, *options, target_var = token.split_contents()