helpers/handlebars-helper-compose

Limit in {{#compose}}

diogomoretti opened this issue · 9 comments

Hello!

How to make a limit in {{#compose}}?

{{#compose src="posts/*.md"}}
  <h1>Title: {{@title}}</h1>
  {{{@content}}}</p>
{{/compose}}

For example: i need the last 3 blog posts.

I just added this option, does this work for you? https://github.com/helpers/handlebars-helper-compose#filtering

Also, here is the filter function as a helper in case you need it for something else (but it won't work for this use case):

// node_modules
var _ = require('lodash');

module.exports.register = function (Handlebars, options, params) {
  'use strict';
  var opts = options || {};

  Handlebars.registerHelper('last',function(context, num) {
    var result = context.slice(Math.max(context.length - num, 0))
    return new Handlebars.SafeString(result);
  });
};

It's used like:

{{last foo 3}}
// => returns the last three items in the array

Btw, you could probably just use the compare option, but that would filter out files after they have been processed. I think this should be faster since it will reduce the number of files that get processed.

@diogomoretti I'm going to go ahead and close. if the solutions I posted don't work for you feel free to re-open

Ow. Its work. Sorry for the no response.

On Monday, January 20, 2014, Jon Schlinkert notifications@github.com
wrote:

Closed #10#10
.


Reply to this email directly or view it on GitHubhttps://github.com//issues/10
.

👍 no prob!

I still need to use the "limit" in compose. which syntax I use?

You'll have to try out the solutions I posed and see which one works for you.

How to use filter?

On Saturday, February 15, 2014, Jon Schlinkert notifications@github.com
wrote:

You'll have to try out the solutions I posed and see which one works for
you.

Reply to this email directly or view it on GitHubhttps://github.com//issues/10#issuecomment-35170028
.

my compose:

{{#compose src="posts/**/*.md"}}
  <h1><a href="{{formatDate date "%Y"}}/{{@basename}}">{{@title}}</a></h1>
  <p>{{{@content}}}</p>
{{/compose}}

Gruntfile.js

pages: {
    options: {
        flatten: true,
        layout: '<%= config.src %>/templates/layouts/default.hbs',
        compose: {
            cwd: 'src/content/blog',
            sortBy: 'date',
            sortOrder: 'desc',
            filter: function(arr) {
                return arr.slice(Math.max(arr.length - 2, 0));
            }
        },
        permalinks: {
            preset: 'pretty'
        }
    },
    files: {
        '<%= config.dist %>/': ['<%= config.src %>/templates/pages/*.hbs']
    }
},

But list all 4 blog posts. After resolve this, i need apply the pagination.