eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

provide JavaScript Layout Templates' `data` and `permalink` class members with some useful info

Open GerHobbelt opened this issue 5 years ago • 0 comments

Provide JavaScript Layout Templates' data and permalink class members with some useful info: the inputPath (template), the invoking template instance and the current engine instance.

permalink

Relevant documentation which drove this change: https://www.11ty.dev/docs/languages/javascript/#permalink-function -- without the change, the permalink member in a _data/layouts/layout.js layout template will fail when it's made a function like the documentation says it can. (The magic line there being https://github.com/11ty/eleventy/compare/master...GerHobbelt:custom-permalinks?expand=1#diff-af43fd5417c10edb3b7199d7c847be65R119)

data

Extra are the extra parameters passed into such a class' data member function: template and engine. For that, a few other classes needed to have their APIs changed as well. (Magic line: https://github.com/11ty/eleventy/compare/master...GerHobbelt:custom-permalinks?expand=1#diff-34fe2185370965f1cbbd2d8e5e0f3e97R98)


The meat for making the permalink member work as a member function type is in src/Template.js, while the meat for the data member function is in src/Engines/JavaScript.js (the inst.data(...) call in there).

Related issues

While working on this I did an issue search (including closed ones) on the 11ty repo in hopes to find someone had encountered the same and tackled it. Alas, not entirely: what I wanted was 11ty to produce URIs which are not exactly "cool" in the perception of 11ty, but which are at least pre/postfixed with a content hash for each page, kinda like 'immutable URIs': every URI has guaranteed never changing content.

After a few iterations I went for the custom dynamic permalink + JavaScript Layout Template approach, defining a site-global template in _data/layouts/default.11ty.js along the lines of https://www.11ty.dev/docs/languages/javascript/#optional-data-method (and onwards in that page). The result was a number of failures, including the lack of template (and engine) info in both the data and permalink members of said _data/layouts/default.11ty.js.

Also there was the surprise mistake of naming the bugger _data/layout/default.js and only finding out it MUST be with .11ty.js extension (another one where #1079 was needed to help me out).

Anyway, these issues look like they come close/near:

  • https://github.com/11ty/eleventy/issues/213
  • https://github.com/11ty/eleventy/issues/913 (as that's rather closer to the sort of URI set I was looking for than default 11ty behaviour)
  • https://github.com/11ty/eleventy/issues/1076 (not the same as #1081 as far as I understood, but darn close, is my hunch after the fact)
  • https://github.com/11ty/eleventy/issues/379#issuecomment-506927758-permalink describes one of the scenarios where (site wide?) link rewriting would be required
  • https://github.com/11ty/eleventy/issues/943 reports one of the errors that I encountered which led to this pullreq

GerHobbelt avatar Apr 09 '20 00:04 GerHobbelt