HTML内的PHP无法使用Flask [英] PHP within HTML not working using Flask
问题描述
我最近刚开始使用Flask,并且没有以前的html / php经验,所以请原谅我,如果这太天真了!
我试图在html文件中使用一些php来改进我构建的webapp,但甚至无法获得最简单的测试用例。例如,从此网站进行测试用例:
- test_app.py -
import flask
app = flask.Flask('flask_test')
@ app.route('/')
def test():
return flask.render_template('test .html')
if __name__ =='__main__':
app.run()
- templates / test.html -
< html>
< head>< / head>
< body>
< ul>
<?php for($ i = 1; $ i <= 5; $ i ++){?>
< li>菜单项<?php echo $ i; ?>< /锂>
<?php}?>
< / ul>
< / body>
< / html>
- 预期产出 -
菜单项目1
菜单项目2
菜单项目3
菜单项目4
菜单项目5
- 实际产出 -
菜单物品
如果与Flask有内在的不兼容性?我是否忘记包含一些东西(无论是在python / flask方面还是在html / php方面),这些东西都可以一起工作?
谢谢!
您不会在Flask中使用PHP。如果您想在模板中使用某些脚本逻辑,请使用如下所示的内容:
< html>
< head>< / head>
< body>
< ul>
{%为范围内的我(1,6)%}
< li>菜单项目{{i}}< / li>
{%endfor%}
< / ul>
< / body>
< / html>
I have just recently started playing around with Flask and have no previous html/php experience, so please forgive me if this is naive!
I'm trying to use some php within an html file to improve a webapp I've built, but cannot even get the simplest test case to work. For example, take a test case from this site:
--test_app.py--
import flask
app = flask.Flask('flask_test')
@app.route('/')
def test():
return flask.render_template('test.html')
if __name__ == '__main__':
app.run()
--templates/test.html--
<html>
<head></head>
<body>
<ul>
<?php for($i=1;$i<=5;$i++){ ?>
<li>Menu Item <?php echo $i; ?></li>
<?php } ?>
</ul>
</body>
</html>
--expected output--
Menu Item 1
Menu Item 2
Menu Item 3
Menu Item 4
Menu Item 5
--actual output--
Menu Item
If there an inherent incompatibility with Flask? Am I forgetting to include something (either on the python/flask side or the html/php side) that will make it all work together?
Thanks!
You would not use PHP in Flask. If you want some scripting logic in your template, use something like this:
<html>
<head></head>
<body>
<ul>
{% for i in range(1,6) %}
<li>Menu Item {{ i }}</li>
{% endfor %}
</ul>
</body>
</html>
这篇关于HTML内的PHP无法使用Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!