Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Precompilation and line endings in YTemplater #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ ytemplater.precompileShifterModule('my-shifter-module/');

Returns a [Q promise](https://github.com/kriskowal/q) that is resolved once the JavaScript file(s) have been written (or rejected in the event of an error).

#### Precompilation and Line Endings

By default, YTemplater precompiles the templates using the same line endings as the ones set in the corresponding .handlebar or .micro file. Consider this if you are planning on versioning any of the precompiled files in a collaborative cross-platform project, as different explicit line endings might cause versioned template files to be dirty.

The following is an example of precompiled template code with explicit Windows line endings:

```
function program1(depth0,data) {

var buffer = "", stack1;

// explicit line feed within the generated code
buffer += "\r\n <select name=\"acuity\" id=\"";

if (stack1 = helpers.id) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
else { stack1 = depth0.id; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
buffer += escapeExpression(stack1)
+ "\">\r\n <option value=\"\" selected=\"selected\"/>\r\n </select>\r\n ";
return buffer;
}
```

#### build.json Reference

YTemplater will look for a `ytemplater` section in the shifter module's `build.json` to determine what template modules to build. This section should contain one or more declared template modules (by module name) each containing a `templateFiles` array with the list of templates (paths to template files relative to the `templates` sub-directory of the shifter module dir) to precompile for that module.
Expand Down
10 changes: 8 additions & 2 deletions test/precompile-templates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ describe('precompile-templates', function() {
it('should precompile to JavaScript that is revivable with the YUI ' + engineInfo.className + ' Template engine', function(done) {
precompile().done(function(templateData) {
var templater = new Template(engineInfo.engine),
templateFn = templater.revive(evalTemplateFunctionString(templateData.precompiled)),
data = { food: 'cake' };
data = { food: 'cake' },
templateFn;

if (engineInfo.className === 'Y.Handlebars') {
templateFn = templater.revive(evalTemplateFunctionString(templateData.precompiled), templater.engine);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding templater.engine as the second argument doesn't make sense to me. Could you please explain?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unit test failing was when running it under the Y.Handlebars engine.

In Y.Handlebars, the templater.revive method is overridden by the function template, were the environment variable is used to call methods like:

  • env.VM.checkRevision
  • env.VM.invokePartial
  • env.compile

All those methods are existing within the engine object itself, hence me passing it as parameter.

} else {
templateFn = templater.revive(evalTemplateFunctionString(templateData.precompiled));
}

expect(templateFn(data)).to.equal('My favorite food is cake.\n');

Expand Down