Skip to content
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

Json encode twig variables #7

Merged
merged 1 commit into from
Aug 8, 2024
Merged

Json encode twig variables #7

merged 1 commit into from
Aug 8, 2024

Conversation

LucaGallinari
Copy link
Member

It's safer and better to use the json encoder for all variables that are passed to twig files

Copy link
Member

@lruozzi9 lruozzi9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ti possible to encode parameters before passing them to twig? Instead of adding so many json_encode and raw filters?

@LucaGallinari
Copy link
Member Author

LucaGallinari commented Jul 24, 2024

We could do that, but the only advantage of that is that we have clean templates, beside this there will be only cons:

  • It would be bit tricky to implement conditions in twig (if/else) cause the data will be json encoded. Examples: string will have double double quotes (like ""search term"" instead of "search term"), some characters will have escape chars etc.
  • Array and objects should not be encoded when passing them to the templates otherwise you will lose the ability to perform if/for on the template. This way you will still have to do the |json_encode|raw in templates for these values.
  • Given the two above you would have some template parameters encoded to json and some not encoded.... this is horrible. Encoding should be done only in "one point", in the PHP logic or in the template.

The best solution that i can think of is that we should not "compose" the json ourself but it should be encoded via a single json_encode from a single PHP array/object: to do this we should transform all "yaml templates" to "php templates" that return an array/object. But this requires a bit of rewriting.

@lruozzi9
Copy link
Member

The best solution that i can think of is that we should not "compose" the json ourself but it should be encoded via a single json_encode from a single PHP array/object: to do this we should transform all "yaml templates" to "php templates" that return an array/object. But this requires a bit of rewriting.

The goal of having JSON files is to write queries in the same syntax as Elasticsearch (see https://www.mon-code.net/post/125/use-elasticsearch-with-symfony-framework-and-elasticsearch-php-library#:~:text=Symfony%20debug%20Toolbar.-,Using%20Twig,-All%20queries%20need). Probably it is ok for now.

@lruozzi9 lruozzi9 merged commit 7e35912 into main Aug 8, 2024
8 of 16 checks passed
@lruozzi9 lruozzi9 deleted the encode-json-twig branch August 8, 2024 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants