text: var eden = require('edencms');
  var async = require('async');
  
  exports = module.exports = function (req, res) {
  
  	var view = new eden.View(req, res);
  	var locals = res.locals;
  
  	// Init locals
  	locals.pageTitle = "Blog";
  	locals.section = 'blog';
  	locals.filters = {
  		category: req.params.category,
  	};
  	locals.data = {
  		posts: [],
  		categories: [],
  	};
  
  	// Load all categories
  	view.on('init', function (next) {
  
  		eden.list('PostCategory').model.find().sort('name').exec(function (err, results) {
  
  			if (err || !results.length) {
  				return next(err);
  			}
  
  			locals.data.categories = results;
  
  			// Load the counts for each category
  			async.each(locals.data.categories, function (category, next) {
  
  				eden.list('Post').model.count().where('categories').in([category.id]).exec(function (err, count) {
  					category.postCount = count;
  					next(err);
  				});
  
  			}, function (err) {
  				next(err);
  			});
  		});
  	});
  
  	// Load the current category filter
  	view.on('init', function (next) {
  
  		if (req.params.category) {
  			eden.list('PostCategory').model.findOne({ key: locals.filters.category }).exec(function (err, result) {
  				locals.data.category = result;
  				next(err);
  			});
  		} else {
  			next();
  		}
  	});
  
  	// Load the posts
  	view.on('init', function (next) {
  
  		var q = eden.list('Post').paginate({
  			page: req.query.page || 1,
  			perPage: 10,
  			maxPages: 10,
  			filters: {
  				state: 'published',
  			},
  		})
  			.sort('-publishedDate')
  			.populate('author categories');
  
  		if (locals.data.category) {
  			q.where('categories').in([locals.data.category]);
  		}
  
  		q.exec(function (err, results) {
  			locals.data.posts = results;
  			console.log(results);
  			next(err);
  		});
  	});
  
  	// Render the view
  	view.render('blog');
  };
  