-2

I have used Python and PHP a lot, but just out of curiosity, is there a version of Python that uses a similar paradigm to PHP for dynamic creation of html pages, i.e. like this:

<html>
<body>

<?py                        # similar to <?php ... ?>
for i in range(10):
    print '<div>Hello%i</div>' % i
?>

</body>
</html>

?


Note 1: I'm not speaking about Django, Flask, Bottle, Twisted, etc. that don't use such syntax.

Note 2: The suggested code would be like this in PHP:

<html>
<body>

<?php 
for ($i = 0; $i < 10; $i++)
{ 
    echo '<div>Hello' . $i . '</div>'; 
}
?>

</body>
</html>
Will Vousden
  • 32,488
  • 9
  • 84
  • 95
Basj
  • 41,386
  • 99
  • 383
  • 673
  • 2
    This article might be useful for you http://karrigell.sourceforge.net/en/pythoninsidehtml.html – styopdev Jan 11 '16 at 09:54
  • Exactly *what* are you trying to achieve with this code? – Sayse Jan 11 '16 at 10:01
  • 2
    pylatte -> https://github.com/rucifer1217/pylatte does <- what you want – Yoav Glazner Jan 11 '16 at 10:13
  • 3
    You should take the hint from the fact that none of the frameworks do it this way. It's a really bad idea. – Daniel Roseman Jan 11 '16 at 10:18
  • Yes you can create a sort of for loop in a django template, but I still don't understand what your question is about. – Sayse Jan 11 '16 at 10:19
  • 1
    We all understand what you mean, we are just pointing out that no-one in the Python world thinks it's a good idea to write code that way. – Daniel Roseman Jan 11 '16 at 10:23
  • I have used PHP once and I *don't* understand what you mean, are you trying to use variables in a template? for loops? The *exact* syntax you've shown above? Use the description part of your question to *describe* it – Sayse Jan 11 '16 at 10:23
  • That still doesn't really explain what you hope to achieve.... [Possible duplicate](http://stackoverflow.com/q/1107737/1324033) – Sayse Jan 11 '16 at 10:28
  • 1
    A Python Server Pages project that did this did exist, but has been dead for years. Again, take the hint. – Daniel Roseman Jan 11 '16 at 10:28
  • @Sayse: it has nothing to do with the question you link. What I want to achieve is to use some code *inside* HTML, in the same way PHP allows to use code *inside* HTML – Basj Jan 11 '16 at 10:30
  • I give up and agree with Daniel Roseman, Django has a template syntax, as do other python packages. Please read [ask]. Your question is unclear. – Sayse Jan 11 '16 at 10:32

1 Answers1

-1

Is there a version of Python that uses a similar paradigm to PHP for dynamic creation of html pages

No, python cannot be embedded in html like the way you want. The idea is to separate the view (html) from the code (python).

Have a look at how template engine works, they can allow fairly complex amount of python code.

Example with Jinja:

    <ul>
        {% for user in users %}
            <li><a href="{{ user.url }}">{{ user.username }}</a></li>
        {% endfor %}
    </ul>

Other solutions closer to what you want will convert the html to a python script that will produce the fully interpreted html. It's a bit of an overkill ...

Cyrbil
  • 6,341
  • 1
  • 24
  • 40