📜 ⬆️ ⬇️

django-php: php in django templates

django php In this post I will tell you how to make friends with Django and PHP using the django-php application.

We will need a php-cgi binary installed in the system. You can check its availability by typing in the terminal

$ which php-cgi 


Installation


django-php is available in PyPI and is installed in the usual way:
')
 $ pip install django_php #  $ easy_install django_php 

In the settings.py file, add 'django_php' to the INSTALLED_APPS list. In addition, you can specify (or you can not specify) the path to php-cgi:

 PHP_CGI = '/usr/local/bin/php-cgi' 

Using


In the template, load the php library:

 {% load php %} 

django-php provides us with two tags, {% php %} and {% startphp %}...{% endphp %} . Single line calls look like this:

 {% php echo 9; %} 

or, for example:

 {% php phpinfo(); %} 

or even like this:

 {% php for ($i = 0; $i < 8; ++$i) { %} <li>{% php echo $i; %}</li> {% php } %} 

For more extensive code sections, a block tag is used:

 {% startphp %} $str = '{{ str|addslashes }}'; $str = strrev($str); echo strtoupper($str); {% endphp %} 

Limitations of the alpha version


This is the very first version of django-php, and at the moment it cannot fcgi, cannot work with cookies, _GET , _POST , send headers.

The demo project is available in the repository ( link to GitHub ) along with the source code of the application itself.

Source: https://habr.com/ru/post/125773/


All Articles