Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handlebars Partial blocks don't work for Patterns #87

Open
giuseppeg opened this issue Aug 26, 2016 · 12 comments
Open

Handlebars Partial blocks don't work for Patterns #87

giuseppeg opened this issue Aug 26, 2016 · 12 comments

Comments

@giuseppeg
Copy link

Hi there,
do handlebars partial blocks work only for the styleguide templates or can I use them in Pattern templates as well?

I could use embed but partial-blocks are neat.

Here's what I am trying to do:

In patterns/components/MyComponent/index.hbs:

<div class="MyComponent">
   {{>@partial-block}}
</div>

Somewhere else:

{{#> patterns.components.MyComponent.index }}
   Lorem ipsum
{{/patterns.components.MyComponent.index }}

Expected result:

<div class="MyComponent">
   Lorem ipsum
</div>

Instead I get an error

Error: The partial @partial-block could not be found
@erikjung
Copy link
Contributor

@giuseppeg

That's interesting. This is definitely not by design, so we'll look into it.

@erikjung
Copy link
Contributor

@giuseppeg

Can you confirm that every instance of the partial in question is being invoked using the block syntax ({{#>)? In my testing, I got this same error, but realized that it was because I had used the partial multiple times (for different pattern variations), some with {{embed}}.

If you need to use either the block syntax or {{embed}} on a component partial, you can also try adding logic to check for @partial-block:

<div class="MyComponent">
  {{#if @partial-block}}    {{! <-------- for using the block syntax }}
    {{> @partial-block}}
  {{else}}
    {{#block "content"}}    {{! <-------- for using embed or extend }}
      Default content
    {{/block}}
  {{/if}}
</div>

@giuseppeg
Copy link
Author

Sorry @erikjung I was out for the weekend.

Yes I've used the block syntax.

If you need to use either the block syntax or {{embed}} on a component partial, you can also try adding logic to check for @partial-block

Do you mean if I want to use a template as embed AND partial? If so, no it is not my case.

It seems that drizzle is just not looking at the patterns/ folder for partials.

@erikjung erikjung added the docs label Aug 29, 2016
@erikjung
Copy link
Contributor

@giuseppeg

I looked into this a bit further, and it appears you can use {{>@partial-block}} as long as some care is taken for base patterns.

Here is an example that builds with no error:
https://github.com/cloudfour/drizzle/compare/debug-issue_87

This is not intuitive, but it kind of makes sense. All patterns are registered as partials, and they are also rendered as patterns unless you put hidden: true in the front matter. It is in this initial rendering that we encounter the error: @partial-block won't exist until that pattern is used as a partial with the block syntax.

So if you intend to make a "base" pattern to be extended, there are two options for using the partial block syntax:

  1. Make the base pattern hidden:

    ---
    hidden: true
    ---
    <div>{{>@partial-block}}</div>
  2. Use a condition to only output @partial-block if it exists:

    <div>
    {{#if @partial-block}}
      {{>@partial-block}}
    {{/if}}
    </div>

It's probably worth mentioning this in the docs :)

@giuseppeg
Copy link
Author

@erikjung awesome man! Thank you so much for debugging this for me :)
I will try your suggestions tomorrow.

@giuseppeg
Copy link
Author

@erikjung the approach seems to be working, however it explodes when I use it as simple partial without the block syntax:

---
hidden: true
---
<div>
  {{#if @partial-block}}
    {{>@partial-block}}
  {{else}}
     Default Content
  {{/if}}
</div>

Works

{{#>partial-name}}
{{/partial-name}}

Doesn't work: RangeError: Maximum call stack size exceeded

{{>partial-name}}

This should display the default content right?

@erikjung
Copy link
Contributor

@giuseppeg

That should work fine. I added another example to this test branch to verify the plain partial usage, and I wasn't able to reproduce that error: https://github.com/cloudfour/drizzle/compare/debug-issue_87

Can you pull that branch and verify?

@giuseppeg
Copy link
Author

Here is a diff of the part that cause the Maximum call stack size exceeded

If I remove

{{>"patterns.components.nav.item-link" href="#foo"}}

the build doesn't break

diff --git a/src/patterns/components/nav/example.hbs b/src/patterns/components/nav/example.hbs
new file mode 100644
index 0000000..7289bad
--- /dev/null
+++ b/src/patterns/components/nav/example.hbs
@@ -0,0 +1,14 @@
+{{#>patterns.components.nav.index}}
+
+  {{>"patterns.components.nav.item-link" href="#foo"}}
+
+  {{#>patterns.components.nav.item}}
+    {{#>patterns.components.nav.link href="#baz"}}
+      Link
+      <span class="SiteNav-wrapIcon">
+        <span class="Icon">🕶</span>
+      </span>
+    {{/patterns.components.nav.link}}
+  {{/patterns.components.nav.item}}
+
+{{/patterns.components.nav.index}}
diff --git a/src/patterns/components/nav/index.hbs b/src/patterns/components/nav/index.hbs
new file mode 100644
index 0000000..ed50b2c
--- /dev/null
+++ b/src/patterns/components/nav/index.hbs
@@ -0,0 +1,10 @@
+---
+hidden: true
+---
+<nav class="SiteNav" role="navigation">
+  {{#if @partial-block}}
+    {{>@partial-block}}
+  {{else}}
+    Nav List
+  {{/if}}
+</nav>
diff --git a/src/patterns/components/nav/item-link.hbs b/src/patterns/components/nav/item-link.hbs
new file mode 100644
index 0000000..a3ec35d
--- /dev/null
+++ b/src/patterns/components/nav/item-link.hbs
@@ -0,0 +1,12 @@
+---
+hidden: true
+---
+<span class="SiteNav-item">
+  <a class="SiteNav-link" href="{{href}}">
+    {{#if @partial-block}}
+      {{>@partial-block}}
+    {{else}}
+      Link
+    {{/if}}
+  </a>
+</span>
diff --git a/src/patterns/components/nav/item.hbs b/src/patterns/components/nav/item.hbs
new file mode 100644
index 0000000..ba6dc63
--- /dev/null
+++ b/src/patterns/components/nav/item.hbs
@@ -0,0 +1,10 @@
+---
+hidden: true
+---
+<span class="SiteNav-item">
+  {{#if @partial-block}}
+    {{>@partial-block}}
+  {{else}}
+    Item
+  {{/if}}
+</span>
diff --git a/src/patterns/components/nav/link.hbs b/src/patterns/components/nav/link.hbs
new file mode 100644
index 0000000..bbf8d23
--- /dev/null
+++ b/src/patterns/components/nav/link.hbs
@@ -0,0 +1,10 @@
+---
+hidden: true
+---
+<a class="SiteNav-link" href="{{href}}">
+  {{#if @partial-block}}
+    {{>@partial-block}}
+  {{else}}
+    Link
+  {{/if}}
+</a>

@erikjung
Copy link
Contributor

@giuseppeg

Thanks for the additional info. It looks like this is an issue that affects Handlebars itself: https://tonicdev.com/erikjung/57c72b19ad12611300cb9f58

^ Same error conditions (no error when removing the patterns.components.nav.item-link partial)

@giuseppeg
Copy link
Author

@erikjung I see, thanks for taking the time to dig into this issue!
I am using handlebars for the first time so I thought I was doing something wrong.

[unrelated] I am working (hacking) on a fork of drizzle that integrates suitcss and what I think is a great (better) folders structure, I am also considering to make a template-engine agnostic builder pkg that uses consolidate.js or metalsmith-layouts behind the scenes – basically I may end up writing my own styleguide generator tool from scratch 😂 let me know if you folks are interested in a collaboration or want to discuss about this in details.

@erikjung
Copy link
Contributor

@giuseppeg We use SUIT on most of our projects, so that's great 👍

The template handling is also something we plan on improving for 1.0. We've received some good feedback on it, and using Consolidate seems to make a lot of sense.

@tylersticka
Copy link
Member

basically I may end up writing my own styleguide generator tool from scratch

That's how we ended up making Drizzle (forked Fabricator)!

Our team welcomes contributions and collaborations. If you have ideas, proposals or wish to discuss some aspect of the hack/fork/project you're working on to see if your ideas would be a good fit for Drizzle proper, please don't hesitate to create issues or ping any of us!

teamwork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants