I am using a Template to Generate HTML Tag Attributes in Meteor, but is seems to be broken. I can’t put a template inside an HTML tag in meteor and I want to know why.
I’m using meteor-router for routing. Since meteor basically only allows a single layout, I wanted to add a class to the body indicating the current view name, like so:
/client/layout.html:
<head>
<title>Meteor App</title>
</head>
<body {{> body-attributes}}>
{{renderPage}}
</body>
<template name="body-attributes">data-view-name="{{page}}" class="{{page}}"<template>
So that I could do this in /client/layout.js:
Template['body-attributes'].page = function () {
return Meteor.Router.page();
};
But that didn’t work, I got an error from the app saying that attributes on the body are not supported. Changing layout.html to this:
<head>
<title>Meteor App</title>
</head>
<body>
<div {{> body-attributes}}>
{{renderPage}}
</div>
</body>
<template name="body-attributes">data-view-name="{{page}}" class="layout-container {{page}}"<template>
Resulted in totally broken HTML. The browser shows a ‘>’ before the page, and the console elements panel looks like so:

Why doesn’t that work?
Calling
{{> body-attributes}}inside an HTML tag (in your case, thebodytag) ultimately means that Meteor will inject a DocumentFragment into the DOM at that location.For the sake of reactivity, these
DocumentFragmentsare not vanilla reproductions of the markup in your template, but rather are wrapped up in special Meteoresque reactivity tags that help the backend engine keep theTemplateupdated. You can see this by invoking the Template function and investigating what_domFraglooks like:So,
Templatesin a meteor context are not designed to be embedded as attributes on HTML tags because it’s injecting additional tags within the tag itself. Your second attempt is more in line with the way the framework works.Hope that helps explain this behavior.