Created
November 30, 2012 15:41
-
-
Save baldurthoremilsson/4176505 to your computer and use it in GitHub Desktop.
DjangoPHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import subprocess | |
from django.template import Library, Node | |
register = Library() | |
class PHPNode(Node): | |
def __init__(self, nodelist): | |
self.nodelist = nodelist | |
def render(self, context): | |
code = "".join([n.s for n in self.nodelist]) | |
proc = subprocess.Popen(['php', '-r', code], stdout=subprocess.PIPE) | |
return proc.stdout.read() | |
@register.tag | |
def php(parser, token): | |
nodelist = parser.parse(('endphp',)) | |
parser.delete_first_token() | |
return PHPNode(nodelist) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load php %} | |
{% php %} | |
for($i = 0; $i < 10; $i++) { | |
echo $i."<br>"; | |
} | |
{% endphp %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment