PhpStorm 2018.3 Help

Working with Twig Templates

Besides the common templates support, the Symfony Plugin provides completion, navigation and code inspection specific to the Twig templating language.

Code completion for Twig templates

In the editor, press Ctrl+Space to invoke code completion and do any of the following:

  • Provide Templates and Blocks for appropriate functions in any twig file.

    ps symfony twig template name completion
  • Provide Extensions, Filters and Macros, both self-defined or installed from a third party.

    ps symfony twig template extension completion
  • Provide individual assets and full bundles.

    ps symfony twig asset name completion

Code navigation for Twig templates

To navigate to the declaration of a template, block, extension, filter, macro, or asset, position the caret at its usage and press Ctrl+B. Alternatively, Ctrl+Click (for Windows and Linux) or ⌘+Click (for macOS) the usage.

ps symfony twig navigate extension

Code inspection for Twig templates

If we refer an asset that is not known to the application (for example, if the asset does not exist yet), PhpStorm displays the corresponding warning from the Missing Asset inspection..

ps symfony twig missing asset inspection

Working with Twig variables

In a Twig template, you can define variables using the {# variable_name variable_value #} syntax, for example:

{# name John #}

As soon as a variable is defined, code completion (Ctrl+Space ) and navigating to declaration (Ctrl+B) for it become available.

In a similar manner, you can define a controller that uses the template by adding a controller annotation, for example:

{# @controller BloggerBlogBundle:Home:index #}

Once we do this, PhpStorm will provide code completion and navigation for all variables passed into the template.

ps symfony twig variable name completion
Last modified: 7 December 2018