deligenius / view-engine

🚀A Template View Engine for Deno frameworks
MIT License
54 stars 15 forks source link

Handlebars configuration properties #7

Closed velascopja closed 4 years ago

velascopja commented 4 years ago

Sorry if this a stupid question. How can I set Handlebar's properties baseDir, extname, layoutsDir, partialsDir, defaultLayout, helpers and compilerOptions.

Thank you very much!

velascopja commented 4 years ago

I'm trying to render the example in handlebars module, a page (index.html) which is in the folder 'views' and should be the body part of the rendered page. The main page (main.hbs) is in the folder 'layouts' (inside the folder 'views'). There is a 'partials' folder (also inside the folder 'views') with two partials ('head.hbs' and 'foot.hbs') used in 'main.hbs' ( {{> head}} and {{> foot}} ), and a subfolder ('page') with the file 'title.hbs' inside, which is referenced in 'index.hbs' ( {{> page/title }} ). But, when I try to render 'index.hbs' I get the error 'The partial page/title could not be found'. If I remove the reference to the partial 'title.hbs', the page renders but only 'index.hbs' ('main.hbs' inside 'layouts' is ignored).

How can I get the 'index.hbs' rendered as the body of the 'main.hbs' page? Is there a way to set the layouts folder, the main layout, the partials folder, etc.?

Thank you in advance.

gjuoun commented 4 years ago

Can you give me code example so that I can reproduce it?

velascopja commented 4 years ago

Hi, find the test project attached to this reply. If you need anything else, please don't hesitate to ask for it.

Best regards. view_engine test.zip

gjuoun commented 4 years ago

Handlebar's properties like baseDir, extname, layoutsDir, partialsDir, defaultLayout, helpers and compilerOptions are coming from a third party library, which needs --unstable flag to run.

But it's an easy fix, I will be back.

velascopja commented 4 years ago

Great!!! Thx!!!

gjuoun commented 4 years ago

After some testing, I decided not to adapt alosaur/handlebars until --unstable flag is no longer needed, since this library works very differently from the Javascript version. I will keep eye on it, and let you know in the future.

You can still use it in this way with Oak:

import { Application } from "https://deno.land/x/oak/mod.ts";
import { Handlebars } from 'https://deno.land/x/handlebars/mod.ts';

const app = new Application();

const DEFAULT_HANDLEBARS_CONFIG = {
    baseDir: 'views',
    extname: '.hbs',
    layoutsDir: 'layouts/',
    partialsDir: 'partials/',
    defaultLayout: 'main',
    helpers: undefined,
    compilerOptions: undefined,
};

const handle = new Handlebars(DEFAULT_HANDLEBARS_CONFIG);

app.use(async (ctx, next) => {
    ctx.response.body = await handle.renderView("index", { name: 'Alosaur' })
    ctx.response.headers.set("Content-Type", "text/html")
});

await app.listen({ port: 8000 });

And you need --unstable flag to run it

velascopja commented 4 years ago

So, there's currently no way in View Engine to render the 'index.hbs' in my code as the body of the 'main.hbs', with partials and all that stuff?

gjuoun commented 4 years ago

Sorry, not for view-engine at the moment.

You can use the library directly by the code above. It already has everything set up for you.

velascopja commented 4 years ago

OK John, I'll use alosaur/handlebars then. Thank you for your kind support!!!