- Python (Flask 框架)junjie8.cn
python
from flask import Flask, render_template
app = Flask(name)
products = [
{“id”: 1, “name”: “产品A”, “price”: 100},
{“id”: 2, “name”: “产品B”, “price”: 200},
# … 更多产品
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
if name == ‘main’:
app.run(debug=True)
HTML (templates/index.html):
html
商品列表
- {% for product in products %}
- {{ product.name }} - 价格: {{ product.price }}
- {% endfor %}
const products = [
{id: 1, name: ‘产品A’, price: 100},
{id: 2, name: ‘产品B’, price: 200},
// … 更多产品
];
app.set(‘view engine’, ‘ejs’);
app.get(‘/’, (req, res) => {
res.render(‘index’, {products: products});
});
app.listen(3000, () => {
console.log(‘商城已启动,端口 3000’);
});
EJS (views/index.ejs):
html
商品列表
- <% products.forEach(function(product){ %>
- <%= product.name %> - 价格: <%= product.price %>
- <% }); %>
java
@Controller
public class ProductController {
private List<Product> products = Arrays.asList(
new Product(1, "产品A", 100),
new Product(2, "产品B", 200)
// ... 更多产品
);
@GetMapping("/")
public String index(Model model) {
model.addAttribute("products", products);
return "index";
}
}
Thymeleaf (src/main/resources/templates/index.html):
html
商品列表
- 产品名称 - 价格: 价格
- Python (Flask 框架)
python
from flask import Flask, render_template
app = Flask(name)
products = [
{“id”: 1, “name”: “产品A”, “price”: 100},
{“id”: 2, “name”: “产品B”, “price”: 200},
# … 更多产品
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
if name == ‘main’:
app.run(debug=True)
HTML (templates/index.html):
html
商品列表
- {% for product in products %}
- {{ product.name }} - 价格: {{ product.price }}
- {% endfor %}
const products = [
{id: 1, name: ‘产品A’, price: 100},
{id: 2, name: ‘产品B’, price: 200},
// … 更多产品
];
app.set(‘view engine’, ‘ejs’);
app.get(‘/’, (req, res) => {
res.render(‘index’, {products: products});
});
app.listen(3000, () => {
console.log(‘商城已启动,端口 3000’);
});
EJS (views/index.ejs):
html
商品列表
- <% products.forEach(function(product){ %>
- <%= product.name %> - 价格: <%= product.price %>
- <% }); %>
java
@Controller
public class ProductController {
private List<Product> products = Arrays.asList(
new Product(1, "产品A", 100),
new Product(2, "产品B", 200)
// ... 更多产品
);
@GetMapping("/")
public String index(Model model) {
model.addAttribute("products", products);
return "index";
}
}
Thymeleaf (src/main/resources/templates/index.html):
html
商品列表
- 产品名称 - 价格: 价格