Git Product home page Git Product logo

Comments (12)

mohd-akram avatar mohd-akram commented on May 26, 2024 2

You can do layouts like this:

home.hbs:

{{#> layout title="Home page"}}

{{#*inline "body"}}
Hello world
{{/inline}}

{{/layout}}

layout.hbs:

<!doctype html>
<html lang="en">
  <head><title>{{title}}</title></head>
  <body>{{#> body}}{{/body}}</body>
</html>

app.js:

module.exports = async function (fastify, opts) {
  fastify.register(require('point-of-view'), {
    engine: { handlebars: require('handlebars') },
    options: { partials: { layout: 'layout.hbs' } }
  });
  fastify.get('/', async (request, reply) => {
    reply.view('home.hbs');
  });
};

from point-of-view.

Uzlopak avatar Uzlopak commented on May 26, 2024 1

@chinhtrandn
according to tests, yes

test('reply.view with handlebars engine with partials', t => {

from point-of-view.

mcollina avatar mcollina commented on May 26, 2024

Can you make an example we can work with? Maybe point-of-view is missing some features!

from point-of-view.

Aragur avatar Aragur commented on May 26, 2024

Done, here is a small example: https://github.com/AragurDEV/fastify-point-of-view-example

from point-of-view.

mcollina avatar mcollina commented on May 26, 2024

Unfortunately I don't know how to help, as I'm not super familiar with handlebars. A PR would be welcome.

from point-of-view.

nikea1 avatar nikea1 commented on May 26, 2024

I've been trying to figure out this problem too. I think it has to deal will how the view engine is set up right now. I compared the engine express-handlebars uses and the one point of view uses. In summary, the express-handlebars render twice. One for the main then one for the layout to put in the main body. Meanwhile, point-of-view is set to only render one template it seems. So if you want something like express-handlebars your best bet right now is writing a plugin that handles that double compile-rendering process. Unfortunately, I have no idea where to begin. I don't know enough handlebars to even begin to make such a plugin.

from point-of-view.

francisbrito avatar francisbrito commented on May 26, 2024

Since layouts are a handlebar-express-package-specific feature, I think this could only be achieved by doing the two-step-render process @nikea1 described at the handler-level, e.g:

fastify.get('/', async (request, reply) => {
  const html = await fastify
    .view('home.hbs', {})
    .then(body => fastify.view('layout.hbs', { body, title: 'It works!' }))

  reply.type('text/html').send(html)
})

given a templates/layout.hbs like this:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{{title}}</title>
</head>
<body>
<section class="main">
{{{body}}}
</section>
</body>
</html>

and a templates/home.hbs like this:

<h1>This is home</h1>

--
I could open a PR to "bake" this functionality into point-of-view plugin, if it makes sense @mcollina

from point-of-view.

mcollina avatar mcollina commented on May 26, 2024

I think that make sense, send a PR over. However we need to make sure that either:

  1. we throw nicely for other engines
  2. we implement some similar feature for them

from point-of-view.

mohd-akram avatar mohd-akram commented on May 26, 2024

Handlebars supports layouts via partial blocks and inline partials.

from point-of-view.

Eomm avatar Eomm commented on May 26, 2024

Handlebars supports layouts via partial blocks and inline partials.

Could you elaborate?
I don't understand the point

from point-of-view.

chinhtrandn avatar chinhtrandn commented on May 26, 2024

Can i set Partials folder?

from point-of-view.

Eomm avatar Eomm commented on May 26, 2024

#136 and #141 implemented this issue

closing

from point-of-view.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.