Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP within HTML not working using Flask

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!

like image 815
Daniel Johnson Avatar asked Feb 11 '26 14:02

Daniel Johnson


1 Answers

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>
like image 198
Matt Healy Avatar answered Feb 14 '26 03:02

Matt Healy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!