This extension adds 8 useful twig snippets that can be used with Drupal 8. These snippets were extracted from the Functions - in Twig Templates documentation.
To install this extension use the following steps:
ctrl-shift-p
(Windows, Linux) or cmd-shift-p
(OSX)Install Extensions
and select that form the list.Drupal 8 Twig Snippets
.Once installed, to use these snippets you can use the following two methods, the first Method is geared towards people who are new to the snippets available while the second method is suitable for people who know the available snippets and are looking to increase their speed.
ctrl-shift-p
(Windows, Linux) or cmd-shift-p
(OSX)Insert Snippet
; this will list all the available snippets.tab
key.Here you will find all the code snippets that are available in this extension. Please note that each title starts with the Prefix of the code snippet to insert; useful to memorize them if you plan to use Method 2 above.
Attaches an asset library to the template.
Example:
{{ attach_library('classy/node') }}
Creates an attribute on a DOM element.
Example:
<div{{ create_attribute({'class': ['region', 'region--header']}) }}>
{{ content }}
</div>
file_url($uri)
This helper function accepts a relative path from the root and creates a relative URI path to the file.
Example:
{{ file_url(node.field_example_image.entity.uri.value) }}
link($text, $url, $attributes)
This helper function accepts as first parameter the text, as second parameter the url and as third paramter any attributes for the link.
Example:
{{ link(item.title, item.url, { 'class':['foo', 'bar', 'baz']} ) }}
path($name, $parameters, $options)
Generates a relative URL path given a route name and parameters.
Example:
{# Link to user entity/profile page. #}
<a href="https://github.com/tsega/drupal-8-twig-snippets-vs-code/blob/master/{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a>
url($name, $parameters, $options)
Generate an absolute URL given a route name and parameters.
Example:
<a href="https://github.com/tsega/drupal-8-twig-snippets-vs-code/blob/master/{{ url('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a>
url('<current>')
Generate an absolute URL to the current url.
Example:
<a href="https://github.com/tsega/drupal-8-twig-snippets-vs-code/blob/master/{{ url('<current>') }}">{{ 'View all content'|t }}</a>
url('<front>')
Generate an absolute URL to the front page.
Example:
<a href="https://github.com/tsega/drupal-8-twig-snippets-vs-code/blob/master/{{ url('<front>') }}">{{ 'View all content'|t }}</a>
These snippets are only made available in files with a .twig
extension. To get proper support for Twig files in Visual Studio Code, you can install the Twig Language 2 extension by mblode.
Fix typos in README
Added the Drupal 8 logo
Initial release