Smarty support

PHP Tools natively support Smarty template engine since version 1.22 in Visual studio 2015 and newer. PHP Tools provide many tools for Smarty, like intellisense and navigation, and the following sections provide detailed information on all the available features. The Smarty support is built-in and its features do not require Smarty to be installed in the project (detailed information is provided for each feature). Smarty can be installed either via a composer package or its source code can be directly integrated into the project.

Syntax highlighting

PHP Tools provide comprehensive syntax highlighting for Smarty templates. The colors follow the Visual studio color scheme and support VS themes. Syntax highlighting does not require Smarty to be installed.

The following image shows highlighted Smarty syntax for a short template.

Smarty syntax highlighting

User extension support

PHP Tools support the smarty engine itself and all the user extensions and definitions allowed by the engine. User extensions are supported through all the features, including intellisense, navigation and syntax validation. PHP Tools support the following features:

  • User defined modifiers and filters (PHP functions with the name prefixed smartymodifier).
  • Variables defined in PHP (defined in PHP code using Smarty->assign('name, 'value');).

Example of the intellisense for a user defined Smarty modifier.

User extension filter

Smarty variable defined by the user in a PHP code and used in a template.

User defined variable

Tooltips

PHP Tools provide tooltips for all known variables and modifiers or filters (both user defined and intrinsic). The tooltips for modifiers follow the Smarty notation ($string|capitalize:true:true). PHP Tools provide basic tooltip for all constructs (signature and default values) and additional information is provided for modifiers declared with PHPDoc documentation. PHP Tools provide tooltip support for the following constructs:

  • Built-in modifiers and filters (requires Smarty to be installed).
  • User defined modifiers and filters (declaration must be part of the project).
  • Variables defined in PHP (Smarty->assign('name, 'value');).
  • Variables used in Smarty template (variables used in the currently edited template).

The following image shows an example of a tooltip for built-in modifier capitalize, including its PHPDoc documentation.

Smarty tooltip

Intellisense completion

PHP Tools provide intellisense completion assistance that provides available functions, variables and keywords. The matching completion list is displayed either when user starts typing a new word or when the completion is directly activated by the Complete Word command (Ctrl+Space). The completion supports the following features:

  • Built-in modifiers and filters (requires Smarty to be installed).
  • User defined modifiers and filters.
  • Variables defined in PHP (Smarty->assign('name, 'value');).
  • Variables used in Smarty template (variables used in the currently edited template).
  • Smarty keywords (including multi-word like is even).

The example image shows the completion options for the word capi with the built-in modifier capitalize selected as best match.

Smarty intellisense completion

Navigation

PHP Tools support the Go To Definition command (F12) for all constructs defined either by the user or directly in Smarty. The command opens the file containing the definition of currently selected variable or modifier and places the caret the beginning of the definition. The navigation supports the following features:

  • Built-in modifiers and filters (requires Smarty to be installed).
  • User defined modifiers and filters.
  • Variables defined in PHP (Smarty->assign('name, 'value');).

The example shows the command in a context menu for the capitalize modifier.

Smarty navigation

Construct completion

PHP Tools provide a command similar to Insert Snippet (Tab) for pair Smarty tags like for or php. The command is activated by pressing Tab when the caret is placed at the end of a pair keyword. When used for a pair tag, PHP Tools add the end tag (for example {/for}) and standard parameters supported by the construct, like initial values or limits. PHP Tools support all the pair tags.

The following examples show the completion of a for loop pair tag, first image shows the initial code.

Incomplete Smarty for loop

The next shows the completed for loop.

Completed Smarty for loop

Syntax validation

PHP Tools provide simplified syntax validation for Smarty templates. The system checks basic Smarty syntax rules, but it does not analyze more complex constructs, like modifier signature (parameters).
The validation is not completely strict to provide more freedom for customization of the Smarty engine, while providing information for the common errors.

The image shows a syntax error where the for tag is used as a filter.

Smarty syntax validation

Smarty installation

Smarty can be installed either via a composer package or its source code can be directly integrated into the project (by copying its code). PHP Tools provide an information bar whenever a template is opened without the Smarty system installed. The bar is shown in the following image. It allows users to seamlessly install Smarty composer package, simply by clicking the Install now link. The bar disappears once Smarty is installed. The bar can be closed either once or forever by clicking the appropriate button.

Smarty information bar

Smarty properties

PHP Tools support global Smarty settings, which modify how the editor works with Smarty. The settings are defined in a project file and can be changed either in the project Properties GUI page or in the project source code (XML). Smarty settings can be accessed by right clicking the project file and selecting Properties. The settings are in the table in the last section titled Settings. The settings are not available when Smarty system is not installed, which is reported by the information bar explained in the previous section. PHP Tools support the following features:

  • Smarty delimiters (parentheses surrounding Smarty tags), delimiters can be changed for each project to prevent conflicts with other languages (for example JavaScript).
    • The delimiters are set using the SmartyDelimiter property using the string "left right", where the space is necessary.

The next image shows the Smarty settings in the project Properties page.

Smarty project settings

The following example shows the Smarty delimiters changed in a project file.

Smarty delimiter settings

Smarty limitation

PHP Tools support Smarty integration with HTML and Cascading Style Sheets (CSS) in Visual studio 2015 and newer. Smarty is also fully integrated with JavaScript in Visual studio 2015.