Arquivo

Textos com Etiquetas ‘legal’

Template Engine em Python – Cheetah

10, setembro, 2009 eliezer Sem comentários

Um modulo simples para python, cheetah. Ele tem tudo que programadores python gosta, simples, bem documentado, comunidade ativa, rápido, possui um mecanismo de cache entre outras funcionalidades.

  • Onde baixar?
  • http://www.cheetahtemplate.org

  • Instalando
  • 1
    
    python setup.py install
  • Criando um objeto simples Cliente
  • 1
    2
    3
    4
    
    class Cliente():
    	def __init__(self, nome, email):
    		self.nome = nome
    		self.email = email
  • Criando um template
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    <html>
    <head><title>$title</title></head>
    <body>
    	<div>
    	#for $cliente in $clientes        
    		<div>
    		<b>$cliente.nome</b>
    		(<a href="mailto:$cliente.email">$cliente.email</a>)
    		</div>
    	#end for
    	</div>
    </body>
    </html>
  • Executando
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    from Cheetah.Template import Template
     
    if __name__ == "__main__":
     
    	clientes = [Cliente("Eliezer Rodrigues", "eliezer@teste.net"), 
                           Cliente("Maria", "maria@teste.net")]
     
    	template = Template(file="page.tpl")
    	template.title = "Todos os clientes"
    	template.clientes = clientes
     
    	print str(template)
  • Resultado:
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    <html>
    <head><title>Todos os clientes</title></head>
    <body>
            <div>
                    <div>
                    <b>Eliezer Rodrigues</b>
                    (<a href="mailto:eliezer@teste.net">eliezer@teste.net</a>)
                    </div>
                    <div>
                    <b>Maria</b>
                    (<a href="mailto:maria@teste.net">maria@teste.net</a>)
                    </div>
            </div>
    </body>

    Curtiu?? para saber mais sobre o projeto entre no guia do usuário e confira tudo sobre o cheetah.