-
Notifications
You must be signed in to change notification settings - Fork 718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modifier called like a function compiled to modifier name, not function callback #1100
Comments
I also do like some functions being compiled into native functions (instead of going through the plugin system), but if I explicitly define a different callback, it should probably use that, not the function name. |
Could you provide a full code example? From your report I have several questions.
|
The register code is literally $this->smarty->registerPlugin('modifier', 'kprint_r_out', trim(...)); with the But the callback doesn't even matter, that's my point. This register would do the same: $this->smarty->registerPlugin('modifier', 'kprint_r_out', 'trim'); or this: $this->smarty->registerPlugin('modifier', 'kprint_r_out', function() {
// Smarty never gets here
exit;
}); as long as Maybe I just misunderstand Smarty syntax, but it's still a bug IMO. I know functions:
and modifiers:
and apparently modifiers can be called like real functions!?:
which I love, because it's much better syntax and supports variadic functions. And they compile into native functions, which is great for performance. Buut that should only happen if the callback is the same as the function name, like with
(used like not if they're different, like with my orig example:
That could be compiled into
because
because |
All kinds of callbacks, and how to reflect their source: https://3v4l.org/D8hib I should even be able to make a Smarty modifier like: $manager = getAccessManager();
$this->smarty->registerPlugin('modifier', 'can', $manager->hasAccess(...)); And then call |
It might be compiled correctly for use as actual modifier though: |
Super simple buggy way to use Reflection to compile those callbacks into code: https://3v4l.org/m0tP7 Sometimes it's possible to compile into native callable, but sometimes it has to go through the plugin systems. |
Aaah got it now! I'll look into it. |
I wrongfully assumed the three dots meant you left some irrelevant code out... 😣 |
@rudiedirkx In v4.5.5 I'm getting a In v5, your code runs fine BTW. |
It is very ambiguous syntax, I agree. That's how I used to write pseudo code too, before this was actual syntax. |
Because that function doesn't exist in your test? Register a function (or modifier?) with name X and callback Y, compile a template with
It runs fine in my setup too, but only because the function name and the actual function are the same. But I can't make a Smarty function |
It's even weirder... Or I really don't know how Smarty works. If I register a modifier
I can call it like a function:
but if I call it something else:
and I call it the same:
I get a compile error
Smarty does something very odd with real function names instead of the registered name and callback. I'm making a demo repo. And how is this weird fatal error possible!?
It's a |
Demo project: https://github.com/rudiedirkx/smarty-demo I definitely don't know how Smarty works. Smarty function vs modifier vs functionlike? Smarty finds it acceptable to call a PHP function, but only if you register it as a modifier, but it doesn't actually call the modifier?? I don't get it. |
Please explain the difference (and how Smarty handles those) between
I've always thought the first two are Smarty syntax that's been around forever (smarty function and smarty modifier), and the third is 'newly' acceptable syntax to call a modifier. The first two work as expected, but the third behaves very strangely. What is that? When is Smarty okay with that? What does it run? |
Smarty 5 works as expected, and it compiles as expected too. All the calls go through the plugin system: echo $_smarty_tpl->getSmarty()->getModifierCallback('trim3')($_smarty_tpl->getValue('myVar')); And modifier trim3 ( This is definitely a bug in Smarty 4. Even if it's a feature, the feature has a bug 😆 I guess I should upgrade to Smarty 5. |
The thing is, in Smarty up until and including v4, you can call any callable PHP function directly from a template as well. I have removed this in Smarty v5, requiring developers to register any function they want to use. Since there is now now difference between the The bug (or the buggy feature) in v4 now seems to be that if you pass a closure as a modifier to |
@rudiedirkx can you take a look at #1101 for me? |
Smarty 4.5.5
Is this a bug or a very strange feature? If I define a modifier:
and call it like:
{kprint_r_out($myVar)}
it will compile into
even though
kprint_r_out
is only the name of the function, not the function itself (butkprint_r_out()
does exist, otherwise it won't compile). The function (trim
in this case) isn't used at all...I like calling functions like function, but this is too weird. If it's intentional, it shouldn't be defined as a modifier with mandatory callback (that's not used).
Or am I abusing another Smarty functionality somehow?
The text was updated successfully, but these errors were encountered: