AutoTemplateStubs by Robin S

Automatically creates stub files for templates when fields or fieldgroups are saved.

Auto Template Stubs

A module for ProcessWire CMS/CMF. Automatically creates stub files for templates when fields or fieldgroups are saved.

Stub files are useful if you are using an IDE (e.g. PhpStorm) that provides code assistance - the stub files let the IDE know what fields exist in each template and what data type each field returns. Depending on your IDE's features you get benefits such as code completion for field names as you type, type inference, inspection, documentation, etc.

Installation


Install the Auto Template Stubs module.

Configuration


  • If you're using custom Page classes and want code assistance for methods in the custom Page class as well as field names you can tick the "Name stub classes for compatibility with custom Page classes" checkbox. This names the template stub classes using the same format as for custom Page class names. A side-effect of this is that your IDE may warn you that multiple definitions exist for your custom Page classes.

  • If you're not using the setting above you have the option to change the class name prefix. It's good to use a class name prefix because it reduces the chance that the class name will clash with an existing class name.

  • The path where AutoTemplateStubs directory will be created is configurable. This directory will contain the stub files.

  • There is a checkbox to manually trigger the regeneration of all stub files if needed.

Usage


Add a line near the top of each of your template files to tell your IDE what stub class name to associate with the $page variable within the template file. For example, with the default class name prefix you would add the following line at the top of the home.php template file:

/** @var tpl_home $page */

Or if you're using the "Name stub classes for compatibility with custom Page classes" setting then it would be:

/** @var HomePage $page */

Now enjoy code completion, etc, in your IDE.

stubs

Adding data types for non-core Fieldtype modules


The module includes the data types returned by all the core Fieldtype modules. If you want to add data types returned by one or more non-core Fieldtype modules then you can hook the AutoTemplateStubs::getReturnTypes() method. For example, in /site/ready.php:

// Add data types for some non-core Fieldtype modules
$wire->addHookAfter('AutoTemplateStubs::getReturnTypes', function(HookEvent $event) {
    $extra_types = [
        'FieldtypeDecimal' => 'string',
        'FieldtypeLeafletMapMarker' => 'LeafletMapMarker',
        'FieldtypeRepeaterMatrix' => 'RepeaterMatrixPageArray',
        'FieldtypeTable' => 'TableRows',
    ];
    $event->return = $event->return + $extra_types;
});

Credits


Inspired by and much credit to the Template Stubs module by mindplay.dk.

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Twitter updates

  • Weekly update – 31 March 2023 – More
    31 March 2023
  • This week ProcessWire 3.0.214 is on the dev branch. Relative to 3.0.213 this version has 16 new commits which include the addition of 3 new pull requests, 6 issue fixes, a new WireNumberTools utility class, and various other improvements. More
    17 March 2023
  • ProcessWire 3.0.213 core updates: This week we’ll look at the new WireSitemapXML module, a new WireNumberTools core class, and a new ability for Fieldtype modules to specify useful ready-to-use configurations when creating new fields. More
    24 February 2023

Latest news

  • ProcessWire Weekly #463
    In the 463rd issue of ProcessWire Weekly brings we'll check out the latest weekly update from Ryan, some weekly forum and online highlights, and more. Read on!
    Weekly.pw / 26 March 2023
  • ProcessWire 3.0.213 core updates
    This week we’ll look at the new WireSitemapXML module, a new WireNumberTools core class, and a new ability for Fieldtype modules to specify useful ready-to-use configurations when creating new fields.
    Blog / 24 February 2023
  • Subscribe to weekly ProcessWire news

“ProcessWire is like a breath of fresh air. So powerful yet simple to build with and customise, and web editors love it too.” —Margaret Chatwin, Web developer