smarty模板可扩展编程

本文介绍了一种使用PHP结合Smarty模板引擎实现数据展示的方法。通过解析JSON数据并将其转化为易于处理的数组形式,最终利用Smarty模板进行高效渲染。具体涉及如何设置Smarty配置、组织数据结构及模板语法应用等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

view层

<?php
    require "../libs/Smarty.class.php";
	require "abc.php";
    $smarty = new Smarty();

    $smarty -> setTemplateDir("../tpls");
    $smarty -> setCompileDir("../coms");

    $smarty -> assign("title","速评表");
//打这个json数据累死我了
    $result = '{
        "service_type": "car",
        "view": {
            "info": {
                "tag_name": "基本信息",
                "data": {
                    "car_jigou": {
                        "explain": "合作机构",
                        "type": "text",
                        "options": "",
                        "value": "合作机构A"
                    },
                    "car_contract": {
                        "explain": "合同类型",
                        "type": "text",
                        "options": "",
                        "value": "类型B"
                    },
                    "car_chexing": {
                        "explain": "车型",
                        "type": "text",
                        "options": "",
                        "value": "豪华"
                    },
                    "car_brand": {
                        "explain": "品牌",
                        "type": "text",
                        "options": "",
                        "value": "宝马"
                    },
                    "car_xinghao": {
                        "explain": "型号",
                        "type": "text",
                        "options": "",
                        "value": "740li"
                    },
                    "car_licheng": {
                        "explain": "行驶里程",
                        "type": "text",
                        "options": "",
                        "value": "123456"
                    },
                    "car_pailiang": {
                        "explain": "排量",
                        "type": "text",
                        "options": "",
                        "value": "4.0"
                    },
                    "car_color": {
                        "explain": "车身颜色",
                        "type": "text",
                        "options": "",
                        "value": "A123456"
                    },
                    "car_number": {
                        "explain": "车牌号",
                        "type": "text",
                        "options": "",
                        "value": "京A123456"
                    },
                    "car_chejiahao": {
                        "explain": "车架号",
                        "type": "text",
                        "options": "",
                        "value": "adadg123333"
                    },
                    "car_chuchang": {
                        "explain": "出厂日期",
                        "type": "text",
                        "options": "",
                        "value": "2013.11"
                    },
                    "car_chudeng": {
                        "explain": "初登日期",
                        "type": "text",
                        "options": "",
                        "value": "2015.3"
                    },
                    "car_yuanjia": {
                        "explain": "抵押物原价",
                        "type": "text",
                        "options": "",
                        "value": "70.8"
                    },
                    "car_gujia": {
                        "explain": "抵押物估价",
                        "type": "text",
                        "options": "",
                        "value": "65.5"
                    },
                    "car_jianyijiekuan": {
                        "explain": "合作机构建议借款",
                        "type": "text",
                        "options": "",
                        "value": "60.3"
                    }
                }
            }
        }
    }';

    $decode = json_decode($result,true);  //变成数组了
	$data=array();
	foreach($layout as $key=>$kuai){
		$datakuai=array();
		$temp=$decode['view'][$key];
		foreach($kuai as $hang){
			$datahang=array();
			foreach($hang as $lie){
				$a=$temp['data'];
				$datahang[$a[$lie]['explain']]=$a[$lie]['value'];
			}
			array_push($datakuai,$datahang);
			//$datakuai.push($datahang);
		}
		$data[$temp['tag_name']]=$datakuai;
		//$data.push($datakuai);
	}
    $smarty -> assign("mycontent2",$data);

    // $smarty -> assign("mycontent2",["速评表详情"=>[["合作机构"=>"abc","合同类型"=>"efg"],
    // ["车型"=>"豪华","品牌"=>"宝马","型号"=>"740li","行驶里程"=>"21564"],["排量"=>"3.0l","车身颜色"=>"白色","车牌号"=>"京A54445","车架号"=>"213435435asdw"],
    // ["出厂日期"=>"2014.11","初登日期"=>"2015.4"]],
    // "议价"=>[["抵押物原价"=>"70","抵押物评估价"=>"65","合作机构建议借款"=>"67"]]]);
    // $smarty ->assign("mycontent",$arr);
	// $smarty-> assign("id",["name","job","kind","address","skill1","skill2","skill3","skill4","name","job","kind","address","skill1","skill2","skill3","skill4"]);
    $smarty -> display("test.tpl");

tpl模板

<html>
	<head>
		<title>{$title}</title>
		<link href="/loan_oa/web/css/bootstrap.min.css" rel="stylesheet">
		<link rel="stylesheet" href="/loan_oa/web/css/mystyle.css">
	</head>
	
	<body>
	{$index=0}
	<div class="infowrap clearfix">
		<div class="infoleft">
			{foreach $mycontent2 as $c=>$mycontent}
				<div class="infoblock mgt">
					<h3>{$c}</h3>
					<table class="table">
						{foreach $mycontent as $content}
						{$length=(24-count($content)*2)/count($content)}
						<tr>
							{foreach $content as $a=>$b}
								<td class="bggray" colspan="3">{$a}</td> 
								<td colspan="{$length}" >{$b}</td>
								{$index=$index+1}
							{/foreach}
						 </tr>
						{/foreach}
					</table>
				</div>
			{/foreach}
		</div>
	</div>


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值