Catalyst and MongoDB

Catalyst::Model::MongoDB

Makes a MongoDB Connection available as a Catalyst Model, including several shortcuts for default collections and the like. Getty's Catalyst::Model::MongoDB is on CPAN and very useful

Accessing MongoDB::ObjectID from within TT templates

TT2 disallows access to hash elements with a key that starts with an underscore by default and for good reasons. The MongoDB::ObjectID embedded in Mongo docs do start with an underscore however, to the _id attribute is inaccessible.

To get around this, one must write a method in the TT View and expose this to the template:

package MyApp::View::HTML;

# ...

sub mongo_oid {
    my ($self$c$mongo_doc) = @_;
    return unless ref $mongo_doc eq 'HASH';
    if ( $mongo_doc->{_id} && $mongo_doc->{value} ) {
        return $mongo_doc->{_id}->{value};
    }
    return;
}

__PACKAGE__->config(
    expose_methods => [qw/mongo_oid/],
);

Now, in the template, the object ID of a mongo doc mongodoc is accessible as [% mongo_oid( mongodoc ) %].

My tags:
 
Popular tags:
  toolkit tt2 mongodb template
Powered by Catalyst
Powered by MojoMojo Hosted by Shadowcat - Managed by Nordaaker