mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.48k stars 634 forks source link

Pug template conversion #1442

Open johntom opened 1 year ago

johntom commented 1 year ago

I'm trying to convert the following respo (https://github.com/rajasegar/htmx-tabular) from pug templates to nunjucks. I've successfull used numjucks with fastify but have problem converting the following refresh of template.

  app.post('/search', (req,res) => {
    const { sortBy, direction, start } = req.query;
    const { query } = req.body;
    console.log(query);
    const data = query ? restaurants.filter(r => r.name.includes(query)) : restaurants;

    const reverse = direction === 'asc' ? 'desc' : 'asc';
    const sortedData = direction === 'asc'
      ? R.sortWith([R.ascend(R.prop(sortBy))], data)
      : R.sortWith([R.descend(R.prop(sortBy))], data);

    const totalPages = sortedData.length / 10;
    const _restaurants = sortedData.slice(start, Number(start) + 10);
   // tring to convert to nunjucks
    const template = pug.compileFile('views/_table.pug');
    const markup = template({ restaurants: _restaurants, sortBy, reverse, direction, start, totalPages, totalResults: sortedData.length });
    res.send(markup);
  });
webketje commented 8 months ago

Please describe the problem in more detail. Does it result in errors? What is missing after the rendering? What have you tried? Are you not able to call methods, etc...

snth commented 4 months ago

Hi,

I came here with a similar question (and as it turns out in order to convert the same htmx-tabular template 😂).

It looks like pypugjs can output Jinja2 templates so hopefully that would be compatible with nunjucks.

My plan is to try that when I've got a spare moment.