-
I'm calling a function in twig: {% set col = col(name: 'projectCountBin', title: 'log10', browseOrder: 600) %} col is a wrapper function that returns my Column class:
The column class has camelCased properties: public function __construct(
public string $name,
public ?string $twigTemplate = null,
public ?string $rowName = 'row', // what's passed to the column renderer
public ?string $prefix = null,
public ?string $internalCode = null, // e.g. label, description, type
public ?string $className = null, // e.g. pull-right for numbers
public bool $searchable = false,
public bool $useDatatables = true,
public bool $browsable = false,
public int $browseOrder = 0, // if 0, same as false, so we can deprecate browsable Is there a way to preserve the property name when passed? I'm getting
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
I have temporarily solved my problem with a hack that maps the keys to camelCase: new TwigFunction('col', function(...$params) {
foreach ($params as $key => $value) {
$newParams[u($key)->camel()->toString()] = $value;
}
return new Column(...$newParams);
} ,
['is_variadic' => true]
) but it seems wrong. I remember there being some discussion about this when named parameters were being discussed. I LOVE being able to call twig functions with named parameters. I even tried browseOrder=600 as a test, but same key renaming. |
Beta Was this translation helpful? Give feedback.
-
Perhaps the twig function call is enforcing the coding standards: https://twig.symfony.com/doc/3.x/coding_standards.html However, it conflicts with the PHP coding standards: https://symfony.com/doc/current/contributing/code/standards.html#naming-conventions To me, it feels wrong that calling a user-defined twig function changes the argument keys. Perhaps there could be a configuration option, like doctrine has for mapping entities to the underlying table and column names. |
Beta Was this translation helpful? Give feedback.
-
Here's are the other relevant discussions, I don't think I add anything new, so I'll close this. |
Beta Was this translation helpful? Give feedback.
-
This is solved in Twig 4 (not released yet) as names are not normalized anymore for variadic arguments (the function would have to change array keys if it expects the old behavior of turning any key to snake case). |
Beta Was this translation helpful? Give feedback.
This is solved in Twig 4 (not released yet) as names are not normalized anymore for variadic arguments (the function would have to change array keys if it expects the old behavior of turning any key to snake case).
In Twig 3, there is no way to solve that with a variadic signature. You would have to use explicit parameter names in your Twig function (so that parameter names are resolved by the Twig compiler)