简体   繁体   English

如何在Smarty中使用json_decode

[英]How to use json_decode with Smarty

Its my first time working with Smarty Template Engine and I am having troubles translating the following PHP json_decode statement into a Smarty friendly code. 这是我第一次使用Smarty模板引擎 ,我遇到麻烦,将以下PHP json_decode语句转换为Smarty友好代码。 The output is an array since I use the TRUE in the json_decode. 输出是一个数组,因为我在json_decode中使用TRUE。

$itemArray = json_decode($dni-content-slider-id-prefix-saved-content-picker-item- . $promos, true)

I've tried 我试过了

{ assign var  $itemArray = value=$dni-content-slider-id-prefix-saved-content-picker-item-|cat:$promos|json_decode}

But it doesnt really work. 但它并没有真正起作用。 any suggestions? 有什么建议么?

Pretty much everything in that line of Smarty is wrong; 几乎所有Smarty系列中的一切都是错误的; let's go through in order: 让我们按顺序完成:

{ assign

Don't put a space between the { and the Smarty tag; 不要在{和Smarty标签之间留一个空格; if Smarty 3's "auto-literal" feature is switched on, it will assume that's a literal { not a Smarty tag. 如果Smarty 3的“自动文字”功能打开,它将假设这是一个文字{不是Smarty标签。

var  $itemArray = 

You seem to be mixing two different functions here. 你似乎在这里混合了两个不同的功能。 The Smarty {assign} function takes the form {assign var=some_name value=$some_value} . Smarty {assign}函数采用{assign var=some_name value=$some_value} Note that the var parameter is the name of the variable to assign, so does not need a $ . 请注意, var参数是要分配的变量的名称 ,因此不需要$

Smarty 3 also has a PHP-style "short-form assign" , which looks like {$some_name=$some_value} (complete with $ , but no assign keyword). Smarty 3还有一个PHP风格的“短格式赋值” ,看起来像{$some_name=$some_value} (带有$ ,但没有assign关键字)。

value=$dni-content-slider-id-prefix-saved-content-picker-item-|cat:$promos

This will take the content of the variable $dni-content-slider-id-prefix-saved-content-picker-item- treat it as a string, and add the content of the variable $promos (also treated as a string) to the end of it. 这将采用变量$dni-content-slider-id-prefix-saved-content-picker-item-将其视为字符串,并将变量$promos (也被视为字符串)的内容添加到结束了。 Looking again, I see that that is also what your PHP code does, but it seems a very odd thing to do, since the first variable would have to be something like "{'foo':'bar'," and the second something like "'baz':'quux'}" . 再看一遍,我看到这也是你的PHP代码所做的,但这似乎是一件非常奇怪的事情,因为第一个变量必须是"{'foo':'bar'," ,第二个变量比如"'baz':'quux'}" Why would you ever have variables like that? 为什么你会有这样的变量?

Based on the PHP code being the same, I'm going to assume this paragraph was wrong on my part What I suspect you want is a variable variable name (a different variable when the code runs); 基于PHP代码是相同的,我将假设这段我错了我 怀疑你想要的是一个变量变量名(代码运行时的另一个变量); there are ways to do that, but this isn't one of them. 有办法做到这一点,但这不是其中之一。 It's also generally a really bad idea; 这通常也是一个非常糟糕的主意; if you want lots of similar variables which you can select from at run-time, put them in an associative array, and index something like $dni-content-slider-id-prefix-saved-content-picker-items[$promos] . 如果你想要很多类似的变量你可以在运行时选择,把它们放在一个关联数组中,并索引像$dni-content-slider-id-prefix-saved-content-picker-items[$promos]

|json_decode}

Finally, the part actually related to your question. 最后,该部分实际上与您的问题有关。 You point out that in PHP, you pass the optional true parameter to the json_decode function, but in this Smarty code, you are not doing so. 你指出在PHP中,你将可选的true参数传递给json_decode函数,但在这个Smarty代码中,你没有这样做。 This can be easily done by adding :true on the end, as in {assign var=itemArray value=$jsonString|json_decode:true} . 这可以通过在末尾添加:true来轻松完成,如{assign var=itemArray value=$jsonString|json_decode:true}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM