Using h2o template engine instead of markup

11 views
Skip to first unread message

vidyanand

unread,
Aug 16, 2010, 4:37:09 PM8/16/10
to cobweb-dev
Hi

I was wondering if its possible to use h20 (http://www.h2o-
template.org/) template engine instead of smarty templates in cobweb .
If so how much of an effort will that be ?

I work a lot on django and this looks like a great port of the same on
PHP.

Great job !!!

Thanks

Øystein Riiser Gundersen

unread,
Aug 17, 2010, 9:15:29 AM8/17/10
to cobwe...@googlegroups.com
Den 16. aug. 2010 kl. 22.37 skrev vidyanand:

> Hi
>
> I was wondering if its possible to use h20 (http://www.h2o-
> template.org/) template engine instead of smarty templates in cobweb .
> If so how much of an effort will that be ?

Not too much effort; I whipped up a very quick application/plugin for using H2O with Cobweb here: <http://github.com/gunderwonder/cobweb-h2o> -- see the README for usage instructions.

In Cobweb, an action/controller method must simply return an instance of `HTTPResponse`, so there is nothing stopping you from using H2O directly:

function action() {
$template = new H2O('template.tpl');
$content = $template->render(array('foo' => 'bar'));
return new HTTPResponse($content);
}

The above application just makes H2O a little bit easier to use with `Controller::render()` etc.

Note: Cobweb (like Django) can load templates from multiple directories (e.g. `/templates`, `/applications/app/templates`), but the default template loader that ships with H2O cannot. This may cause problems when including templates that are not in the same base directory as the top-level template. Extending the default H2O loader to support multiple base directories shouldn't be too difficult, though -- I can look into that when I have the time.

> I work a lot on django and this looks like a great port of the same on
> PHP.
>
> Great job !!!

--
Øystein Riiser Gundersen
oyste...@gmail.com

Øystein Riiser Gundersen

unread,
Aug 21, 2010, 10:05:20 AM8/21/10
to Vidyanand Murunnikara

Den 17. aug. 2010 kl. 22.47 skrev Vidyanand Murunnikara:

> Hi Oystein
>
> Thanks for the information. This seems to work great.
>
> I had a couple of comments.
> • In the tutorial you might want to make the following change (in bold). If the change is not present and you want to use MySQL it will not work:
> • $this->hasColumn('wikiword', 'string', 128, array('unique' => true, 'notnull' => true));

Right, thanks for catching that. Fixed in r474: <http://code.google.com/p/cobweb/source/detail?r=474>

The Cobweb documentation is unfortunately pretty weak at the moment; lots of interesting features such as the session and authentication frameworks, the forms module etc. are undocumented. I plan to launch a dedicated site with API docs, and a more complete manual in the coming months.

> • In the readme the H2OController needs to extend Controller.

Of course :) Fixed in @c2576e50: <http://github.com/gunderwonder/cobweb-h2o/commit/c2576e502e1e25a98cd9908e25f5f7140eb0ddce>

> I would have preferred to use the h2o template engine globally, but it does not render the error page.

Hm, this should have been fixed back in r452 <http://code.google.com/p/cobweb/source/detail?r=452> Try running from trunk see if this fixes things. In any case, I'll tag an 0.3 release as soon as possible.

(I'm considering replacing Cobweb's template adapter API with Symfony's template component <http://components.symfony-project.org/templating/>, which seems really nice. This should help fix some problems with using/mixing templating engines other than Smarty.)


> thanks
> Vidyanand.
>
> 2010/8/17 Øystein Riiser Gundersen <oyste...@gmail.com>

> --
> You received this message because you are subscribed to the Google Groups "cobweb-dev" group.
> To post to this group, send email to cobwe...@googlegroups.com.
> To unsubscribe from this group, send email to cobweb-dev+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cobweb-dev?hl=en.

Øystein Riiser Gundersen

unread,
Aug 26, 2010, 3:11:01 PM8/26/10
to Vidyanand Murunnikara
Den 25. aug. 2010 kl. 21.38 skrev Vidyanand Murunnikara:

> Better documentation will definitely help with the project and probably a dedicated website. I remember the main reason our team had gone with the django project was its really awesome documentation part :-)

Indeed -- Django's documentation is superior.

> I was going through the code base a bit and realised that you have built in authentication application just like django. I will need to look at a lot more to understand how to use groups and permissions. But like you said it is nice to have it documented. Right now I have to check code randomly to check if a particular functionality is present or not :-)

I can see how this is not ideal :) Anyways, until I get to write real documentation for this, here's the gist of the authentification application:

- Add `session`, `authentification` to your `INSTALLED_APPLICATIONS` setting, in that order. The session app starts a new session on each request and adds a `$session` property to the request object (session key-value store) and the authentification app adds a `$user` property to the request (an instance of the `User` class).

- Build the authentification tables by running `cobweb install-authentification` from the command line from your project directory. This will also create a default superuser. (Alternatively, run `Doctrine::createTablesFromArray(array('User', 'Usergroup', 'Permission', 'UsergroupPermission', 'UserPermission'));` to build the tables.)

- Add `authentification.authentification` to your `INSTALLED_MIDDLEWARE` setting. This will hook into the request cycle and intercept any actions with the `RequiresAuthentification` annotation. To use this annotation, simply add this doc comment to the controller method you wish to "protect":

/**
* @RequiresAuthentification
*/
public function superSecretStuff() {
//...
}

There are also other authentification-related annotations such as `RequiresPermission({'user.can_do_super_secret_stuff'})`, `RequiresUsergroup({'Administrator'})` etc. (See also <http://code.google.com/p/addendum/> for annotation syntax documentation)

If a user is not logged in, the middleware short-circuits a request to `superSecretStuff()` and redirects to a login page (specified by the `LOGIN_URL` setting). Add `authentification.user.login` action to your url config for a default login implementation, or copy-paste/inherit to build your own. If a login succeeds, the user is redirected to a URL specified by the `LOGIN_REDIRECT_URL` setting.

Hope this helps :)

---
Tangentially, the combination of using middleware and annotations to hook into and override certain events such as request and response phases, exceptions etc. is powerful tool for writing reusable code for stuff like authentification, caching and more. (Although not as nice as Python's decorators/Django's middleware system, the way Cobweb works is roughly analogous to Django in this area.)

Examples: Use the the `CacheControl('no_cache', 'must_revalidate'})` annotation to add cache-busting HTTP headers to outgoing responses. Or use the `AllowedMethods({'POST'})` annotation to return '405 Method not allowed' responses when a GET request is used for something which requires POST. Et cetera...


> We might be working on a PHP project soon and I am most likely to use cobweb for that. I will let you know any feedback my team has on it.

Any feedback would be greatly appreciated! :)

>
> thanks
> Vidyanand.
>
> 2010/8/21 Øystein Riiser Gundersen <oyste...@gmail.com>

joylin

unread,
Sep 23, 2010, 5:20:59 AM9/23/10
to cobweb-dev
Hi

In the INSTALLED_APPLICATIONS` setting, I added 'session' and
'authentification' which gives me an error "Could not load application
session". Without adding session app, I get the error "Call to a
member function get() on a non-object in /home/joylin/www/cobweb/
private/cobweb/applications/authentification/middleware/
authentification.middleware.php on line 20"

I went through the code base and found there is no session app under
applications.

Thanks



On Aug 27, 12:11 am, Øystein Riiser Gundersen <oystein...@gmail.com>
wrote:
> Den 25. aug. 2010 kl. 21.38 skrev Vidyanand Murunnikara:
>
> > Better documentation will definitely help with the project and probably a dedicated website. I remember the main reason our team had gone with the django project was its really awesome documentation part :-)
>
> Indeed -- Django's documentation is superior.
>
> > I was going through the code base a bit and realised that you have built in authentication application just like django. I will need to look at a lot more to understand how to use groups and permissions. But like you said it is nice to have it documented. Right now I have to check code randomly to check if a particular functionality is present or not :-)
>
> I can see how this is not ideal :) Anyways, until I get to write real documentation for this, here's the gist of the authentification application:
>
> - Add `session`, `authentification` to your `INSTALLED_APPLICATIONS` setting, in that order. The session app starts a new session on each request and adds a `$session` property to the request object (session key-value store) and the authentification app adds a `$user` property to the request (an instance of the `User` class).
>
> - Build the authentification tables by running `cobweb install-authentification` from the command line from your project directory. This will also create a default superuser. (Alternatively, run `Doctrine::createTablesFromArray(array('User', 'Usergroup', 'Permission', 'UsergroupPermission', 'UserPermission'));` to build the tables.)
>
> - Add `authentification.authentification` to your `INSTALLED_MIDDLEWARE` setting. This will hook into the request cycle and intercept any actions with the `RequiresAuthentification` annotation. To use this annotation, simply add this doc comment to the  controller method you wish to "protect":
>
> /**
> * @RequiresAuthentification
> */
> public function superSecretStuff() {
>    //...
>
> }
>
> There are also other authentification-related annotations such as `RequiresPermission({'user.can_do_super_secret_stuff'})`, `RequiresUsergroup({'Administrator'})` etc. (See also <http://code.google.com/p/addendum/> for annotation syntax documentation)
>
> If a user is not logged in, the middleware short-circuits a request to `superSecretStuff()` and redirects to a login page (specified by the `LOGIN_URL` setting). Add `authentification.user.login` action to your url config for a default login implementation, or copy-paste/inherit to build your own. If a login succeeds, the user is redirected to a URL specified by the `LOGIN_REDIRECT_URL` setting.
>
> Hope this helps :)
>
> ---
> Tangentially, the combination of using middleware and annotations to hook into and override certain events such as request and response phases, exceptions etc. is powerful tool for writing reusable code for stuff like authentification, caching and more. (Although not as nice as Python's decorators/Django's middleware system, the way Cobweb works is roughly analogous to Django in this area.)
>
> Examples: Use the the `CacheControl('no_cache', 'must_revalidate'})` annotation to add cache-busting HTTP headers to outgoing responses. Or use the `AllowedMethods({'POST'})` annotation to return '405 Method not allowed' responses when a GET request is used for something which requires POST. Et cetera...
>
> > We might be working on a PHP project soon and I am most likely to use cobweb for that.  I will let you know any feedback my team has on it.
>
> Any feedback would be greatly appreciated! :)
>
>
>
>
>
> > thanks
> > Vidyanand.
>
> > 2010/8/21 Øystein Riiser Gundersen <oystein...@gmail.com>
>
> > Den 17. aug. 2010 kl. 22.47 skrev Vidyanand Murunnikara:
>
> >> Hi Oystein
>
> >> Thanks for the information. This seems to work great.
>
> >> I had a couple of comments.
> >>      • In the tutorial  you might want to make the following change (in bold). If the change is not present and you want to use MySQL it will not work:
> >>              • $this->hasColumn('wikiword', 'string', 128, array('unique' => true, 'notnull' => true));
>
> > Right, thanks for catching that. Fixed in r474: <http://code.google.com/p/cobweb/source/detail?r=474>
>
> > The Cobweb documentation is unfortunately pretty weak at the moment; lots of interesting features such as the session and authentication frameworks, the forms module etc. are undocumented. I plan to launch a dedicated site with API docs, and a more complete manual in the coming months.
>
> >>      • In the readme the H2OController needs to extend Controller.
>
> > Of course :) Fixed in @c2576e50: <http://github.com/gunderwonder/cobweb-h2o/commit/c2576e502e1e25a98cd9...>
>
> >> I would have preferred to use the h2o template engine globally, but it does not render the error page.
>
> > Hm, this should have been fixed back in r452 <http://code.google.com/p/cobweb/source/detail?r=452> Try running from trunk see if this fixes things. In any case, I'll tag an 0.3 release as soon as possible.
>
> > (I'm considering replacing Cobweb's template adapter API with Symfony's template component <http://components.symfony-project.org/templating/>, which seems really nice. This should help fix some problems with using/mixing templating engines other than Smarty.)
>
> >> thanks
> >> Vidyanand.
>
> >> 2010/8/17 Øystein Riiser Gundersen <oystein...@gmail.com>
> >> Den 16. aug. 2010 kl. 22.37 skrev vidyanand:
>
> >>> Hi
>
> >>> I was wondering if its possible to use h20 (http://www.h2o-
> >>> template.org/) template engine instead of smarty templates in cobweb .
> >>> If so how much of an effort will that be ?
>
> >> Not too much effort; I whipped up a very quick application/plugin for using H2O with Cobweb here: <http://github.com/gunderwonder/cobweb-h2o> -- see the README for usage instructions.
>
> >> In Cobweb, an action/controller method must simply return an instance of `HTTPResponse`, so there is nothing stopping you from using H2O directly:
>
> >> function action() {
> >>    $template = new H2O('template.tpl');
> >>    $content = $template->render(array('foo' => 'bar'));
> >>    return new HTTPResponse($content);
> >> }
>
> >> The above application just makes H2O a little bit easier to use with `Controller::render()` etc.
>
> >> Note: Cobweb (like Django) can load templates from multiple directories (e.g. `/templates`, `/applications/app/templates`), but the default template loader that ships with H2O cannot. This may cause problems when including templates that are not in the same base directory as the top-level template. Extending the default H2O loader to support multiple base directories shouldn't be too difficult, though -- I can look into that when I have the time.
>
> >>> I work a lot on django and this looks like a great port of the same on
> >>> PHP.
>
> >>> Great job !!!
>
> >> --
> >> Øystein Riiser Gundersen
> >> oystein...@gmail.com
>
> >> --
> >> You received this message because you are subscribed to the Google Groups "cobweb-dev" group.
> >> To post to this group, send email to cobwe...@googlegroups.com.
> >> To unsubscribe from this group, send email to cobweb-dev+...@googlegroups.com.
> >> For more options, visit this group athttp://groups.google.com/group/cobweb-dev?hl=en.
>
> > --
> > Øystein Riiser Gundersen
> > oystein...@gmail.com
>
> --
> Øystein Riiser Gundersen
> oystein...@gmail.com

Øystein Riiser Gundersen

unread,
Sep 23, 2010, 5:41:58 AM9/23/10
to cobweb-dev


On Sep 23, 11:20 am, joylin <joylinvin...@gmail.com> wrote:
> Hi
>
> In the INSTALLED_APPLICATIONS` setting, I added 'session' and
> 'authentification' which gives me an error "Could not load application
> session". Without adding session app, I get the error "Call to a
> member function get() on a non-object in /home/joylin/www/cobweb/
> private/cobweb/applications/authentification/middleware/
> authentification.middleware.php on line 20"
>
> I went through the code base and found there is no session app under
> applications.

