2016-06-12 97 views
0

我使用流星,但想使用一些使用html模板元素的香草javascript/HTML代碼。如何在Meteor Blaze模板中包含HTML <template>元素?

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template

當我嘗試包括一個流星模板中:

<template name="test2"> 
    <template id="test3">Some stuff i will clone in javascript</template> 
</template> 

流星顯示以下錯誤:

While processing files with templating (for target web.browser): 
client/index.html:153: Expected one of: <body>, <head>, <template> 

有沒有辦法在一個流星,包括這些模板?我知道我可以將它們包含在body標籤中,但它有點笨拙。

+0

你特別感興趣的嵌入模板標籤或者你只是想調用一個子模板? –

+0

特別感興趣的是使用標準的JavaScript嵌入模板標籤,我可以查詢,克隆等。 – Greg

回答

0

使用輔助

在客戶端的js文件

Template.registerHelper('vanillaTemplate',() => { 
    return '<template id="test3">Some stuff i will clone in javascript</template>' 
}) 

在客戶端的HTML文件

<template name="test2"> 
    {{> vanillaTemplate}} 
</template> 
+0

謝謝,這將會很好。 – Greg

1

AFAICT這不能流星完成,因爲流星使用模板標籤整整其設計,從你的鏈接的原因:

The HTML template element is a mechanism for holding client-side content that is not to be rendered when a page is loaded but may subsequently be instantiated during runtime using JavaScript.

你不應該看到<template>標籤居然出現在DOM這會阻止你在以後的DOM中操作它。

這聽起來像你也許試圖做一些Blaze本身承擔的責任。也許你可以告訴我們你想用你的模板做什麼,我們可以告訴你如何在流星中做到這一點。

+0

哦,我只是有一些使用HTML模板的庫,我想我可以編寫一些代碼來移植它們,但更容易將它們作爲一個大塊粘在body元素中。雖然將它們分成單獨的文件本來是很好的。 – Greg