Opened 18 years ago

Closed 18 years ago

Last modified 17 years ago

#2440 closed enhancement (wontfix)

Template Loader for PHP files (integrate django into existing PHP site)

Reported by: django@… Owned by: Adrian Holovaty
Component: Template system Version: dev
Severity: normal Keywords: template loader PHP
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

There are a number of sites out there which already have a PHP framework for the base page generation.
Some of them use other tools like Zope, RoR or J2EE to do CMS style applications for which PHP is not a good fit.
Switching to django is difficult because there is no easy way to integrate the two.

For PyCon06 we created a custom template loader.

A template name prefixed with 'php:' denotes passing the template throught the PHP process and returning the result.
The initial template is loaded from the loader system as well.
This worked better than a custom inclusion tag because it works with the base template loader calls and regular template include tags.
(Some django templates get the sub-template name to include from the context and do not know to load some custom tag or to use a custom include tag)

The base PHP template (index.php) needs to be accessable via TEMPLATE_DIRS and has html like:

<html>
<head>
<title><!-- {% block title %} -->PyCon<-- {% endblock %} --></title>
</head>
<body>
<!-- imagine a bunch of menu and other html here -->
<!-- {% block body %} -->
<!-- {% endblock %} -->
</body>
</html>

for example, and can be used for all the regular php generated pages, or when included in a django template, overloaded.

In this example you need to remember to do something like:

{% include "php:index.php" %}
{% block body %} -->
<!-- django template code here -->
-->{% endblock %}

The loader has been converted to 9.5, works on both unix and windows, is highly configurable, and can use the cache system (because PHP executions may be expensive). It does not use any temporary files (uses the subprocess module and pipes the stdin/stdout).

Due to complications with windows IO blocking, threading is used by default on that OS.

# optional settings.py options for this loader:
PHP_BIN           # defaults to: unix='/usr/bin/php', win32='php.exe')
PHP_ARGS          # defaults to: ['-q',] (supress http header)
PHP_IN_SHELL      # defaults to: True (execute in sub shell with full env?)
PHP_THREAD_IO     # defaults to: unix=False, win32=True (use async io on spawned php process)
PHP_BUFFER_SIZE   # defaults to: unix=4096, win32=-1 (io memory buffer size)
PHP_CACHE_SECONDS # defaults to: None (set to >0 and will cache on template name (including 'php:')

Was hoping it would be added as django/template/loaders/php.py

Attachments (2)

php.py (3.6 KB ) - added by django@… 18 years ago.
custom php template loader. Add to TEMPLATE_LOADERS
AJSHP.js (6.4 KB ) - added by anonymous 17 years ago.

Download all attachments as: .zip

Change History (6)

by django@…, 18 years ago

Attachment: php.py added

custom php template loader. Add to TEMPLATE_LOADERS

comment:1 by django@…, 18 years ago

Just realized that because it uses subprocess it is python2.4 specific.
Could use some help converting it to os.popen2. (which has other stdin issues I could not resolve).

comment:2 by Malcolm Tredinnick, 18 years ago

Resolution: wontfix
Status: newclosed

This is not something we want to put into core. However, since configuring a system to use external loaders is a settings.py configuration, feel free to post a link on the Wiki so that people can download it if they have the need.

comment:3 by anonymous, 17 years ago

I couldn't get it to work until I put it first in TEMLPATE_LOADERS

by anonymous, 17 years ago

Attachment: AJSHP.js added

comment:4 by Armin Ronacher, 17 years ago

I ported django like templates some time ago to php: http://lucumr.pocoo.org/trac/repos/ptemplates/
Note that i do not support it, if you find bugs file a ticket in the lucumr trac (http://lucumr.pocoo.org/trac/).

Have fun anyway.

Note: See TracTickets for help on using tickets.
Back to Top