Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. TwigExtension.php

function TwigExtension::attachLibrary

Attaches an asset library to the template, and hence to the response.

Allows Twig templates to attach asset libraries using


{{ attach_library('extension/library_name') }}

Parameters

string $library: An asset library.

File

core/lib/Drupal/Core/Template/TwigExtension.php, line 372

Class

TwigExtension
A class providing Drupal Twig extensions.

Namespace

Drupal\Core\Template

Code

public function attachLibrary($library) {
    assert(is_string($library), 'Argument must be a string.');
    // Use Renderer::render() on a temporary render array to get additional
    // bubbleable metadata on the render stack.
    $template_attached = [
        '#attached' => [
            'library' => [
                $library,
            ],
        ],
    ];
    $this->renderer
        ->render($template_attached);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal