Web2py - Components


Advertisements


A component is defined as the functional part of a web page, which works autonomously. It can be composed of modules, controllers and views, which are embedded in a web page. The component in an application, must be localized tag and the performance is considered to be independent of module.

In web2py, the main focus is on using components that are loaded in page and which communicate with the component controller via AJAX.

web2py includes a function, which is called the LOAD function, which makes implementation of components easy without explicit JavaScript or AJAX programming.

Consider a simple web application namely “test” that extends the web2py application with custom model in file “models/db_comments.py”.

db.define_table(
   'comment_post', Field('body','text',
   label = 'Your comment'),auth.signature
)

The above code will create a table “comment_post” with the proper table definition. The action will be implemented with the help of functions in “controllers/comments.py”.

def post():
   return dict(
      form = SQLFORM(db.comment_post).process(),
      comments = db(db.comment_post).select()
   )

The corresponding view will be displayed as −

{{extend 'layout.html'}}
{{for post in comments:}}

<div class = "post">
   On {{= post.created_on}} {{= post.created_by.first_name}}
   says <span class = "post_body">{{= post.body}}</span>
</div>

{{pass}}
{{= form}}

The page can be accessed using the given URL − http://127.0.0.1:8000/test/comments/post

The method mentioned above is a traditional way of accessing a view, which can be changed with the implementation of the LOAD function.

This can be achieved by creating a new view with the extension ".load" that does not extend the layout.

The new view created would be "views/comments/post.load"

<div class = "post">
   On {{= post.created_on}} {{= post.created_by.first_name}}
   says <blockquote class = "post_body">{{= post.body}}</blockquote>
</div>

{{pass}}
{{= form}}

The URL to access the page would be − http://127.0.0.1:8000/test/comments/post.load

The LOAD component can be embedded into any other page of web2py application. This can be done by using the following statement.

{{= LOAD('comments','post.load',ajax = True)}}

For example, the Controllers can be edited as −

def index():
   return dict()

In View, we can add the component −

{{extend 'layout.html'}}
{{= LOAD('comments','post.load',ajax = True)}}

The page can be accessed with the URL − http://127.0.0.1:8000/test/default/index

Component Plugins

Component plugins are the plugins, which uniquely define Components. Components access the database directly with their model definition.

As mentioned in the previous example, comments component into a comments_plugin can be done in the Models section −

"models/plugin_comments.py" −

db.define_table(
   'plugin_comments_comment',
   Field('body','text', label = 'Your comment'),
   auth.signature
)

The Controller will include the following plugin −

def plugin_comments():
   return LOAD('plugin_comments','post',ajax = True)


Advertisements
E-Books Store