Dust.js - partial extend base

227 views Asked by At

I use NodeJS as engine to dust. I have problem with partials. I think that it is possible, what I want. Here is my index.dust :

<!DOCTYPE html>
<html lang="pl">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Project - {title}</title>
</head>

<body>
<div id="content">
    {>"common/navbar-logout"/}
    {+content /}
</div>
    {+scripts /}
</body>
</html>

I would like to "import" common/navbar-logout file. In "common/navbar-logout" file, I have:

{>"index" /}
{! some HTML!}
{<scripts}
<script>
    console.log("yyyy");
</script>
{/scripts}

When I run this script, I've got:

RangeError: Maximum call stack size exceeded at Object.dust.filters.h (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:296:16) at Object.dust.filter (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:290:34) at Chunk.reference (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:764:30) at body_0 (evalmachine.:1:371) at load (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:151:14) at Chunk.partial (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:911:14) at body_0 (evalmachine.:1:157) at load (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:151:14) at Chunk.partial (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:911:14) at body_0 (evalmachine.:1:661) at load (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:151:14) at Chunk.partial (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:911:14) at body_0 (evalmachine.:1:157) at load (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:151:14) at Chunk.partial (/APP_PATH/node_modules/dustjs-linkedin/lib/dust.js:911:14) at body_0 (evalmachine.:1:661)

When I remove {>"index" /} in "common/navbar-logout", application run, but I don't have script block from "common/navbar-logout" in "index" .

My goal is inject script-block from "common/navbar-logout" into "index" using partial "{>"common/navbar-logout"/}"

1

There are 1 answers

1
Interrobang On

Dust partials can only provide blocks to templates that they include into themselves. Instead of index requiring common/navbar-logout, you would have to have common/navbar-logout include index as a partial. It could then supply its inline partials to the blocks in index. This doesn't lend itself well to your use case.

To accomplish what you actually want to do, I've used some custom helpers in the past. Something like this:

{! some HTML!}
{@script}
    console.log("yyyy");
{/script}

dust.helpers.script = function(chunk, context, bodies, params) {
  if (params.src) {
    addScript(params.src); // adds to some global context var
  } else {
    chunk = chunk.write('<script>').render(bodies.block, context).write('</script>');
  }
  return chunk;
};