processwire / processwire-requests

ProcessWire feature requests.
39 stars 0 forks source link

Alternative names for api-vars #527

Closed pine3ree closed 2 weeks ago

pine3ree commented 2 weeks ago

In order to reduce the chances of variable name collision, it would be nice to have alternative api variable names in templates such as: $wireFiles, $wireUser, $wireInput, ... mimicing the nomenclature for the wire* function-API

Right now I do this in my _init.php file:

<?php namespace ProcessWire;

//...

//------------------------------------------------------------------------------
/** @var TemplateFile $this */
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// API VARS as $wire{ApiName}
//------------------------------------------------------------------------------
/** @var WireCache $wireCache */
/** @var WireClassLoader $wireClassLoader */
/** @var Config $wireConfig */
/** @var WireDatabasePDO $wireDatabase */
/** @var WireDateTime $wireDatetime */
/** @var Fields $wireFields */
/** @var WireFileTools $wireFiles */
/** @var WireInput $wireInput */
/** @var Languages $wireLanguages */
/** @var WireLog $wireLog */
/** @var WireMail $wireMail */
/** @var Modules $wireModules */
/** @var Page $wirePage */
/** @var Pages $wirePages */
/** @var Permissions $wirePermissions */
/** @var Roles $wireRoles */
/** @var Sanitizer $wireSanitizer */
/** @var Session $wireSession */
/** @var Templates $wireTemplates */
/** @var User $wireUser */
/** @var Users $wireUsers */

foreach ($this->wire->fuel()->getArray() as $apiName => $apiVar) {
    if ($apiName !== 'wire') {
        $wireApiName = "wire" . ucfirst($apiName);
        $this->wire->set($apiName, ${$wireApiName} = $apiVar);
    }
}
//------------------------------------------------------------------------------

before I used to call the same-name function to obtain the api-var (and skip calling the api-function again)

<?php namespace ProcessWire;
// file: view/layout.html.php
//...
$wireFiles = wireFiles();
//...
//...
$vars = get_defined_vars();
$wireFiles->render('header/topbar.html.php', $vars);
$wireFiles->render('header/navbar.html.php', $vars));
//...
$wireFiles->render('main/main.html.php', $vars);
//...
$wireFiles->render('header/footer.html.php', $vars));

kind regards

BernhardBaumrock commented 2 weeks ago

Why not just use $wire->files, $wire->cache etc?

pine3ree commented 2 weeks ago

Why not just use $wire->files, $wire->cache etc?

Of course and if not overwritten $this->files, etc works as well. But $wire->files is not a direct call, it goes through WireData::__get() and WireData::get(); (correction) Wire::__get() Wire::fuel->get()

Cheers

pine3ree commented 2 weeks ago

I am closing this because I realize it is maybe too opinionated and also trivial to achieve