Skip to content

Commit

Permalink
Support for <head> and <body> tags in server side templates.
Browse files Browse the repository at this point in the history
See: #110
  • Loading branch information
mitar committed Dec 31, 2015
1 parent 5dd3d21 commit 6fd2454
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## vNEXT

* Blaze Components can now process `<body>` tags in the server side templates, and ignore
`<head>` tags (which are already rendered on the server side).

## v0.16.0, 2015-Dec-30

* `this` inside a `component.autorun` computation is bound automatically to the component.
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ or a package does not depend on the `templating` packages (often through the `bl
Blaze Components supersedes the `templating` package and provides its functionality as well.

Blaze Components compile HTML template files both on the client and server side. If you have previously been adding
HTML files on the server side as well, you might want to limit those to the client side only. Moreover, server
side HTML template files do not support `<head>` or `<body>` tags.
HTML files on the server side as well, you might want to limit those to the client side only.

Additional packages
-------------------
Expand Down Expand Up @@ -1013,8 +1012,6 @@ on the server side, and elsewhere (like `lib` directory) if you want them to be
side. For packages, use `architecture` argument to your [`addFiles`](http://docs.meteor.com/#/full/pack_addFiles) calls
to control where are added files available, `'client'`, `'server'`, and `['client', 'server']`, respectivelly.

*Currently, server side HTML template files do not support `<head>` or `<body>` tags.*

Use with existing classes
-------------------------

Expand Down
4 changes: 4 additions & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ Package.onUse(function (api) {
api.addFiles([
'client.coffee'
], 'client');

api.addFiles([
'server.coffee'
], 'server');
});

Package.onTest(function (api) {
Expand Down
20 changes: 20 additions & 0 deletions patch-compiling.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,23 @@ SpacebarsCompiler.CodeGen.prototype.codeGenMustache = function (path, args, must
return originalCodeGenMustache.call(self, path, args, mustacheType);
}
};

// From tools/utils/archinfo.js.
var matches = function (host, program) {
return host.substr(0, program.length) === program &&
(host.length === program.length ||
host.substr(program.length, 1) === ".");
};

// <head> tag in server side templates should not be processed. Because client side
// templates already add <head> content to what Meteor renders on the server side.
// See: https://github.com/meteor/meteor/issues/5913
var originalAddCompileResult = CachingHtmlCompiler.prototype.addCompileResult;
CachingHtmlCompiler.prototype.addCompileResult = function (inputFile, compileResult) {
if (compileResult.head && !matches(inputFile.getArch(), 'web')) {
// Head can only be emitted to web targets.
delete compileResult.head;
}

originalAddCompileResult.call(this, inputFile, compileResult);
};
2 changes: 2 additions & 0 deletions server.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# No-op on the server.
Template.body.renderToDocument = ->
15 changes: 15 additions & 0 deletions tests/tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3139,4 +3139,19 @@ class BasicTestCase extends ClassyTestCase
,
/Invalid event handler/

testClientBody: ->
output = Blaze.toHTML Template.body

@assertTrue $($.parseHTML(output)).is('.bodyTest')

testServerBody: ->
output = Blaze.toHTML Template.body

@assertEqual trim(output), trim """
<div class="bodyTest">Body test.</div>
"""

testClientHead: ->
@assertTrue jQuery('head').find('noscript').length

ClassyTestCase.addTest new BasicTestCase()
1 change: 1 addition & 0 deletions tests/tests.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.bodyTest,
.eventsTestTemplate,
.animationTestTemplate,
.argumentsTestTemplate,
Expand Down
8 changes: 8 additions & 0 deletions tests/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,11 @@ <h3 class="insideBlockHelperTemplate">({{componentName}}/{{component.componentNa
{{> InvalidInlineEventsComponent}}
</div>
</template>

<body>
<div class="bodyTest">Body test.</div>
</body>

<head>
<noscript>JavaScript required.</noscript>
</head>

0 comments on commit 6fd2454

Please sign in to comment.