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.

Latest news

  • ProcessWire Weekly #561
    In the 561st issue of ProcessWire Weekly we're going to check out the latest core updates, share recent support forum highlights and online resources, and more. Read on!
    Weekly.pw / 8 February 2025
  • ProcessWire 3.0.244 new main/master version
    ProcessWire 3.0.244 is our newest main/master/stable version. It's been more than a year in the making and is packed with tons of new features, issue fixes, optimizations and more. This post covers all the details.
    Blog / 18 January 2025
  • Subscribe to weekly ProcessWire news

“Indeed, if ProcessWire can be considered as a CMS in its own right, it also offers all the advantages of a CMF (Content Management Framework). Unlike other solutions, the programmer is not forced to follow the proposed model and can integrate his/her ways of doing things.” —Guy Verville, Spiria Digital Inc.