Random ramblings about web2py, python,
Zope and (sometimes) bit of Windows.
Home | web2py | Who am I? | Contact Me | Lenguaje: Espanol |   

Last 10
Older Posts
External Links

[Back to the Homepage]

Added Feb 02 2009 , Modified Feb 07 2009 - 09:03 PM
So, you are looking to expand your skills, or perhaps jump ship from the evil Visual Studio into the communist-laden, higher-TCO-development open source evil-er movement, but you are unsure what path to take?

Well my friend, search no more, for your prayers have been answered:

web2py will make you a better person, hmm, that's a little far-feched, maybe a happier programmer would be more appropriate.

If you still don't know what is this all about, I invite you to read a little bit of what we2py is, the main page even includes a "language comparison" and many tips on how to get you up and running in no time.

The framework uses a traditional MVC pattern, where you first define your model (DB abstraction layer, hook-ups and plug-ins), then code the controller and display your page with a view. Every page you hit with your web browser is a controller, or a function in python terms, the controller then calls a view which does the actual visualization/rendering of the page. Take a look at this controller example:

Controller: default.py, defines the "index" function, which will render the "index" view

def index(): """ Main Index Page """ age = 30 name = "John Smith" return dict(name=name, age=age)

View: index.html

{{extend 'layout.html'}} <!-- Contains your "master" layout //--> <h2>Hello {{=name}}</h2> <p>Your Age is {{=age}} years old, in about a year, you will be {{=(age + 1}} years old, isn't that scary?</p> {{if age < 21:}} <p>You are a kid!</p> {{pass}}

And yes, yet another templating system, but hear this from one that come from the ZPT realm, web2py is easy, non-obtrusive and easy to code on.

You can even "code" all your html markup in your controller, by using what is called html helpers in web2py, this means that you can embed html code in your controller by constructing your markup using python functions, consider the following example:

Controller: default.py

def index(): page_info = DIV('Hello', ' ', 'World', _id='hellomsg', _class='info') return dict(page_info=page_info)

View: index.html

{{extend 'layout.html'}} <!-- Contains your "master" layout //--> {{=page_info}}

This will in turn render <div id="hellomsg" class="info">Hello World</div>

Now I do not personally (or necessarily) accept this pattern of coding, and will suggest using it sparingly in your projects. The mixing of markup (presentation) and code (logic) should be avoided as much as possible, note the as much as possible mention, what bothers me a little bit is that web2py is relying on this pattern a little bit too much if you ask me, for example, it makes the creation of your forms and fields (that will eventually tie up to a database field) way too easy, the problem that I see with this approach is this:

Suppose you have this nice controller (remember? a .py python source code file), which contain form, paragraph, classes, ids, and a myriad of markup code in it, and the view (the ultimately rendered HTML code) containing only a {{=big_html_generated_in_python}} variable, at is all good and dandy until the business (because you work at a big business, don't you?) decides to change the entire layout, moving fields around your form, etc, who will have the task of making the changes? the software engineer?? or the html code monkey??, there is a reason for this separation.

Needless to say, I am impressed with this web app development platform, and hope that its user base keeps growing as it appears to be happening now, best to python and web2py.

c o m m e n t s    f o r
web2py - A primer basic example
Added Jul 25 2009 , Modified Jul 25 2009 - 06:58 AM By Martin
Hola julio, felicitaciones por tu blog, es excelente.

Te cuento me estoy pasando de codeigniter (PHP) para web2py... hay algo que yo hacia en codeigniter que es lo siguiente:

Esta seria mi vista:

# el header q se repite en todas las webs

............ # markup HTML con tags PHP(plantilla) que son unicos para cada web

# el pie de pagina que es comun para todas las webs

Ahora yo veo que tu extiendes de un layout.html , este layout contiene todo el disenio base? si es asi.. en que parte del markup estarias insertando ese codigo que mencionas tu.. supongo que al final , pero y el footer.. supon un disenio base donde tienes un header con un menu (igual para todas las webs), luego un espacio en el centro donde se cargaria el contenido segun que opcion del menu se elija.. (esto varia, osea seria 1 vista diferente para cada boton del menu) y luego tendrias un pie de pagina que seria igual para todas.

extends 'header.html'

#codigo python

extends 'footer.html'

Seria esta la manera correcta ? puedes explicar un poco sobre la parte de layout.. contenido estatico y todo eso ? Gracias y mis felicitaciones ;-)
Added Jun 17 2010 , Modified Jun 17 2010 - 04:48 AM By mo..@gmail.com
Just: Thanks for the fun :-)

Add a Comment | Back to the Homepage

 

TechFuel.net | Web Standards xhtml 1.1 and css 2.1 | Rel 16