Skip to content

Commit

Permalink
Rename $ to json.
Browse files Browse the repository at this point in the history
File                             Raw         Min         Zip     Min/Zip
index.js                 30301/29.59  9884/09.65  7580/07.40  3821/03.73

Closes #109.
Closes #107.
  • Loading branch information
flatheadmill committed Jun 25, 2013
1 parent 29706b5 commit b9ee341
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 27 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,67 @@ serialize to older HTML flavors for older browsers.
On the browser, when we generate Stencil XML, we simply import it into the
existing DOM using `Document.adoptNode`.

### The Context Object

From within your templates you can reference the context object itself either by
referencing `this` or the special variable dollar sign `$` with is an alias for
for `this`.

```xml
<html xmlns:s="stencil">
<body>
<dl>
<s:each select="Object.keys(this)" as="key">
<dt><s:value select="key"/></dt>
<dd><s:value select="this[key]"/></dd>
</s:each>
</dl>
</body>
</html>
```

You can also use the dollar sign `$` alias, if you find that more aesthetically
pleasing.

```xml
<html xmlns:s="stencil">
<body>
<dl>
<s:each select="Object.keys($)" as="key">
<dt><s:value select="key"/></dt>
<dd><s:value select="$[key]"/></dd>
</s:each>
</dl>
</body>
</html>
```

But, again, why? Only because I'm doing this stupid thing with prototypes that
I'm going to try to document right here...

### The `stencil` Variable

The `stencil` variable is a special object added to the context by the Stencil
that has properties of the current template. It is not visible in the ordinary
scope of template, (why? why not? I mean really, why was this important? now you
have to add a paragraph explaining special properties? Are you just being a
bonehead? Didn't we talk about that? No really, I can't wait to see this...)

You can distinguish the context variables set by the Stencil engine from the
context variables set by your application using `hasOwnProperty`. I'm not sure
if this is useful, but it's there.

(Oh, you're a real prize. Can we please [kill this
poodle](http://www.lileks.com/bleats/archive/03/0103/010301.html#010303)? You
know how else you could distinguish between a property set by the Stencil engine
and a property provided by your application? `key == "stencil"`. Yes. You are a
bonehead. TODO: Don't be such a bonehead. Then you don't have to document this.
You might not be able to accept this right now, but there are people in this
world how are smart enough to figure that out for themselves, I know, hard to
accept that world won't embrace the genius of your using object prototypes to do
something, that you know what you're doing, but there are object prototypes
involved and that's really special, so, I can't even, I can't.)

## Tag Libraries

Using the same language as used in templates, Stencil supports the creation of
Expand Down
12 changes: 12 additions & 0 deletions diary.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ uses JSON resources, it loads JavaScript modules, it loads XML, but it does not
load JSON resources. That should be set in the context by the application or the
application framework, then pushed through Stencil to the scaffolds that use it.

**Umdate**: Um, but, how are you going to market Stencil? As a command line
application or as a the cure for a noder's Rails envy? Oh, hai, yes, you just
bolt on the only way your program will ever work if you need it, but it wasn't
necessary because in theory, Stencil could be used in burgeoning command line
generated HTML5 field of applications.

Have you stopped to think about how the one thing everyone has to swallow is
that stencil generates **hideous** HTML? Stencil is a DOM templating language,
not a string templating language, they'll hear you say, and then your going to
waggle your ***hideous*** HTML in their face by spewing it all over their
console? People are not going to like you as a person.

## Module Loading

I'm not loving having all that cladding wrapped around a Stencil scaffold, when
Expand Down
28 changes: 15 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
}

if (!json) {
json = function (url, callback) {
get(url, 'responseText', function (error, body) {
if (error) callback(error);
else callback(null, JSON.parse(body));
});
json = function (url) {
return function (callback) {
url = this.stencil.absolutize(this.stencil.url + '/..', url);
get(url, 'responseText', function (error, body) {
if (error) callback(error);
else callback(null, JSON.parse(body));
});
}
}
}

Expand Down Expand Up @@ -460,13 +463,7 @@
function rewrite (parent, frames, page, template, includes, named,
directives, path, context, generating, callback) {
var okay = validator(callback), prefix = '$';
context = extend(Object.create({ $: function (url) {
return function (callback) {
json(absolutize(template.url + '/..', url), function (error, result) {
callback(error, result);
});
}
}}), context);
context = extend(Object.create(template.context), context);
if (frames[0].attributes) {
frames[0].attributes.forEach(function (attributes) {
context[prefix + 'attributes'] = attributes;
Expand Down Expand Up @@ -641,7 +638,12 @@

// Create our template.
templates[url] = {
url: url, page: page, directives: directives, tags: tags
url: url, page: page, directives: directives, tags: tags,
context: {
stencil: {
url: url, absolutize: absolutize, normalize: normalize, json: json
}
}
};

// Send the template to our caller.
Expand Down
15 changes: 9 additions & 6 deletions json/file.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var fs = require("fs"), path = require("path");
exports.create = function create (base) {
return function json (file, callback) {
try {
callback(null, JSON.parse(fs.readFileSync(path.join(base, file), "utf8")));
} catch (error) {
callback(error);
exports.create = function (base) {
return function (file) {
return function (callback) {
try {
file = this.stencil.absolutize(this.stencil.url + '/..', file);
callback(null, JSON.parse(fs.readFileSync(path.join(base, file), "utf8")));
} catch (error) {
callback(error);
}
}
}
}
2 changes: 1 addition & 1 deletion t/context/fixtures/resolver.stencil
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html xmlns:s="stencil">
<body>
<ul>
<s:each select="$.$('items.json')" as="item">
<s:each select="$.stencil.json('items.json')" as="item">
<li><s:value select="item.name"/></li>
</s:each>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion t/context/resolver.t.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

require('./proof')(2, function (step, context, ok, compare, fixture) {
var fs = require('fs');
var fs = require('fs'), json = require('../../json/file').create(__dirname);

step(function (stencil, resolver) {
context.generate('fixtures/resolver.stencil', {}, step());
Expand Down
5 changes: 2 additions & 3 deletions t/directives/proof.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var fs = require('fs'), path = require('path');
module.exports = require('proof')(function () {
var javascript = require('../../javascript/common').create(__dirname),
xml = require('../../xml/file').create(__dirname),
json = require('../../json/file').create(__dirname);
xml = require('../../xml/file').create(__dirname);
var context =
{ context: require('../..').create(javascript, xml, json)
{ context: require('../..').create(javascript, xml)
, compare: require('../compare')
, fixture: function (file, callback) { fs.readFile(path.resolve(__dirname, file), 'utf8', callback) }
};
Expand Down
5 changes: 2 additions & 3 deletions t/tags/proof.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var fs = require('fs'), path = require('path');
module.exports = require('proof')(function () {
var javascript = require('../../javascript/common').create(__dirname),
xml = require('../../xml/file').create(__dirname),
json = require('../../json/file').create(__dirname);
xml = require('../../xml/file').create(__dirname);
var context =
{ context: require('../..').create(javascript, xml, json)
{ context: require('../..').create(javascript, xml)
, compare: require('../compare')
, fixture: function (file, callback) { fs.readFile(path.resolve(__dirname, file), 'utf8', callback) }
};
Expand Down

0 comments on commit b9ee341

Please sign in to comment.