Python(使用Flask框架)
Flask视图函数示例(游戏列表)
python
from flask import Flask, render_template
app = Flask(name)
@app.route(‘/’)
def game_list():
games = [
{‘name’: ‘Game 1’, ‘price’: 10.99},
{‘name’: ‘Game 2’, ‘price’: 19.99},
# … 更多游戏
]
return render_template(‘game_list.html’, games=games)
if name == ‘main’:
app.run(debug=True)
HTML模板(game_list.html)
html
Game List
- {% for game in games %}
- {{ game.name }} - ${{ game.price }}
- {% endfor %}
javascript
const express = require(‘express’);
const app = express();
app.get(‘/’, (req, res) => {
const games = [
{ name: ‘Game 1’, price: 10.99 },
{ name: ‘Game 2’, price: 19.99 },
// … 更多游戏
];
res.render(‘game_list’, { games });
});
// 需要设置模板引擎(如EJS、Pug等)和静态文件路径
// … 其他中间件和配置
app.listen(3000, () => console.log(‘Server started on port 3000’));
EJS模板(game_list.ejs)
ejs
Game List
- <% games.forEach(function(game){ %>
- <%= game.name %> - $<%= game.price %>
- <% }); %>
java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Arrays;
import java.util.List;
@Controller
public class GameController {
@GetMapping("/")
public String gameList(Model model) {
List<Game> games = Arrays.asList(
new Game("Game 1", 10.99),
new Game("Game 2", 19.99)
// ... 更多游戏
);
model.addAttribute("games", games);
return "game_list";
}
// 假设有一个简单的Game类
static class Game {
String name;
double price;
// 构造函数、getter和setter...
}
}
Thymeleaf模板(game_list.html)
#chhas{
margin-top: 50px;
padding:machinefireworks.cn;
font-size: 18px;
cursor: 10px 20px;
}
html
Game List
- -