简体   繁体   English

如何在smarty模板中显示json数据?

[英]How to display json data in a smarty template?

I'm using smarty and php for my website. 我的网站使用smarty和php。 There is some code which converts the data into json. 有一些代码将数据转换为json。 I want to use that data in a smarty template. 我想在一个聪明的模板中使用该数据。 That data is a set of error messages and I want to display those messages in a assigned smarty template at desired ID. 该数据是一组错误消息,我想以所需的ID在分配的smarty模板中显示这些消息。 I'm not able to get that in smarty template. 我无法在smarty模板中得到它。 Actually what is happening is the error messages are displayed on a plain page instead in the desired tag. 实际上发生的是错误消息显示在普通页面上而不是所需的标记中。 Following is my smarty and PHP code: Following is the smarty code where I want to display the error messages: 以下是我的smarty和PHP代码:以下是我要显示错误消息的smarty代码:

if $error_msg}<div class="error-info">{$error_msg.error_msgs}</div>{/if}

Now following is my PHP code: 现在,下面是我的PHP代码:

<?php
if($request['form_submitted']=='yes') {
                $ret = $objPracticeSheet->InsertPracticeSheet($request, $practice_sheet_error_messages);
                if(!$ret) { 
                    $error_msg  = $objPracticeSheet->GetAllErrors();
                    $data = array();
                    $data['error_message'] = $error_msg['error_msgs'];
                    $data = json_encode($data);
                    echo $data;
                    die;
                } else {
                    $data = array();
                    $data['success_message'] = "success";
                    $data = json_encode($data);
                    echo $data;
                    die;
                }
            } else { 

                $all_subjects = $objSubjectsTopicsQues->GetAllSubjectsHavingTopics();
                $smarty->assign('all_subjects', $all_subjects);
                $smarty->assign('sheet_type', 'practice');
                $bread_crumbs_text = 'Add Practice Sheet';
                $submit_value      = 'Submit';
                $cancel_value      = 'Cancel';
                $file_to_show      = 'manage-practice-sheet.tpl';
            }
$smarty->assign("op", $op);
    $smarty->assign("query_string", $query_string);
    $smarty->assign("bread_crumbs_text", $bread_crumbs_text);
    $smarty->assign("submit_value", $submit_value);
    $smarty->assign("cancel_value", $cancel_value);
  $smarty->assign("error_msg", $error_msg);
  $smarty->assign("file_to_show", $file_to_show);
  /*$smarty->assign('create', '-active');
  $smarty->assign("sub_menu_file", "epn-create-sub-menu.tpl");
  $smarty->assign('practice_sheet', '-active');*/
  $smarty->assign('practice_sheet', 'active');
  $smarty->assign('prepare', 'selected');
  $smarty->display("index.tpl");
?>

Can you help me in displaying the error messages json data in the above div? 您能帮我在上述div中显示错误消息json数据吗? Thanks in advance. 提前致谢。 I'm also attaching the screenshot of the current output. 我还要附上当前输出的屏幕截图。 在此处输入图片说明

Use JavaScript to parse the JSON, then put the errors in the div. 使用JavaScript解析JSON,然后将错误放入div。

To make this easier, let's give your div an id : 为了简化操作,我们给div一个id

<div id="error-info-main" class="error-info"></div>

Then 然后

<script>
{literal}
(function() {
    var error_json = {/literal}{$error_msg.error_msgs}{literal};
    var errors = JSON.parse(error_json);
    document.getElementById('error-info-main').innerHTML = errors.error_message;
}());
{/literal}
</script>

Alternatively, you could just the pass the message itself from PHP: 或者,您可以只通过PHP传递消息本身:

$smarty->assign("error_msg", $error_msg->error_message);

This may not be 100% correct, as I don't have your data to test with, but I believe that's right. 这可能不是100%正确的,因为我没有可以测试的数据,但是我认为这是正确的。 You get the idea at any rate. 无论如何,您都会想到这个主意。

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

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