Apologies, my info was all wrong -- I wrote that down too quickly :)

The session middleware resides in the `cobweb` application, which is
always loaded, regardless of the `INSTALLED_APPLICATIONS` setting.

Hence, only the middleware needs to be loaded by adding
`cobweb.session` to your `INSTALLED_MIDDLEWARE` before the
authentification middleware:

'INSTALLED_MIDDLEWARE' => array(
'cobweb.session', // session support
'authentification.authentification', // auth support
/* ... */
);


`cobweb.session` add a $session property to the request object (e.g.
$request->session['foo'] = 'bar') and
`authentification.authentification` adds a $user object (instance of
User, e.g. $request->user->fullname() etc.). The auth middleware also
handles actions annotated with `@RequiresLogin`, etc.

Hope this helps :)

joylin

unread,
Sep 23, 2010, 8:21:32 AM9/23/10
to cobweb-dev
Thanks. It worked :)

On Sep 23, 2:41 pm, Øystein Riiser Gundersen <oystein...@gmail.com>

joylin

unread,
Sep 29, 2010, 9:04:11 AM9/29/10
to cobweb-dev
Hi,

I tried out the authentification related annotations such as
RequiresPermission and RequiresAuthentification. These works great. Is
there any annotation related to user groups?
RequiresUsergroup({'Admin'}) doesnt seem to work.

