smarty icon indicating copy to clipboard operation
smarty copied to clipboard

nullsafe operator is not working

Open mikehageman opened this issue 4 years ago • 7 comments

Using ?-> results in: Unexpected "?", expected one of: "}" when php 8 is used

mikehageman avatar Jan 14 '21 12:01 mikehageman

@mikehageman could you provide a little more context? E.g. a line of an actual template?

wisskid avatar Jan 14 '21 23:01 wisskid

Sorry ofcourse, I figured it's one of the new key feautures of php8 so it would be kinda obvious (and unfortunately i do not have the time to fix it myself)

<input id="images_categories" name="images_categories" list="images_categories_data" maxlength="50" class="form-control" value="{$images->get_images_categories()?->get_category()}"

Fatal error: Uncaught --> Smarty Compiler: Syntax error in template "file:....images.tpl" on line 51 "value="{$images->get_images_categories()?->get_category()}"" - Unexpected "?", expected one of: "}" <-- thrown in ..../vendor/smarty/smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php on line 51

$images is a database model, so if i call get_images_categories() it wil give me either null or a images_categories model. The operator ?-> makes sure that get_category() won't be executed if get_images_categories() evaluates to null. https://stitcher.io/blog/php-8-nullsafe-operator

mikehageman avatar Jan 15 '21 06:01 mikehageman

@mikehageman thank you! We don't suppor the nullsafe operator yet. I'll put it on the backlog, cause it looks very much worth considering.

wisskid avatar Jan 15 '21 09:01 wisskid

I would be interested in implementing this. I assumed I simply needed to add & slightly change the same code that "PTR" uses, which seems to be true. I don't have any prior experience with yacc fwiw.

I do have a working change, but I'm not sure if I should make a pull request in this repo, or in https://github.com/smarty-php/smarty-lexer ?

Thanks!

j-applese3d avatar Dec 12 '22 20:12 j-applese3d

I would be interested in implementing this. I assumed I simply needed to add & slightly change the same code that "PTR" uses, which seems to be true. I don't have any prior experience with yacc fwiw.

I do have a working change, but I'm not sure if I should make a pull request in this repo, or in https://github.com/smarty-php/smarty-lexer ?

I've been looking at this too as I've wanted this feature in Smarty for years - long before it was even in PHP! It's insanely useful and reduces template code/increases readability massively!

Anyway, from what I have managed to gather so far, if it's something within the remit of smarty-lexer then it would go in that repo, as it seems (as far as I can figure) that that is then included in Smarty.

Snor avatar Jan 26 '23 20:01 Snor

@Snor your changes should go in the smarty repo. Take a look at the smarty5 branch, and specifically at the Makefile in the root. The template lexer and parser are generated from the .y and .plex sources. If you base your PR against the smarty5 branch, we'll have a nice extra feature to launch v5 with! 🚀

Be sure to add unit tests. I'll be happy to help.

wisskid avatar Jan 27 '23 17:01 wisskid

Ah, had not seen this, sorry. I'd prefer my version of TemplateParser.y because it keeps the rules simple (the parser is generally not in a perfect condition and should be looked at; too much redundancy).

As for res = $this->compiler->compileTag(i,a,array('modifierlist'=>l, 'object_method'=>me)); there is the same problem I stumbled upon. (AFAICS) this ultimately calls return "{$callbackObject}->{$function}"; in Compile/ObjectMethodBlockCompiler.php which should be made nullsafe depending on -> vs. ?-> in the template, so more work would be needed. here.

SlowFox71 avatar May 15 '24 06:05 SlowFox71