Component plugins
Component plugins are plugins that define components. Components usually access the database and define with their own models.
Here we turn the previous comments
component into a comments_plugin
by using the same code we wrote before, but following all of the previous rules.
First, we create a model called “models/plugin_comments.py”:
db.define_table('plugin_comments_comment',
Field('body', 'text', label='Your comment'),
auth.signature)
def plugin_comments():
return LOAD('plugin_comments', 'post', ajax=True)
(notice the last two lines define a function that will simplify the embedding of the plugin)
Second, we define a “controllers/plugin_comments.py”
def post():
if not auth.user:
return A('login to comment', _href=URL('default', 'user/login'))
comment = db.plugin_comments_comment
return dict(form=SQLFORM(comment).process(),
comments=db(comment).select())
Third, we create a view called “views/plugin_comments/post.load”:
{{for comment in comments:}}
<div class="comment">
on {{=comment.created_on}} {{=comment.created_by.first_name}}
says <span class="comment_body">{{=comment.body}}</span>
</div>
{{pass}}
{{=form}}
Now we can use admin to pack the plugin for distribution. Admin will save this plugin as:
web2py.plugin.comments.w2p
We can use the plugin in any view by simply installing the plugin via the edit page in admin and adding this to our own views
{{=plugin_comments()}}
Of course we can make the plugin more sophisticated by having components that take parameters and configuration options. The more complex the components, the more difficult it becomes to avoid name collisions. The Plugin Manager described below is designed to avoid this problem.