|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
andrewFountain Smarty n00b
Joined: 31 Aug 2018 Posts: 3
|
Posted: Wed Apr 24, 2019 5:10 am Post subject: json encode - pass multiple bitmask constants for formatting |
|
|
Hi,
I'm wondering if there is any way to pass multiple bitmask constants when formatting using json_encode in a template.
The json structure has been assigned to the page as an associative array that cannot easily be converted in the php side of the application, so I'm using json_encode inside the template.
Unfortunately as the bitmask uses pipes to separate the constants in php which is syntax for a function in smarty eg.
PHP
this syntax will work in php
Code: | $json = [
'one' => 1,
'arr' => [
'two' => 2
]
];
echo json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPE_SLASHES);
|
SMARTY
this syntax won't work in smarty as the `|` "pipe" is used to call a function on the value
Code: | {$json = [
'one' => 1,
'arr' => [
'two' => 2
]
]}
{$json|json_encode:$smarty.const.JSON_PRETTY_PRINT|$smarty.const.JSON_PRETTY_PRINT}
|
I have tried to pass the constants as arguments and as items in an array but neither solution worked.
Code: | {$json = [
'one' => 1,
'arr' => [
'two' => 2
]
]}
{$json|json_encode:$smarty.const.JSON_PRETTY_PRINT:$smarty.const.JSON_PRETTY_PRINT}
|
Code: | {$json = [
'one' => 1,
'arr' => [
'two' => 2
]
]}
{$json|json_encode:[$smarty.const.JSON_PRETTY_PRINT, $smarty.const.JSON_PRETTY_PRINT]}
|
Is there any way that I can pass more than one constant to json_encode?
** note **
We have disabled the {php}{/php} tag as it's a security concern for us to allow integrators to execute unknown php in their templates.
Edit: would it be better to create a modifier to perform a json encode.
Code: | function smarty_modifier_json($json, ...$constants) {
$jsonConstants = [
'JSON_PRETTY_PRINT' => JSON_PRETTY_PRINT,
'JSON_UNESCAPE_SLASHES' => JSON_UNESCAPE_SLASHES,
// add more constants here
];
$validConstants = array_filter($constants, function($constant) {
return !empty($jsonConstants[$constant]);
});
$cominedConstants = array_reduce($validConstants , function($acc, $constant) {
return $acc | $constant;
}, 0);
return json_encode($json, $cominedConstants);
}
|
Code: | {$json|json:'JSON_PRETTY_PRINT':'JSON_UNESAPE_SLASHES'} |
Cheers,
Andrew[/code] |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Apr 24, 2019 7:15 pm Post subject: |
|
|
Use braces.
But I have a better question: Why do you use Smarty at all for JSON encoding? |
|
Back to top |
|
andrewFountain Smarty n00b
Joined: 31 Aug 2018 Posts: 3
|
Posted: Fri Apr 26, 2019 4:38 am Post subject: |
|
|
Because it's takes a long time to get anything changed in the back end and we have so many modules without any underlying consistent framework so to add a new method to anything takes a very long time, and this gives me full control of structuring my json before consuming it.
The answer is actually very simple. You can just add the different bitwise constants as that is all the Or operator actually does.
Code: | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT === 196
JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT) === 196
196 === 0b11000100 |
Code: | {$json|json_encode:(
$smarty.const.JSON_UNESCAPED_SLASHES
+ $smarty.const.JSON_PRETTY_PRINT
)}
|
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Apr 27, 2019 5:46 pm Post subject: |
|
|
That's only true if operands are single different bits.
But try to compare
Code: | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_SLASHES |
versus
Code: | JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_SLASHES |
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Apr 27, 2019 7:29 pm Post subject: |
|
|
After trying your issue in sandbox, I understand the origin better.
Smarty uses "|" as filter separator, and there seems to be no way to escape it.
Your only options is to set encoding options from PHP as a variable or supply them numerically in template. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|