简体   繁体   English

在Smarty中定义AJAX返回的格式

[英]Define in Smarty the format to be returned by AJAX

I have a html form in a Smarty template. 我在Smarty模板中有一个html表单。 A select in the form is populated via AJAX. 表单中的select通过AJAX填充。 I would like to specify in the Smarty template, the format to be returned by the AJAX-call. 我想在Smarty模板中指定AJAX调用返回的格式。 This way I can use the same request-url for different purposes. 这样,我可以将相同的请求网址用于不同的目的。 For example if the select contains users, sometimes I might be interested in knowing the user's email address (format: "{$Option->GetName()} ({$Option->Email})" ), and other times the user's organisation (format: "{$Option->GetName()} ({$Option->Organisation})" ). 例如,如果select包含用户,有时我可能想知道用户的电子邮件地址(格式: "{$Option->GetName()} ({$Option->Email})" ),而有时是用户的组织(格式: "{$Option->GetName()} ({$Option->Organisation})" )。

I have a few ideas of how to achieve this: 我对如何实现这一点有一些想法:

  1. Store the format in html/js in the template. 将格式存储在模板中的html / js中。 Then the AJAX-call can send the desired return-format along with the request. 然后,AJAX调用可以将所需的返回格式与请求一起发送。 Drawback : A malicious user can change the format in the request, which might be a security issue. 缺点 :恶意用户可以更改请求中的格式,这可能是安全问题。
  2. Pass along the entire $Option object, and do the formatting using JavaScript. 传递整个$Option对象,并使用JavaScript进行格式化。 Drawback : Cannot use the PHP object's methods, such as $Option->GetName() . 缺点 :无法使用PHP对象的方法,例如$Option->GetName()
  3. Store the format in a separate template file, and reference to this file in html/js and send along with the AJAX request. 将格式存储在单独的模板文件中,并在html / js中引用此文件,并与AJAX请求一起发送。 Drawback : The reference to the template file can be changed by a malicious user, but probably no damage can be done. 缺点 :恶意用户可以更改对模板文件的引用,但可能不会造成损坏。 This approach requires extra template files, and the presentation logic will be spread over different files. 这种方法需要额外的模板文件,并且表示逻辑将散布在不同的文件中。
  4. Create a Smarty plugin that automates the process of item 3. Then the presentation logic will not be separated. 创建一个Smarty插件,该插件可以自动执行第3项的处理。然后,将不会分离表示逻辑。 Drawback : The reference to the template file can be changed by a malicious user, but probably no damage can be done. 缺点 :恶意用户可以更改对模板文件的引用,但可能不会造成损坏。

Do anyone have any other/better ideas, or any experience with doing something like this? 是否有人有其他/更好的主意,或有做类似事情的经验?

I don't think you need to set some format. 我认为您不需要设置某种格式。 You can create a javascript function with 3 parameters. 您可以使用3个参数创建一个javascript函数。 The <select> tag identifier, the name of the key used for options value, and the key name used for options text. <select>标记标识符,用于选项值的键的名称以及用于选项文本的键名。 The function will make the ajax request. 该函数将发出ajax请求。 That way you can call the function with settings you need for particular situation. 这样,您可以使用特定情况所需的设置来调用该函数。

Further details depend on your current code. 更多详细信息取决于您当前的代码。

I ended up using option 4, and I'm very happy with the result. 我最终使用了选项4,对此结果我感到非常满意。

If anyone come across this and need information on how I implemented it, just post a comment, and I'll try to explain. 如果有人遇到这个问题,并且需要有关我如何实现的信息,只需发表评论,我将尽力解释。

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

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