smarty 模拟 for 循环

本文介绍如何在Smarty模板引擎中实现简单的循环操作,通过使用section标签来模拟传统的for循环,解决了直接循环整数的需求。

问:
$mySmarty->assign('sCount', 20); 
$mySmarty->display('xxx.tpl'); 

我想要的很简单,就是能在xxx.tpl中循环$sCount变量,就像类似于下面的PHP循环形式: 

for ($i = 0; $i < $sCount; $i++) 

...... 


每个语言都有循环语句,smarty(暂且勉强归类到语言中)也不例外,但是查了手册上的资料,竟然全部都是关于数组循环的语句,难道我要想循环一个100次的语句,就要成立一个数组,赋值100次?循环1000次,5000次呢?难道就没有最简单、最原始的办法了? 

-----------------------------
答:
用一段section模拟 
{section name=loop loop=$count} 
id: {$smarty.section.loop.index} 
{/section} 


给count赋个值 
$smarty->assign('count', 5);

✓ 客服系统登录 请使用您的账号登录系统 ✓ 登录成功!正在跳转到控制面板... 用户名 admin 密码 •••••• 登录 快速链接 帮助中心 找回密码 用户注册 联系我们 客服系统 v2.5.1 © 2023 [root@yfw ~]# cd /www/wwwroot/szrengjing.com/kefu [root@yfw kefu]# ll total 80 drwxr-xr-x 2 www www 4096 Aug 6 20:14 cache -rwxr-xr-x 1 www www 5060 Aug 6 19:41 config.php drwxr-xr-x 4 root root 4096 Aug 6 14:46 includes -rwxr-xr-x 1 www www 48150 Aug 6 20:11 index.php drwxr-xr-x 3 root root 4096 Aug 6 14:24 js drwxr-xr-x 2 www www 4096 Aug 6 20:15 logs drwxr-xr-x 2 root root 4096 Aug 6 20:16 templates drwxr-xr-x 2 www www 4096 Aug 6 21:04 templates_c [root@yfw kefu]# <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>客服系统 - 登录页面</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } body { background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d); min-height: 100vh; display: flex; justify-content: center; align-items: center; padding: 20px; } .container { width: 100%; max-width: 500px; background: rgba(255, 255, 255, 0.95); border-radius: 15px; box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3); overflow: hidden; } .header { background: #2c3e50; color: white; padding: 25px 30px; text-align: center; position: relative; } .header h1 { font-size: 24px; margin-bottom: 10px; display: flex; align-items: center; justify-content: center; gap: 10px; } .header h1 i { font-size: 28px; } .content { padding: 30px; } .message-container { margin-bottom: 25px; padding: 15px; border-radius: 8px; display: flex; align-items: center; gap: 12px; font-size: 16px; } .info { background-color: #e3f2fd; color: #0d47a1; border-left: 4px solid #2196f3; } .success { background-color: #e8f5e9; color: #2e7d32; border-left: 4px solid #4caf50; } .error { background-color: #ffebee; color: #c62828; border-left: 4px solid #f44336; } .error-details { margin-top: 10px; padding: 10px; background: #fff; border-radius: 5px; border: 1px solid #ffcdd2; font-size: 14px; } .form-group { margin-bottom: 20px; } .form-group label { display: block; margin-bottom: 8px; font-weight: 500; color: #333; } .form-control { width: 100%; padding: 14px; border: 1px solid #ddd; border-radius: 8px; font-size: 16px; transition: border-color 0.3s; } .form-control:focus { outline: none; border-color: #3498db; box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2); } .btn { display: block; width: 100%; padding: 14px; background: linear-gradient(to right, #3498db, #2980b9); color: white; border: none; border-radius: 8px; font-size: 18px; font-weight: 600; cursor: pointer; transition: all 0.3s; } .btn:hover { background: linear-gradient(to right, #2980b9, #2573a7); transform: translateY(-2px); box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); } .links-container { margin-top: 25px; padding-top: 20px; border-top: 1px solid #eee; } .links-container h3 { margin-bottom: 15px; color: #2c3e50; text-align: center; } .link-list { display: flex; flex-wrap: wrap; gap: 15px; justify-content: center; } .link-item { display: inline-block; padding: 10px 20px; background: #f8f9fa; border-radius: 30px; color: #3498db; text-decoration: none; font-weight: 500; transition: all 0.3s; border: 1px solid #e9ecef; } .link-item:hover { background: #3498db; color: white; transform: translateY(-3px); box-shadow: 0 5px 15px rgba(52, 152, 219, 0.3); } .footer { text-align: center; padding: 20px; background: #f8f9fa; color: #6c757d; font-size: 14px; border-top: 1px solid #e9ecef; } @media (max-width: 576px) { .content { padding: 20px 15px; } .header { padding: 20px; } .link-list { flex-direction: column; align-items: center; } } </style> </head> <body> <div class="container"> <div class="header"> <h1><i class="icon">✓</i> 客服系统登录</h1> <p>请使用您的账号登录系统</p> </div> <div class="content"> <!-- 消息提示区域 - 修复了Smarty语法错误 --> <div class="message-container success"> <span class="icon">✓</span> <div class="message"> 登录成功!正在跳转到控制面板... </div> </div> <!-- 登录表单区域 - 修复了Smarty语法错误 --> <form id="loginForm"> <div class="form-group"> <label for="username">用户名</label> <input type="text" id="username" class="form-control" placeholder="请输入用户名" value="admin"> </div> <div class="form-group"> <label for="password">密码</label> <input type="password" id="password" class="form-control" placeholder="请输入密码" value="••••••"> </div> <button type="submit" class="btn">登录</button> </form> <!-- 链接区域 - 修复了Smarty语法错误 --> <div class="links-container"> <h3>快速链接</h3> <div class="link-list"> <a href="#" class="link-item">帮助中心</a> <a href="#" class="link-item">找回密码</a> <a href="#" class="link-item">用户注册</a> <a href="#" class="link-item">联系我们</a> </div> </div> </div> <div class="footer"> 客服系统 v2.5.1 © 2023 </div> </div> <script> // 模拟Smarty模板功能 document.getElementById('loginForm').addEventListener('submit', function(e) { e.preventDefault(); const username = document.getElementById('username').value; const password = document.getElementById('password').value; // 模拟不同消息类型 const messageContainer = document.querySelector('.message-container'); const messageIcon = messageContainer.querySelector('.icon'); const messageText = messageContainer.querySelector('.message'); if(username === 'admin' && password === 'password123') { messageContainer.className = 'message-container success'; messageIcon.textContent = '✓'; messageText.textContent = '登录成功!正在跳转到控制面板...'; } else { messageContainer.className = 'message-container error'; messageIcon.textContent = '✗'; messageText.innerHTML = '登录失败!用户名或密码错误<br><div class="error-details">错误详情:用户不存在或密码不匹配</div>'; } }); </script> </body> </html> 根据信息 请生成对应源文件完整版 全面兼容PHP5.3.29
08-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值