3

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!

2
  • have you try to change your template to test.php instead of test.html? Commented Jul 4, 2015 at 2:17
  • hmm, is there any reason to use PHP with flask? as flask already support its own template solution Commented Jul 4, 2015 at 2:31

3 Answers 3

5

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>
Sign up to request clarification or add additional context in comments.

Comments

0

Answer to question by Matt Healy is right.

But looking at your code I seem problem it should be like this

<ul> 
 <?php 
    for($i=1;$i<=5;$i++){ 
      echo "<li>Menu Item ".$i."</li>"; 
    } 
 ?>
</ul>

And make sure you have .php as the file extension. Because Php will not run in HTML file.

Comments

0

The PHP interpreter does not work the way you want to use it. You are calling the HTML (template) file from within a flask app, thus not using the PHP interpreter. You best stick to PHP with supportet templating eg. smarty, or go for Pythonic. Trying mixing the two is a bad idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.