question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Would it be possible to pass a sub-tree as parameter to a partial?

See original GitHub issue

Apologies if this has been previously discussed or is simply unpractical to implement, but I wish there as a way to pass a sub-tree of DOM elements to a partial as a parameter.

Example:

<!-- Calling the partial -->
{>"components/my-component1" parameter1="foobar"}
    <div class="component__inner">
        <p>Hello world</p>
    </div>
{<}
<!-- Inside the partial -->
<div class="component component--{parameter1}">
    {_content}
</div>

Result:

<div class="component component--foobar">
    <div class="component__inner">
        <p>Hello world</p>
    </div>
</div>

This would allow developers to create self-contained and reusable components as Dust.js partials. You can argue that this is technically possible now, but one would have to pass any markup required by the component as a normal parameter, which can get messy very easily:

{>"components/my-component1" parameter1="foobar" content="<div class='component__inner'><p>Hello world</p></div>"}

A similar approached is used by Sass in their mixins, as developers can define normal parameters as well a special code block defined as @content.

@mixin component1($arg1, $arg) {
    .component1 {
        width: $arg1;
        height: $arg2;
        @content;
    }
}

React components also make this possible through this.props.children.

I’d love to hear your thoughts about this. Thanks in advance!

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
eduardoboucascommented, Apr 15, 2016

For anyone interested, this is possible by creating a very simple helper used to call a partial and inject its content block as a parameter:

(function (dust) {
  /**
   * @partial helper
   * Calls a partial and injects the body block as a `$content` parameter
   *
   * Example:
   *    {@partial $name="partials/form"}
   *      <input name="foo" type="text">
   *    {/partial}
   *
   * By @eduardoboucas
   * https://eduardoboucas.com/blog/2016/04/15/creating-modular-ui-components-with-dustjs.html
   */  
  dust.helpers.partial = function (chunk, context, bodies, params) {
    var newContext = {
      $content: bodies.block
    };

    return chunk.partial(params.$name, context.push(newContext), params);
  };
})(typeof exports !== 'undefined' ? module.exports = require('dustjs-linkedin') : dust);

A more detailed explanation can be found here.

0reactions
eduardoboucascommented, Apr 15, 2016

That’d be great. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net mvc3: How to pass parameters from controller to a ...
1 Answer 1 ... I think you could use @Html.RenderAction instead of RenderPage. Then you can design an action, pass parameters and choose...
Read more >
passing parameters to a partial - Using Umbraco And Getting ...
I want to be able to use the same partial multiple times in a template, but pass it the value of different aliases...
Read more >
8 Scapegoat Trees - Open Data Structures
A ScapegoatTree keeps itself balanced by partial rebuilding operations. During a partial rebuilding operation, an entire subtree is deconstructed and rebuilt ...
Read more >
A program to check if a Binary Tree is BST or not
If the value of the right child of the node is less than or equal to the current node then return false; If...
Read more >
11.8.2 - Minimal Cost-Complexity Pruning - STAT ONLINE
The complexity parameter α adjusts that. ... If the optimal subtrees are nested, the computation will be a lot easier. ... Here we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found