Thanks


On Aug 27, 12:11 am, Øystein Riiser Gundersen <oystein...@gmail.com>
wrote:
> Den 25. aug. 2010 kl. 21.38 skrev Vidyanand Murunnikara:
>
> > Better documentation will definitely help with the project and probably a dedicated website. I remember the main reason our team had gone with the django project was its really awesome documentation part :-)
>
> Indeed -- Django's documentation is superior.
>
> > I was going through the code base a bit and realised that you have built in authentication application just like django. I will need to look at a lot more to understand how to use groups and permissions. But like you said it is nice to have it documented. Right now I have to check code randomly to check if a particular functionality is present or not :-)
>
> I can see how this is not ideal :) Anyways, until I get to write real documentation for this, here's the gist of the authentification application:
>
> - Add `session`, `authentification` to your `INSTALLED_APPLICATIONS` setting, in that order. The session app starts a new session on each request and adds a `$session` property to the request object (session key-value store) and the authentification app adds a `$user` property to the request (an instance of the `User` class).
>
> - Build the authentification tables by running `cobweb install-authentification` from the command line from your project directory. This will also create a default superuser. (Alternatively, run `Doctrine::createTablesFromArray(array('User', 'Usergroup', 'Permission', 'UsergroupPermission', 'UserPermission'));` to build the tables.)
>
> - Add `authentification.authentification` to your `INSTALLED_MIDDLEWARE` setting. This will hook into the request cycle and intercept any actions with the `RequiresAuthentification` annotation. To use this annotation, simply add this doc comment to the  controller method you wish to "protect":
>
> /**
> * @RequiresAuthentification
> */
> public function superSecretStuff() {
>    //...
>
> }
>
> There are also other authentification-related annotations such as `RequiresPermission({'user.can_do_super_secret_stuff'})`, `RequiresUsergroup({'Administrator'})` etc. (See also <http://code.google.com/p/addendum/> for annotation syntax documentation)
>
> If a user is not logged in, the middleware short-circuits a request to `superSecretStuff()` and redirects to a login page (specified by the `LOGIN_URL` setting). Add `authentification.user.login` action to your url config for a default login implementation, or copy-paste/inherit to build your own. If a login succeeds, the user is redirected to a URL specified by the `LOGIN_REDIRECT_URL` setting.
>
> Hope this helps :)
>
> ---
> Tangentially, the combination of using middleware and annotations to hook into and override certain events such as request and response phases, exceptions etc. is powerful tool for writing reusable code for stuff like authentification, caching and more. (Although not as nice as Python's decorators/Django's middleware system, the way Cobweb works is roughly analogous to Django in this area.)
>
> Examples: Use the the `CacheControl('no_cache', 'must_revalidate'})` annotation to add cache-busting HTTP headers to outgoing responses. Or use the `AllowedMethods({'POST'})` annotation to return '405 Method not allowed' responses when a GET request is used for something which requires POST. Et cetera...
>
> > We might be working on a PHP project soon and I am most likely to use cobweb for that.  I will let you know any feedback my team has on it.
>
> Any feedback would be greatly appreciated! :)
>
>
>
>
>
> > thanks
> > Vidyanand.
>
> > 2010/8/21 Øystein Riiser Gundersen <oystein...@gmail.com>
>
> > Den 17. aug. 2010 kl. 22.47 skrev Vidyanand Murunnikara:
>
> >> Hi Oystein
>
> >> Thanks for the information. This seems to work great.
>
> >> I had a couple of comments.
> >>      • In the tutorial  you might want to make the following change (in bold). If the change is not present and you want to use MySQL it will not work:
> >>              • $this->hasColumn('wikiword', 'string', 128, array('unique' => true, 'notnull' => true));
>
> > Right, thanks for catching that. Fixed in r474: <http://code.google.com/p/cobweb/source/detail?r=474>
>
> > The Cobweb documentation is unfortunately pretty weak at the moment; lots of interesting features such as the session and authentication frameworks, the forms module etc. are undocumented. I plan to launch a dedicated site with API docs, and a more complete manual in the coming months.
>
> >>      • In the readme the H2OController needs to extend Controller.
>
> > Of course :) Fixed in @c2576e50: <http://github.com/gunderwonder/cobweb-h2o/commit/c2576e502e1e25a98cd9...>
>
> >> I would have preferred to use the h2o template engine globally, but it does not render the error page.
>
> > Hm, this should have been fixed back in r452 <http://code.google.com/p/cobweb/source/detail?r=452> Try running from trunk see if this fixes things. In any case, I'll tag an 0.3 release as soon as possible.
>
> > (I'm considering replacing Cobweb's template adapter API with Symfony's template component <http://components.symfony-project.org/templating/>, which seems really nice. This should help fix some problems with using/mixing templating engines other than Smarty.)
>
> >> thanks
> >> Vidyanand.
>
> >> 2010/8/17 Øystein Riiser Gundersen <oystein...@gmail.com>
> >> Den 16. aug. 2010 kl. 22.37 skrev vidyanand:
>
> >>> Hi
>
> >>> I was wondering if its possible to use h20 (http://www.h2o-
> >>> template.org/) template engine instead of smarty templates in cobweb .
> >>> If so how much of an effort will that be ?
>
> >> Not too much effort; I whipped up a very quick application/plugin for using H2O with Cobweb here: <http://github.com/gunderwonder/cobweb-h2o> -- see the README for usage instructions.
>
> >> In Cobweb, an action/controller method must simply return an instance of `HTTPResponse`, so there is nothing stopping you from using H2O directly:
>
> >> function action() {
> >>    $template = new H2O('template.tpl');
> >>    $content = $template->render(array('foo' => 'bar'));
> >>    return new HTTPResponse($content);
> >> }
>
> >> The above application just makes H2O a little bit easier to use with `Controller::render()` etc.
>
> >> Note: Cobweb (like Django) can load templates from multiple directories (e.g. `/templates`, `/applications/app/templates`), but the default template loader that ships with H2O cannot. This may cause problems when including templates that are not in the same base directory as the top-level template. Extending the default H2O loader to support multiple base directories shouldn't be too difficult, though -- I can look into that when I have the time.
>
> >>> I work a lot on django and this looks like a great port of the same on
> >>> PHP.
>
> >>> Great job !!!
>
> >> --
> >> Øystein Riiser Gundersen
> >> oystein...@gmail.com
>
> >> --
> >> You received this message because you are subscribed to the Google Groups "cobweb-dev" group.
> >> To post to this group, send email to cobwe...@googlegroups.com.
> >> To unsubscribe from this group, send email to cobweb-dev+...@googlegroups.com.
> >> For more options, visit this group athttp://groups.google.com/group/cobweb-dev?hl=en.
>
> > --
> > Øystein Riiser Gundersen

Øystein Riiser Gundersen

unread,
Oct 1, 2010, 5:34:54 AM10/1/10
to cobwe...@googlegroups.com

Den 29. sep. 2010 kl. 15.04 skrev joylin:

> Hi,
>
> I tried out the authentification related annotations such as
> RequiresPermission and RequiresAuthentification. These works great. Is
> there any annotation related to user groups?
> RequiresUsergroup({'Admin'}) doesnt seem to work.

Ah, I never got around to supporting that, but it isn't too difficult to add. I will commit the needed changes to SVN soon as I have the time (by the next couple of days) and let you know on this list.

Reply all
Reply to author
Forward
0 new messages