Version 2 (modified by riklaunim@…, 9 years ago) (diff)

extra example added

Using Myghty templates in Django is easy.

  1. Install Myghty - Myghty.org
  1. edit yours views like this:
import myghty.interp as interp
from django.http import HttpResponse

interpreter = interp.Interpreter(
        data_dir = '/path/to/cache', # cache folder
        component_root = '/path/to/templates', # templates folder
    )


def my_view(request): 
     response = HttpResponse() # A file-like object 
     interpreter.execute('mytemplate.myt', out_buffer = response) 
     return response 

It will use selected template.

You may also pass variables to the templates like this:

interpreter.execute('mytemplate.myt', out_buffer = response, request_args = {'foo' : 'banana'}) 

for a template looking like this:

<%args> 
 foo 
 </%args> 
 
%m.write(foo) 

This example shows how to execute a myghty template and pass the result to django template :)

from django.shortcuts import render_to_response 
 import myghty.interp as interp 
 
interpreter = interp.Interpreter( 
         data_dir = './cache', # path to cache dir 
         component_root = '/path/to/myghty/templates', 
     ) 
 
class AFile(object): 
     """I'm a file :) from http://blog.zabiello.com""" 
     __content = '' 
     def write(self, txt): 
         self.__content += txt 
     def read(self): 
         return self.__content 
 
def myview(request): 
         file = AFile() 
         #execute a template 
         interpreter.execute('mytemplate.myt', out_buffer = file) 
         # show it via django template 
         return render_to_response('index.html', {'content': file.read()}) 
Back to Top