frctl / fractal

A tool to help you build and document website component libraries and design systems.
https://fractal.build
MIT License
2.11k stars 168 forks source link

Using `contextData` handlebars helper #213

Closed maban closed 6 years ago

maban commented 7 years ago

I'm trying to override the text in a button in one of my view templates. I'd like to do this using contextData, following the documentation:

<!--
  The same as above, but passing in some data to override
   some items in the @button-2 data. 'someData' is an
   object, i.e. {text: 'foo'}
-->
{{> '@button-1' (contextData '@button-2' someData) }}

When I look at the example, @paulrobertlloyd has done it like this:

{% render "@button", { label: "Search", submit: true } %}

How do I write the same thing in Handlebars? I've been trying for quite a while but keep getting syntax errors. Here's what I've tried (including separating with commas, adding double quotes…) but I'm missing something.

{{> '@button-1' (contextData '@button-2' {text: 'foo'} }}

What am I doing wrong here?

Really enjoying using Fractal properly, by the way! :)

allmarkedup commented 7 years ago

Hey @maban - unfortunately Handlebars doesn't let you define data objects in-line like this :-( It's one of my main frustrations with Handlebars as a template language for component libraries.

To get something similar to work you'd can define a property in your context with the data you want to merge in, like this:

{
  context: {
    customButton: {
      text: 'foo'
    }
  }
}

And then use it like:

{{> '@button-1' (contextData '@button-2' customButton }}

Obviously you lose the convenience of being able to define that customButton data on-the-fly in the template but this is unfortunately a Handlebars limitation and so not something that Fractal can do much about!

maban commented 7 years ago

Awww maaaaan! Do you know of a helper that will let me do this?

allmarkedup commented 7 years ago

No I'm afraid not - it's a language-level limitation so not something that a helper can work around (especially in a subexpression like this). Sorry :-(

maban commented 7 years ago

@joshje had a look and helped me write this:

{{> @button label="Search" type="Submit" }}

It works! I don't know how but it does 👍

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.