smarty-php / smarty-php/smarty

Smarty registerClass() call with named (like PHP 8+) arguments

Open
#864 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
PHP
Stars
2.3k
Forks
709
PR merge metrics
No merged PRs in 30d

Description

We can register class:

$smarty->registerClass("OptionSwitcher", "\My\App\OptionSwitcher");

and use it something like this:

{OptionSwitcher::html('switch_debug_mode', 'is_debug_mode_active') }

It works nice... but OptionSwitcher::html() method have more than two arguments:

public static function html(string $switcher_id = '', string $exposed_smarty_variable = '', string $on_text = 'YES', string $off_text = 'NO', string $css_tuning_class = '', string $request_uri = '', bool $is_enabled = true): string
    {
// ... 
}

If it is necessary to pass a different value for the is_enabled argument than the default value, you will need to list all the arguments preceding it, passing the desired values (or default values):

{OptionSwitcher::html('switch_debug_mode', 'is_debug_mode_active', 'YES', 'NO', '', '', false) }

... looks ugly

The way out of this is the named argument mechanism introduced in PHP 8: https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments

array_fill(start_index: 0, count: 100, value: 50);

or, in my case:

// call
OptionSwitcher::html('switch_debug_mode', 'is_debug_mode_active', is_enabled: false)

Does Smart support such a mechanism at the templates? Like this:

{OptionSwitcher::html('switch_debug_mode', 'is_debug_mode_active', is_enabled: false) }

?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests. Start by locating Smarty's handling of registered-class calls and comparing its argument parsing with PHP 8 named arguments. Done means a template call using a named argument works while existing positional calls remain supported, with tests covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.