Skip to content

Commit d2d133d

Browse files
ptrdoptrdo
authored andcommitted
Documents module compliance
1 parent 02b1c92 commit d2d133d

9 files changed

Lines changed: 114 additions & 70 deletions

File tree

README.md

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#### This is pre-release software. Use at your own risk.
2-
# microdata-template
1+
###### This is released software. Please **[log issues](https://github.com/ptrdo/microdata-template/issues)** found.
2+
# microdata-template `v2.0.0`
33
An implementation of HTML template by way of the microdata mechanism.
44
### The Gist
55
This JavaScript module should simplify adding dynamic content to HTML documents while staying true to the recommendations of web standards. There are no dependencies here except the JavaScript [ECMA5 standard](http://www.ecma-international.org/ecma-262/5.1/) which enjoys [nearly universal support](http://kangax.github.io/compat-table/es5/) in modern browsers. Also, since the HTML recommendations for integral technologies such as [template](https://www.w3.org/TR/html52/semantics-scripting.html#the-template-element) and [microdata](https://www.w3.org/TR/microdata/) are variably implemented by modern browsers, this module serves as a [polyfill](https://en.wikipedia.org/wiki/Polyfill) to assure reliable results. Best of all, this methodology encourages the writing of low-dependency JavaScript and perfectly valid HTML — even within fully-functional templated markup.
@@ -11,7 +11,8 @@ This JavaScript module should simplify adding dynamic content to HTML documents
1111
<script src="./lib/microdata-template.js"></script>
1212
<script>
1313
document.addEventListener("DOMContentLoaded", function() {
14-
var templater = new MicrodataTemplate();
14+
var templater = window.MicrodataTemplate.init();
15+
console.log("MicrodataTemplate!", templater.getVersion());
1516
});
1617
</script>
1718
```
@@ -39,7 +40,7 @@ var data = [{
3940
"Url": "/about"
4041
}];
4142

42-
var templater = new MicrodataTemplate();
43+
var templater = window.MicrodataTemplate.init();
4344
templater.render(document.getElementById("example"), data);
4445
```
4546

@@ -77,51 +78,43 @@ var templater = new MicrodataTemplate();
7778

7879
***
7980
### Advanced Usage
80-
Alternatively, the module can be assigned to a discrete namespace:
81-
```html
82-
<script>
83-
this.PTRDO = { utils: {} };
84-
this.exports = PTRDO.utils;
85-
</script>
86-
<script src="./lib/microdata-template.js"></script>
87-
<script>
88-
document.addEventListener("DOMContentLoaded", function() {
89-
var templater = new PTRDO.utils.MicrodataTemplate();
90-
});
91-
</script>
92-
```
93-
This module is organized to be attached to an HTML document as a simple external script, but also in a project governed by [Asynchronous Module Definition](https://en.wikipedia.org/wiki/Asynchronous_module_definition) (AMD) with a library such as [RequireJS](https://github.com/requirejs/requirejs), or a [CommonJS](https://en.wikipedia.org/wiki/CommonJS) project governed by a framework such as [NodeJS](https://en.wikipedia.org/wiki/Node.js). The expectations are the same, but the syntax used to load, instantiate, and then address the module will be as a local reference rather than a window namespace.
81+
This module is organized to be attached to an HTML document as a simple external script (see: [Simple Usage](https://github.com/ptrdo/microdata-template#simple-usage)), but also in a project governed by [Asynchronous Module Definition](https://en.wikipedia.org/wiki/Asynchronous_module_definition) (AMD) with a library such as [RequireJS](https://github.com/requirejs/requirejs), or an [ES6-compliant](http://es6-features.org/) project bundled by a library such as [Webpack](https://webpack.js.org/). The expectations are the same, but the syntax used to load, instantiate, and then address the module may be slightly different depending on circumstance.
9482

95-
**AMD (RequireJS) Implementation:**
83+
**Old-fashioned AMD (RequireJS) Implementation:**
9684
```javascript
9785
require.config({
98-
paths: { auth: "/path/to/idmorg-auth"},
99-
shim: { auth: { exports: "auth" }}
86+
paths: {
87+
text: "./node_modules/requirejs-text/text",
88+
json: "./node_modules/requirejs-json/json",
89+
}
10090
});
101-
102-
define(["auth"], function(Auth) {
103-
Auth.init({
104-
applicationName: "SomeAppName",
105-
endpoint: "https://comps-dev.idmod.org/api/",
106-
parentElement: someDOMElement
107-
});
108-
109-
Auth.signout();
91+
define(
92+
[
93+
"./path/to/microdata-template.js",
94+
"json!./path/to/collection.json"
95+
],
96+
function(templater, collection) {
97+
98+
var render = function(rootElement) {
99+
var templateElement = rootElement.querySelector("TR[itemscope]");
100+
templater.render(templateElement, collection);
101+
}
110102
});
111103
```
112-
**CommonJS (NodeJS) Implementation:**
104+
**New-fangled ES6 Module Implementation:**
113105
```javascript
114-
import Auth from "path/to/idmorg-auth.js";
115-
116-
$(function() {
117-
Auth.init({
118-
applicationName: "SomeAppName",
119-
endpoint: "https://comps-dev.idmod.org/api/",
120-
parentElement: someDOMElement
121-
});
122-
123-
Auth.signout();
124-
});
106+
import templater from "./path/to/microdata-template.js";
107+
import collection from "./path/to/data/collection.json";
108+
109+
class Example {
110+
111+
render(rootElement=document) {
112+
let templateElement = rootElement.querySelector("TR[itemscope]");
113+
templater.render(templateElement, collection);
114+
};
115+
}
116+
117+
export default Example;
125118
```
126119

127120
***

example/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
###### This is released software. Please **[log issues](https://github.com/ptrdo/microdata-template/issues)** found.
2+
# Examples of microdata-template
3+
These samples demonstrate implementations of the microdata-template utility for rendering a variety of data to an HTML page. Clone this project and then follow the instructions (below) which are most-appropriate for application.
4+
5+
***
6+
### simple-javascript
7+
This example includes HTML documents which can be run directly in a web browser. A web server (or localhost) is recommended for demonstrating complete functionality in real-world circumstance, but is not required. All links are relative, including to the data sources and the microdata-template library.
8+
9+
***
10+
### minimal-requirejs
11+
This example demonstrates integration into a minimal [Asynchronous Module Definition](https://en.wikipedia.org/wiki/Asynchronous_module_definition) (AMD) project governed by the [RequireJS](https://github.com/requirejs/requirejs) library. An enclosed `package.json` designates all dependencies required for running the demonstration.
12+
13+
[Yarn](https://yarnpkg.com/) is an excellent choice for managing packages for web clients and can be [installed a variety of ways](https://yarnpkg.com/en/docs/install). Alternatively, the Node Package Manager ([NPM](https://www.npmjs.com/get-npm)) can also be used.
14+
15+
**1:** From a command prompt, navigate to the project path where the clone of this repository is installed, and then the path where the relevant `package.json` file exists.
16+
```sh
17+
> cd C:\path\to\microdata-template\example\minimal-requirejs
18+
```
19+
**2:** From a command prompt, run the Yarn `install` command to get the dependencies as prescribed in the `package.json` file. This will create a path local to this example `\node_modules` for deposit of the downloaded code.
20+
```sh
21+
> yarn install
22+
```
23+
**3:** The file to run is `\minimal-requirejs\www\index.html`. A `localhost` or other webserver will be required for the page to import all runtime components and then render without error. One convenient means is use an Integrated Development Environment (IDE) such as JetBrain's [WebStorm](https://www.jetbrains.com/webstorm/) or [IntelliJ](https://www.jetbrains.com/idea/) which can open these files for review and edit and then run them in its internal webserver.
24+
25+
***
26+
### minimal-webpack
27+
This example demonstrates integration into a minimal [ES6-compliant](http://es6-features.org/) project bundled by the [Webpack](https://webpack.js.org/) library. An enclosed `package.json` designates all dependencies required for running the demonstration.
28+
29+
[NodeJS](https://nodejs.org/en/download/) is a technology which can execute scripts on a computer. In this application, NodeJS fasciliates the Webpack framework in assembling the various ingredients of the Client code, preparing them for deployment to a browser. It will be necessary to install NodeJS to run these examples.
30+
31+
The Node Package Manager ([NPM](https://www.npmjs.com/get-npm)) is installed as a component of NodeJS and is a popular means for executing the `package.json` of a project.
32+
33+
**1:** From a command prompt, navigate to the project path where the clone of this repository is installed, and then the path where the relevant `package.json` file exists.
34+
```sh
35+
> cd C:\path\to\microdata-template\example\minimal-webpack
36+
```
37+
**2:** From a command prompt, run the NPM `install` command to get the dependencies as prescribed in the `package.json` file. This will create a path local to this example `\node_modules` for deposit of the downloaded code.
38+
```sh
39+
> npm install
40+
```
41+
**3:** From a command prompt, run the NPM `start` command which has been configured in the `webpack.config.js` to instruct Webpack to survey the dependencies prescribed in the project code and then compile the bundled JavaScript.
42+
```sh
43+
> npm start
44+
```
45+
**4:** Open a browser and navigate to `http://localhost:8080` to view the deployed code. Note: If this does not work, there may be a conflict with other processes, so th `8080` port can be changed by [configuring the devServer](https://webpack.js.org/configuration/dev-server/).

example/simple-javascript/array.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@
108108
// This code will use the data provided here to populate a form.
109109
// Run this in a browser, and be sure to view the rendered source!
110110

111-
var templater = window.MicrodataTemplate;
112-
var templateElement = document.getElementsByTagName("DD")[0];
111+
var templater = window.MicrodataTemplate.init();
112+
var templateElement = document.querySelector("DD[hidden]");
113113

114114
var months = [
115115
"janvier",

example/simple-javascript/collection.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
// This code will load an adjacent data file and use it to draw a table.
5656
// Run this in a browser, and be sure to view the rendered source!
5757

58-
var templater = window.MicrodataTemplate;
58+
var templater = window.MicrodataTemplate.init();
5959
var templateBody = document.getElementsByTagName("TBODY")[0].getElementsByTagName("TR")[0];
6060

6161
var ajax = function(endpoint, options, callback) {
@@ -88,7 +88,7 @@
8888
request.send();
8989
};
9090

91-
ajax("data/collection.json", null, function(data){
91+
ajax("../data/collection.json", null, function(data){
9292

9393
if ("StatePopulations" in data) {
9494
templater.render(templateBody, data["StatePopulations"]);

example/simple-javascript/form.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
// This code will use the data provided here to populate a form.
4545
// Run this in a browser, and be sure to view the rendered source!
4646

47-
var templater = window.MicrodataTemplate;
47+
var templater = window.MicrodataTemplate.init();
4848
var templateElement = document.getElementsByTagName("FORM")[0];
4949

5050
var info = {

example/simple-javascript/multiple.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Microdata Template: Example: Multiple Renders</title>
6-
<script src="../lib/microdata-template.js" crossorigin="anonymous"></script>
6+
<script src="../../lib/microdata-template.js" crossorigin="anonymous"></script>
77
<style>
88
body {
99
font-family: monospace;
@@ -48,7 +48,7 @@
4848
// This code will use the sample data here to draw two aspects of a table.
4949
// Run this in a browser, and be sure to view the rendered source!
5050

51-
var templater = window.MicrodataTemplate;
51+
var templater = window.MicrodataTemplate.init();
5252
var templateHead = document.getElementsByTagName("THEAD")[0].getElementsByTagName("TH")[0];
5353
var templateBody = document.getElementsByTagName("TBODY")[0].getElementsByTagName("TR")[0];
5454

example/simple-javascript/object.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// This code will access the browser's navigator object and use it to draw a table.
5050
// Run this in a browser, and be sure to view the rendered source!
5151

52-
var templater = window.MicrodataTemplate;
52+
var templater = window.MicrodataTemplate.init();
5353
var templateElement = document.getElementsByTagName("TBODY")[0].getElementsByTagName("TR")[0];
5454

5555
templater.getSetShowHeritage(true);

example/simple-javascript/transformer.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h5 itemprop="screen_name">@{{ concat:user.screen_name }} tweeted:</h5>
5656
// Also, a custom transformer is employed for special formatting of date values.
5757
// Run this in a browser, and be sure to view the rendered source!
5858

59-
var templater = window.MicrodataTemplate;
59+
var templater = window.MicrodataTemplate.init();
6060
var templateElement = document.getElementsByTagName("BLOCKQUOTE")[0];
6161

6262
var ajax = function(endpoint, options, callback) {
@@ -89,7 +89,7 @@ <h5 itemprop="screen_name">@{{ concat:user.screen_name }} tweeted:</h5>
8989
request.send();
9090
};
9191

92-
ajax("data/transformer.json", null, function(data) {
92+
ajax("../data/transformer.json", null, function(data) {
9393

9494
templater.setTransformer("formatDate", function(value, index) {
9595
var time = Date.parse(value);

lib/microdata-template.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,32 @@
88
* @author Peter Sylwester
99
* @copyright (c)2018 Peter Sylwester
1010
* @license MIT
11-
* @version 1.02, 2018/01/02
11+
* @version 2.00, 2018/02/09
1212
* @requires "HTML5", "ECMA-262 Edition 5.1"
1313
*
14-
* AMD (RequireJS) IMPLEMENTATION:
1514
*
16-
* require.config({
17-
* paths: { templater: "/path/to/microdata-template"},
18-
* shim: { templater: { exports: "Templater" }}
19-
* });
15+
* ES6-COMPLIANT IMPLEMENTATION:
16+
*
17+
* import templater from "./path/to/microdata-template.js";
2018
*
21-
* define(["templater"], function(Templater) {
22-
* Templater.render(someHTMLElement, someData);
19+
* $(function() {
20+
* templater.render(someHTMLElement, someCorrespondingData);
2321
* });
2422
*
2523
*
26-
* CommonJS (NodeJS) IMPLEMENTATION
24+
* AMD IMPLEMENTATION (RequireJS):
2725
*
28-
* import Templater from "path/to/microdata-template.js";
26+
* require.config({
27+
* paths: { templater: "./path/to/microdata-template"},
28+
* shim: { templater: { exports: "templater" }}
29+
* });
2930
*
30-
* $(function() {
31-
* Templater.render(someHTMLElement, someData);
31+
* define(["templater"], function(templater) {
32+
* templater.render(someHTMLElement, someCorrespondingData);
3233
* });
3334
*
3435
*
35-
* BROWSER IMPLEMENTATION
36+
* ECMA-262 IMPLEMENTATION:
3637
*
3738
* <script type="text/javascript" src="./path/to/microdata-template.js"></script>
3839
*
@@ -43,14 +44,16 @@
4344
*/
4445

4546
var namespace = "MicrodataTemplate";
46-
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() :
47-
typeof define === "function" && define.amd ? define([], factory) :
48-
(root[namespace] = factory());
47+
typeof exports === "object" && typeof module !== "undefined"
48+
? module.exports = factory()
49+
: typeof define === "function" && "amd" in define
50+
? define([], factory)
51+
: ((root||window)[namespace] = factory());
4952

5053
}(this, (function () {
5154
"use strict";
5255

53-
var element, source;
56+
var element, source, version = "2.0.0";
5457

5558
/**
5659
* Markers are the HTML node element attributes used to designate components of the templated markup.
@@ -656,12 +659,15 @@
656659
},
657660

658661
getElement: function () {
659-
660662
return element;
661663
},
662664

663665
getTransformers: function () {
664666
return TRANSFORMER;
667+
},
668+
669+
getVersion: function () {
670+
return version;
665671
}
666672
};
667673
})));

0 commit comments

Comments
 (0)