# Generic bbcode wrapper for theme components

**URL:** https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516
**Category:** Announcements
**Tags:** new-feature
**Created:** [July 20, 2019, 12:48pm UTC](https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516 "2019-07-20T12:48:59Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [July 20, 2019, 12:49pm UTC](https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516/1 "2019-07-20T12:49:00Z")

</div>

I added this feature a while back and realised I didn’t post about it. You can now use a special syntax in markdown to have it cooked and usable in theme components without having to write a plugin.

```plaintext
// wrapped in div.d-wrap
[wrap=baz foo=bar]Content[/wrap]

// wrapped in div.d-wrap
[wrap=baz foo=bar]
Content
[/wrap]

// wrapped in div.d-wrap
[wrap=baz foo=bar]
[/wrap]

// this one will be rendered as a span.d-wrap instead of a div.d-wrap
a [wrap=baz]Content[/wrap] b

```

The name of the component will be added as a data-attribute: `data-wrap="baz"` and every properties will also be data-attributes: `data-foo="bar"` on the element.

If you want to read about a real world use case, see

> [@Placeholder Forms](https://meta.discourse.org/t/discourse-placeholder-theme-component/113533):
>
> discourse2Summary Placeholder Forms will let you build dynamic documentation, by creating a form that replaces any occurrence of a =PATTERN= in your post with the value from a text or selectable input field.eyeglassesPreview [Preview on Discourse Theme Creator](https://discourse.theme-creator.io/theme/Discourse/placeholder-forms)hammer_and_wrenchRepository Link [https://github.com/discourse/discourse-placeholder-theme-component](https://github.com/discourse/discourse-placeholder-theme-component)open_bookNew to Discourse Themes? [Beginner’s guide to using Discourse Themes](https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966) Install this theme component …

---

<div class="post-metadata">

### Author: ![lcestou](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lcestou/32/148373_2.png) [@lcestou](https://meta.discourse.org/u/lcestou)
#### Post date: [July 22, 2019, 5:49am UTC](https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516/2 "2019-07-22T05:49:44Z")

</div>

Hello Joffrey,

Thanks for suggesting this feature in the other topic.

I was excited to try to use it and see if it worked for my case, however I encountered a problem.

It seems to ignore attribute names that has multiple hyphens in between and defaults to only pick the characters after the last hyphen.

I am using v2.4.0.beta2 +33, I apologize if this has been fixed in a newer commit since.

Examples:

```
// No multiple hyphen this works fine: data-bloodmallet="chart".
[wrap=test bloodmallet=chart]Content[/wrap]

// This way it omits "user" from the attribute name and output is data-id="1" instead of data-user-id="1".
[wrap=test2 user-id=1]Content[/wrap]

```

This following request is optional: Is there a way to not wrap the content inside the div with `<p>` tag?

Thanks for this feature and hopefully I can use it to fix my problem!

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [July 22, 2019, 6:14am UTC](https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516/3 "2019-07-22T06:14:50Z")

</div>

I can fix the first issue

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [July 22, 2019, 7:18am UTC](https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516/4 "2019-07-22T07:18:45Z")

</div>

I just pushed a fix for this, slight difference is that I won’t support foo-bar but fooBar. So you can now write:

```plaintext
[wrap=foo userId=1]
[/wrap]

```

And you will get:

```plaintext
<div data-wrap="foo" data-user-id="1">
</div>

```

---

<div class="post-metadata">

### Author: ![4ong](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/4ong/32/186618_2.png) [@4ong](https://meta.discourse.org/u/4ong)
#### Post date: [July 1, 2021, 9:18pm UTC](https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516/5 "2021-07-01T21:18:53Z")

</div>

Hi @j.jaffeux  
Thanks for this feature! Do you know the best direction how to add feature for things like

```plaintext
<button class="snipcart-add-item" data-item-id="product-1" data-item-url="/" data-item-name="Product #1" data-item-price="10.99">
  Add to cart
</button>

```

in topics. And generally - to whitelist

```plaintext
class="snipcart-add-item" data-item-id="product-1"

```

Working example - [https://codepen.io/thatfrankdev/pen/xxwRXQw](https://codepen.io/thatfrankdev/pen/xxwRXQw)

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [July 1, 2021, 9:53pm UTC](https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516/6 "2021-07-01T21:53:43Z")

</div>

You need to decorate your wrap in a theme component, like we do here for example: [discourse-placeholder-theme-component/javascripts/discourse/initializers/setup.js at main · discourse/discourse-placeholder-theme-component · GitHub](https://github.com/discourse/discourse-placeholder-theme-component/blob/main/javascripts/discourse/initializers/setup.js#L107)

---

<div class="post-metadata">

### Author: ![4ong](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/4ong/32/186618_2.png) [@4ong](https://meta.discourse.org/u/4ong)
#### Post date: [July 1, 2021, 10:19pm UTC](https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516/7 "2021-07-01T22:19:13Z")

</div>

@j.jaffeux Thanks for the awesome advice!

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [February 14, 2022, 1:07am UTC](https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516/8 "2022-02-14T01:07:55Z")

</div>

Recently we expanded this to support generic components using:

````plaintext
```customblock param=1
```

````

This will be translated to:

```plaintext
<pre data-code-param="1" data-code-wrap="customblock"><code class="lang-nohighlight"></code></pre>

```

Thus as a component you can use `data-code-wrap` to determine special handling and syntax highlighting is disabled.

We used this pattern here:

[https://github.com/discourse/discourse-mermaid-theme-component](https://github.com/discourse/discourse-mermaid-theme-component)

Which is an official mermaid theme component.
