Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add advanced “TEMPLATE_ENGINE.md” documentation. #193

Merged
merged 1 commit into from
Oct 19, 2024

Conversation

theengineear
Copy link
Collaborator

@theengineear theengineear commented Oct 18, 2024

The point of this document is to explain (to folks that are already experts in web development) how html template engines conceptually work.

It walks the reader through a series of iterations which add increasing sophistication to a toy template engine library.

Closes #187.

@theengineear theengineear force-pushed the advanced-template-engine-docs branch from 61b0c6e to 0f2c0ff Compare October 18, 2024 16:31
@theengineear
Copy link
Collaborator Author

theengineear commented Oct 18, 2024

@klebba — Took some time (maybe more than I’d like to admit) trying to update these docs as you suggested.

As discussed, these are aimed at folks who are already experts with JS / DOM-APIs. Rather than be a high-level explainer (like the last one was) — it goes through a series of examples (each building off the last) to introduce the key problems that template engine has to solve.

@theengineear theengineear force-pushed the advanced-template-engine-docs branch 3 times, most recently from 4fa7318 to 7666e4c Compare October 18, 2024 18:16
klebba
klebba previously approved these changes Oct 18, 2024
An html template engine allows developers to ergonomically create dynamic web
applications — plain and simple.

One way to accomplish this would be to create an entire html document in a
Copy link
Collaborator

@klebba klebba Oct 18, 2024

Choose a reason for hiding this comment

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

IMO the server side example could be omitted here, else give a complete example for comparison. If the latter, point out that a server side template takes the form of a single string of markup to be unpacked by the browser. The browser would not be able to detect the difference between preprocessed HTML and static HTML

interface ought to be something like:

```js
render(container, html`<div>markup</div> + <div>${data}</div>`);
Copy link
Collaborator

@klebba klebba Oct 18, 2024

Choose a reason for hiding this comment

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

nit: the + looks like an operator — how about &plus;

const selectedItem = data.items[data.selected];

// Interpolate our template with our state idiomatically.
const result = html`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Following is an example which contains:
an attribute binding on line
a property binding on line
a list on line
nesting within the list
a conditional statement

These cover most of the cases the engine must handle

### Iteration 1 — a first crack

```js
// Recursively create html by joining strings with values.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: densify the comments here

* Some scripted implementation details leaking. ††††
* Change-by-value detection. †††††

† This has broad implications — including unsafe injection of strings.<br>
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: inline the subtext?


**What it doesn’t do**

* Nothing is cached and the DOM is thrashed.
Copy link
Collaborator

Choose a reason for hiding this comment

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

elaborate on thrashed

**What it doesn’t do**

* Some markup abstractions are leaking.
* Many edge cases are not considered.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: e.g.:

does recursive string concatenation… all the way to an html-aware, performant
DOM management system. Nice!

The remaining edge-cases and markup-cleanup won’t be covered here, but this
Copy link
Collaborator

Choose a reason for hiding this comment

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

perhaps "wont be covered here, but from here we would direct you to the source itself"

The point of this document is to explain (to folks that are already
experts in web development) how html template engines conceptually work.

It walks the reader through a series of iterations which add increasing
sophistication to a toy template engine library.

Closes #187.
@theengineear
Copy link
Collaborator Author

Iterated on nearly all the comments you left in here @klebba — the one main thing that I am going to put to later is adding additional code comments to the example code. I think that will actually take quite a bit more time to thoughtfully do and I’d like to pause on this for now.

Screenshot 2024-10-18 at 5 02 30 PM

@theengineear theengineear merged commit 15d9692 into main Oct 19, 2024
1 check passed
@theengineear theengineear deleted the advanced-template-engine-docs branch October 19, 2024 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consolidate / improve internal template engine logic.
2 participants