Skip to content

Commit

Permalink
Adds page.url to global data.
Browse files Browse the repository at this point in the history
Fixes 11ty#22
Fixes 11ty#36
  • Loading branch information
zachleat committed Jan 15, 2018
1 parent d85cdec commit b6d25b4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ This allows you to assign data values right in the template itself. Here are a f
* `pkg`: The local project’s `package.json` values.
* `pagination`: (When enabled in front matter) [Read more about Pagination](docs/pagination.md).
* `collections`: Lists of all of your content, grouped by tags. [Read more about Collections](docs/collections.md)
* `page`: Has information about the current page. Currently holds: `{ url: "/current/page/url.html" }`. Useful for finding the current page in a collection. [Read more about Collections](docs/collections.md) (look at _Example: Navigation Links with an `active` class added for on the current page_).

### Data Files

Expand Down
12 changes: 12 additions & 0 deletions docs/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ This will place this `mypost.md` into the `post` collection with all other piece
</ul>
```

### Example: Navigation Links with an `active` class added for on the current page

Comapre the `post.url` and special Eleventy-provided `page.url` variable to find the current page. Building on the previous example:

```
<ul>
{%- for post in collections.post -%}
<li{% if page.url == post.url %} class="active"{% endif %}>{{ post.data.title }}</li>
{%- endfor -%}
</ul>
```

## Tag Syntax

You can use a single tag, as in the above example OR you can use any number of tags for the content, using YAML syntax for a list.
Expand Down
17 changes: 16 additions & 1 deletion src/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,24 @@ class Template {
return Object.assign({}, data, mergedLayoutData, mergedLocalData);
}

async addPageUrlToData(data) {
if ("page" in data && "url" in data.page) {
debug(
"Warning: data.page.url is in use by the application will be overwritten: %o",
data.page.url
);
}
if (!("page" in data)) {
data.page = {};
}
data.page.url = await this.getOutputHref();
}

// getData (with renderData and page.url added)
async getRenderedData() {
let data = await this.getData();
await this.addPageUrlToData(data);

if (data.renderData) {
data.renderData = await this.mapDataAsRenderedTemplates(
data.renderData,
Expand Down Expand Up @@ -432,7 +448,6 @@ class Template {
data: data
};

// console.log( await this.render(data) );
map.date = await this.getMappedDate(data);

return map;
Expand Down
14 changes: 1 addition & 13 deletions src/TemplateMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class TemplateMap {
getMapTemplateIndex(item) {
let inputPath = item.inputPath;
for (let j = 0, k = this.map.length; j < k; j++) {
// inputPath should be unique (even with pagination?)
if (this.map[j].inputPath === inputPath) {
return j;
}
Expand Down Expand Up @@ -129,24 +130,11 @@ class TemplateMap {
}
}

async assignActiveTemplate(activeTemplate) {
if (activeTemplate) {
for (let collectionName in this.collectionsData) {
for (let item of this.collectionsData[collectionName]) {
// Assign active keys for all templates (both true and false)
item.active = await item.template.isEqual(activeTemplate);
}
}
}
}

async getCollectionsDataForTemplate(template) {
if (!this.collectionsData) {
await this.cache();
}

await this.assignActiveTemplate(template);

return this.collectionsData;
}
}
Expand Down
17 changes: 6 additions & 11 deletions src/TemplateWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const fs = require("fs-extra");
const parsePath = require("parse-filepath");
const Template = require("./Template");
const TemplatePath = require("./TemplatePath");
const TemplateRender = require("./TemplateRender");
const TemplateMap = require("./TemplateMap");
const EleventyError = require("./EleventyError");
const Pagination = require("./Plugins/Pagination");
Expand Down Expand Up @@ -199,11 +198,11 @@ TemplateWriter.prototype._createTemplateMap = async function(paths) {
return this.templateMap;
};

TemplateWriter.prototype._writeTemplate = async function(
tmpl,
outputPath,
data
) {
TemplateWriter.prototype._writeTemplate = async function(mapEntry) {
let outputPath = mapEntry.outputPath;
let data = mapEntry.data;
let tmpl = mapEntry.template;
console.log(mapEntry.data.page.url);
try {
await tmpl.writeWithData(outputPath, data);
} catch (e) {
Expand All @@ -225,11 +224,7 @@ TemplateWriter.prototype.write = async function() {
await this._createTemplateMap(paths);

for (let mapEntry of this.templateMap.getMap()) {
await this._writeTemplate(
mapEntry.template,
mapEntry.outputPath,
mapEntry.data
);
await this._writeTemplate(mapEntry);
}

eleventyConfig.emit(
Expand Down
10 changes: 0 additions & 10 deletions test/TemplateMapTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ test("populating the collection twice should clear the previous values (--watch
t.is(tm.getMap().length, 2);
});

test("Active template flags are set properly by `assignActiveTemplate`", async t => {
let tm = new TemplateMap();
await tm.add(tmpl1);
await tm.add(tmpl2);
let collectionsData = await tm.getCollectionsDataForTemplate(tmpl1);
t.is(collectionsData.all.length, 2);
t.true(collectionsData.all[0].active);
t.false(collectionsData.all[1].active);
});

test("TemplateMap adds collections data and has templateContent values", async t => {
let tm = new TemplateMap();
await tm.add(tmpl1);
Expand Down
11 changes: 11 additions & 0 deletions test/TemplateTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,14 @@ test("getMappedDate (created date)", async t => {
t.true(date instanceof Date);
t.truthy(date.getTime());
});

test("getRenderedData() has page.url", async t => {
let tmpl = new Template(
"./test/stubs/template.ejs",
"./test/stubs/",
"./dist"
);
let data = await tmpl.getRenderedData();

t.truthy(data.page.url);
});

0 comments on commit b6d25b4

Please sign in to comment.