Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber623 committed Mar 27, 2018
0 parents commit e6f37f2
Show file tree
Hide file tree
Showing 14 changed files with 504 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig is awesome: http://EditorConfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_size = 2
indent_style = space
trim_trailing_whitespace = true

[*.md]
indent_size = 4
indent_style = tab
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/*
scripts/*
18 changes: 18 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
env:
browser: true
es6: true
extends: 'eslint:recommended'
rules:
indent:
- error
- 2
- VariableDeclarator: 2
linebreak-style:
- error
- unix
quotes:
- error
- single
semi:
- error
- always
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Jason Garber

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# TemplateTemplate

**A very small JavaScript `<template>` manipulation library.**

[![npm](https://img.shields.io/npm/v/templatetemplate.svg?style=for-the-badge)](https://www.npmjs.com/package/templatetemplate)
[![Downloads](https://img.shields.io/npm/dt/templatetemplate.svg?style=for-the-badge)](https://www.npmjs.com/package/templatetemplate)

## Acknowledgments

TemplateTemplate is written and maintained by [Jason Garber](https://sixtwothree.org/) and is another in a growing collection of small, curiously-named JavaScript utilities:

- [CashCash](https://github.com/jgarber623/CashCash), a very small DOM library inspired by jQuery.
- [RadioRadio](https://github.com/jgarber623/RadioRadio), a very small [PubSub](https://en.wikipedia.org/wiki/Publish–subscribe_pattern) library.
- [RouterRouter](https://github.com/jgarber623/RouterRouter), a very small routing library extracted from [Backbone's Router](http://backbonejs.org/docs/backbone.html#section-169).

## License

TemplateTemplate is freely available under the [MIT License](https://opensource.org/licenses/MIT). Use it, learn from it, fork it, improve it, change it, tailor it to your needs.
35 changes: 35 additions & 0 deletions dist/templatetemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*!
* TemplateTemplate 0.1.0
*
* A very small JavaScript <template> manipulation library.
*
* Source code available at: https://github.com/jgarber623/TemplateTemplate
*
* (c) 2018-present Jason Garber (https://sixtwothree.org)
*
* TemplateTemplate may be freely distributed under the MIT license.
*/

(function(root, factory) {
root.TemplateTemplate = factory();
})(this, function() {
"use strict";
return function(template, insertions) {
template = template instanceof HTMLElement ? template : document.querySelector(template);
var fragment = document.importNode(template.content, true);
Object.entries(insertions).forEach(function(insertionArray) {
var node = fragment.querySelector(insertionArray[0]), insertion = insertionArray[1];
if (insertion instanceof HTMLElement || insertion instanceof DocumentFragment) {
node.appendChild(insertion);
} else if (insertion instanceof Array) {
node.textContent = insertion[0];
Object.entries(insertion[1]).forEach(function(attributesArray) {
node.setAttribute(attributesArray[0], attributesArray[1]);
});
} else {
node.textContent = insertion;
}
});
return fragment;
};
});
13 changes: 13 additions & 0 deletions dist/templatetemplate.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*!
* TemplateTemplate 0.1.0
*
* A very small JavaScript <template> manipulation library.
*
* Source code available at: https://github.com/jgarber623/TemplateTemplate
*
* (c) 2018-present Jason Garber (https://sixtwothree.org)
*
* TemplateTemplate may be freely distributed under the MIT license.
*/

!function(t,e){t.TemplateTemplate=function(){"use strict";return function(t,e){t=t instanceof HTMLElement?t:document.querySelector(t);var n=document.importNode(t.content,!0);return Object.entries(e).forEach(function(t){var e=n.querySelector(t[0]),o=t[1];o instanceof HTMLElement||o instanceof DocumentFragment?e.appendChild(o):o instanceof Array?(e.textContent=o[0],Object.entries(o[1]).forEach(function(t){e.setAttribute(t[0],t[1])})):e.textContent=o}),n}}()}(this);
79 changes: 79 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">

<title>TemplateTemplate: Examples</title>
</head>
<body>

<table id="projects-table">
<caption>A table of open source projects.</caption>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Author</th>
<th scope="col">URL</th>
<th scope="col">Languages</th>
</tr>
</thead>
<tbody></tbody>
</table>

<template id="anchor-template">
<a></a>
</template>

<template id="project-template">
<tr>
<th class="project-name" scope="row"></th>
<td class="project-author"></td>
<td class="project-url"></td>
<td class="project-languages"></td>
</tr>
</template>

<script src="../src/templatetemplate.js"></script>
<script>
var tbody = document.querySelector('#projects-table tbody'),
anchor = document.createElement('a');

anchor.setAttribute('href', 'https://sixtwothree.org');
anchor.textContent = 'Jason Garber'

tbody.appendChild(
TemplateTemplate(document.querySelector('#project-template'), {
'.project-name': 'TemplateTemplate',
'.project-author': 'Jason Garber',
'.project-url': 'https://github.com/jgarber623/TemplateTemplate',
'.project-languages': 'JavaScript'
})
);

tbody.appendChild(
TemplateTemplate('#project-template', {
'th': 'RadioRadio',
'.project-author': anchor,
'td:nth-child(3)': 'https://github.com/jgarber623/RadioRadio',
'td:last-child': 'JavaScript'
})
);

tbody.appendChild(
TemplateTemplate('#project-template', {
'th': 'CashCash',
'.project-author': 'Jason Garber',
'.project-url': ['github.com/jgarber623/CashCash', {
'style': 'font-weight: bold;'
}],
'td:last-child': TemplateTemplate('#anchor-template', {
'a': ['JavaScript', {
'href': 'https://github.com/search?q=language%3AJavaScript'
}]
})
})
);
</script>

</body>
</html>
Loading

0 comments on commit e6f37f2

Please sign in to comment.