TmplUtil
Template utility.
- License:
- Apache-2.0Copyright 2020 Satoshi Soma
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. - Source:
- lib/TmplUtil.js, line 24
Methods
attrs(data, sepopt) → string
Composes HTML attributes from the given object.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
data
|
|
Object with keys as attribute names, or just a string |
|
sepopt
|
string |
(whitespace)
|
Separator for array values |
- Source:
- lib/TmplUtil.js, line 92
Returns:
HTML attributes
- Type:
- string
br(str) → string
Replaces linebreak chars with <br> in the given string.
Parameters:
| Name | Type | Description |
|---|---|---|
str
|
string |
- Source:
- lib/TmplUtil.js, line 221
Returns:
Modified string
- Type:
- string
classes(obj) → string
Outputs class attribute with the given object's keys.
Parameters:
| Name | Type | Description |
|---|---|---|
obj
|
object |
- Source:
- lib/TmplUtil.js, line 121
Returns:
class attribute
- Type:
- string
e(str) → string
Escapes non-safe characters in the given string.
Parameters:
| Name | Type | Description |
|---|---|---|
str
|
string |
- Source:
- lib/TmplUtil.js, line 133
Returns:
Modified string
- Type:
- string
elem(name, attrsopt, contentopt) → string
Composes HTML an element with the given arguments.
Parameters:
| Name | Type | Description |
|---|---|---|
name
|
string | Tag name |
attrsopt
|
|
Attributes. Requires the number of args to be 3 |
contentopt
|
string | Content of the element |
- Source:
- lib/TmplUtil.js, line 57
Returns:
HTML
- Type:
- string
Example
const $ = require('TmplUtil'); let html = $.elem( 'a', { href: 'https://example.com', title: 'Example Site' }, 'See example' );Composing a simple anchor element
elems(…argsrpt) → string
Composes multiple HTML elements with the given array of arguments.
Parameters:
| Name | Type | Description |
|---|---|---|
argsrpt
|
array | Array(s) of arguments for TmplUtil#elem |
- Source:
- lib/TmplUtil.js, line 81
Returns:
HTML
- Type:
- string
Example
const $ = require('TmplUtil');
let html = $.elems(
['script', { id: 'data1', type: 'application/json' }, data1],
['script', { id: 'data2', type: 'application/json' }, data2],
['script', { id: 'data3', type: 'application/json' }, data3],
);
icon(name, attrsopt) → string
Returns <svg> string of an icon.
Parameters:
| Name | Type | Description |
|---|---|---|
name
|
string | Icon name |
attrsopt
|
object | HTML attributes to add to resulting |
- Source:
- lib/TmplUtil.js, line 247
- See:
- https://github.com/feathericons/feather
Returns:
HTML
- Type:
- string
if(cond, then, replaceopt, oropt) → string
Ternary-like.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
cond
|
any | Condition |
|
then
|
string | String to return if |
|
replaceopt
|
string |
{*}
|
Substring in |
oropt
|
string | String to return if |
- Source:
- lib/TmplUtil.js, line 158
Returns:
- Type:
- string
Example
const $ = require('TmplUtil');
let name = 'Joji';
let greet = $.if(name, 'Hello, {*}.'); // "Hello, Joji."
iterate(over, fn, oropt) → string
Iterates over the given array or object.
Parameters:
| Name | Type | Description |
|---|---|---|
over
|
|
Array or object to iterate over |
fn
|
function | Callback for each iteration |
oropt
|
string | Fallback if |
- Source:
- lib/TmplUtil.js, line 193
Returns:
A string consists of all the return values of fn concatenated
- Type:
- string
lines(strs, linebreak) → string
Formats an array to a multiline string.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
strs
|
|
Array of strings |
|
linebreak
|
string |
\n
|
Linebreak to insert |
- Source:
- lib/TmplUtil.js, line 183
Returns:
- Type:
- string
link(str) → string
Converts URLs begin with http(s):// in the given string to <a> elements.
Parameters:
| Name | Type | Description |
|---|---|---|
str
|
string |
- Source:
- lib/TmplUtil.js, line 237
Returns:
Modified string
- Type:
- string
list(items) → string
Formats an array to a <ul> list.
If the array has only 1 item, omits <ul> and <li> tags
Parameters:
| Name | Type | Description |
|---|---|---|
items
|
string[] | Array of strings |
- Source:
- lib/TmplUtil.js, line 167
Returns:
HTML
- Type:
- string
paramAttrs(param) → object[]
Returns all attributes of the given param object.
Parameters:
| Name | Type | Description |
|---|---|---|
param
|
object | Param object |
- Source:
- lib/TmplUtil.js, line 259
Returns:
Attributes
- Type:
- object[]
tag(name, attrsopt) → string
Outputs an HTML tag with the given name and attributes.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
name
|
string | Tag name |
|
attrsopt
|
|
null
|
Attributes |
- Source:
- lib/TmplUtil.js, line 38
Returns:
HTML
- Type:
- string
Example
const $ = require('TmplUtil');
$.tag('link', {
type: 'text/css',
rel: 'stylesheet',
href: 'style.css'
});
wbr(str) → string
Inserts <wbr> tags between words, numbers, and symbols in the given string.
Parameters:
| Name | Type | Description |
|---|---|---|
str
|
string |
- Source:
- lib/TmplUtil.js, line 229
Returns:
Modified string
- Type:
- string