# Document Fragment to Stache Template

**URL:** https://forums.bitovi.com/t/document-fragment-to-stache-template/82
**Category:** CanJS
**Created:** [December 22, 2015, 12:37am UTC](https://forums.bitovi.com/t/document-fragment-to-stache-template/82 "2015-12-22T00:37:06Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![thecountofzero](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/thecountofzero/32/79_2.png) [@thecountofzero](https://forums.bitovi.com/u/thecountofzero)
#### Post date: [December 22, 2015, 12:37am UTC](https://forums.bitovi.com/t/document-fragment-to-stache-template/82/1 "2015-12-22T00:37:06Z")

</div>

Is there a simple way to take a document fragment and create a stache template that we can insert into the document?

---

<div class="post-metadata">

### Author: ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)
#### Post date: [December 22, 2015, 1:12am UTC](https://forums.bitovi.com/t/document-fragment-to-stache-template/82/2 "2015-12-22T01:12:25Z")

</div>

What do you mean? Stache templates return document fragments.

---

<div class="post-metadata">

### Author: ![thecountofzero](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/thecountofzero/32/79_2.png) [@thecountofzero](https://forums.bitovi.com/u/thecountofzero)
#### Post date: [December 22, 2015, 1:47am UTC](https://forums.bitovi.com/t/document-fragment-to-stache-template/82/3 "2015-12-22T01:47:03Z")

</div>

Let’s say I’ve created a document fragment using JavaScript. Said fragment contains custom elements which will be handled by a can.Component. Now I want to insert said fragment into the DOM. I was trying this late last night and most likely overlooked the simple solution…

---

<div class="post-metadata">

### Author: ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)
#### Post date: [December 22, 2015, 1:53am UTC](https://forums.bitovi.com/t/document-fragment-to-stache-template/82/4 "2015-12-22T01:53:26Z")

</div>

Components only work if their elements are created by a template. The fragment needs to be created by stache.

```auto
var frag = can.stache(" .... <custom-element> .... </custom-element> ....");
$("body").append(frag);

```

You can not (easily) programmatically create a fragment and have custom element behavior. Something like:

```auto
var frag = document.createDocumentFragment();
var custom = document.createElement('custom-element');
frag.appendChild(custom);
$("body").append(frag);

```

does not work.

---

<div class="post-metadata">

### Author: ![thecountofzero](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/thecountofzero/32/79_2.png) [@thecountofzero](https://forums.bitovi.com/u/thecountofzero)
#### Post date: [December 22, 2015, 1:55am UTC](https://forums.bitovi.com/t/document-fragment-to-stache-template/82/5 "2015-12-22T01:55:02Z")

</div>

That is what I feared. Ugh. Ok, I will re-think my plan of attack. Was more of a proof on concept anyhow. Thanks for the confirmation.

Hope all is well. I’ve been MIA for a while, but should be back in the fray…

---

<div class="post-metadata">

### Author: ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)
#### Post date: [December 22, 2015, 1:55am UTC](https://forums.bitovi.com/t/document-fragment-to-stache-template/82/6 "2015-12-22T01:55:32Z")

</div>

You can initialize a Component manually. You just need to pass it the element and [tagData](http://canjs.com/docs/can.view.tagData.html):

```auto
new CustomElement(el, {.. tagData...}) 

```
