j6t
A lightweight template library using es6 tagged template literals based on jQuery.
Usage
Install via bower
bower install j6t
Just put a <script> tag in your page after the tag you load jQuery library and your good to go and use j6t. You can download and use a local copy of j6t or use jsdelivr CDN like below.
After that you need to create at least one component and render it using j6t. In below sections I have described various features of j6t.
User Guide
Basic
base Component class
j6t resembles React in a sense that there is a base Component class from which you can derive your components.
Component
Component.render()
Each component should override a render() method it inherits from Component and return its own output in this method.
Component { return ` <div> Welcome to j6t! </div>` }
Using template literals in render()
There is no rendering language such as JSX in j6t. Instead, you return component's output as simple strings.
Component { return "<div>" + "Welcome to j6t" + "</div>" }
Using ES6 template literals and interpolations is a great way to specify componet's output.
Component { const name = 'j6t'; return ` <div> Welcome to . </div>` }
j6t.render()
Finally to render your component and display it in the page, you need to pass it to j6t.render(). This method has two arguments and works nearly in the same way as ReactDOM.render(). The first argument is a component. The second is a DOM node to which the component will be rendered to.
Component { const name = 'j6t' return ` <div> Wecome to . </div>` } const app = ; j6t
jsfiddle: https://jsfiddle.net/omrani/znksgp6v/50/
props and constructor
j6t's components have a props property (similar to React with some restrictions). You can pass your properties to your component's constructor as an object when instantiating from your component. Pay attention that you need to specify an explicit constructor for your component and call super() in it.
Component { superprops } { const name = thisprops; return ` <div> Wecome to . </div>` } const app = name: 'j6t' ;
jsfiddle: https://jsfiddle.net/omrani/znksgp6v/6/
Component.parse() and HTMLEncode by default
j6t's Component comes with an intelligent parse() method, and it is then when the interesting part begins.
Instead of returning a template literal, you can pass your template literal to parse() method and return its result as your return value.
Component ... { ... return thisparse` <div> Welcome to . </div>` }
jsfiddle: https://jsfiddle.net/omrani/znksgp6v/9/
Component.parse() is a tag function. It html encodes interpolated expressions by default. This prevents XSS injection attacks which normal template literals potentially have.
So, name variable will be html encoded in the following example.
Component ... { ... return thisparse` <div> </div>` } const app = name: '<script>alert("XSS succeeded!")</script>j6t'; j6t
Writing raw HTML values
If you want your interpolated expression be written as raw HTML, prepend an excalamtion mark before $ sign in your template literal.
Component { return thisparse` <div> Welcome to ! </div>` }
jsfiddle: https://jsfiddle.net/omrani/znksgp6v/15/
HTML Attributes
ES6 template literals really eases using HTML attributes.
Component ... { const type size text = thisprops; return ` <button id="btn1" class="btn btn-" style="width: "> </button>` } const btn = type: 'primary' size: '60px' text: 'Click me'
But as we stated earlier, this pure template literals are prone to XSS. It is better to use parse() to return your content in your render() method.
j6t introduces also another shorter syntax for attributes. If we prepend the name of the attribute before an interpolated expression, j6t adds the extra quotation marks before and after the value. Here is an example:
Component ... { const type value = thisprops; return thisparse`<input type value />` }
jsfiddle: https://jsfiddle.net/omrani/znksgp6v/19/
The above code is equivalent to the following code.
Component ... { const type value = thisprops; return thisparse`<input type="" value="" />` }
CSS, Styles and HTML class attribute
We can specify styles for an element using pure style="..." or style${'...'}. However, style${...} has a major benefit. We can specify a javascript object for style${...}. j6t converts the object to a css style string.
Component ... { return thisparse` <div style></div> ` }
jsfiddle: https://jsfiddle.net/omrani/znksgp6v/20/
The final style attribute for the above <div> would be as below:
<div style="background-color: red; width: 100px; height: 100px"> ...
Similar to style${...}, we can also use class${...} to specify CSS class(s) of a tag.
Component ... { return thisparse` <button class>Submit</button> ` }
j6t special HTML attributes
Component.parse() supports special HTML attributes.
id${...}, #${...}
j6t has a dynamic DOM id generator. In order to generate dynamic ids for your tags, use id${}. You need to specify an integer as an index for the id being generated.
Component ... { ... return thisparse` <button id class="btn btn-" style="width: "> </button>` }
Later in your component you can refer to the generated id using #${...} and specify its index.
Component ... { const type size text = thisprops; return thisparse` <button id class="btn btn-" style="width: "> </button> <script> const btn = document.getElementById('#'); btn.onclick = function() { alert('Welcome to j6t'); } </script> ` }
Dynamic ids is important when creating reusable components. If you specify a fixed or static id for your component its id might collide with another element which has the same id.
Component's wrapper and its id
While you can specify the content of a component in render() method freely, it is necessary for a component to have a main or root container in order to refresh and work correctly. To this need, j6t checks if the user has specified a root container for the component. This happens at the end of rendering and producing the component's output. Each component has a unique id which is generated for it automatically. In order to specify the main or root container for a component, we need to use id${'#'} or id#{'.'}. Here is an example:
Component ... { ... return thisparse` <div id> <button id class="btn btn-" style="width: "> </button> </div>` }
Here, the <div> is the root container for Button component. It is not necessary to wrap your tags inside a <div> though. If you have only a single tag, you can specify id${'#'} for that tag and make it the root container.
Component ... { ... return thisparse` <button id class="btn btn-" style="width: "> </button>` }
If j6t sees that a component doesn't have a root container, it adds a defualt <div id${'#'}> to the conent returned by render().
Access current component's id
In order to access current component's id we can use #${'.'}.
Component ... { ... return thisparse` <div id> <button id class="btn btn-" style="width: "> </button> </div> <style # button { width: 80px } </style> ` }
Here the CSS rule is only applied to the button inside our component, not any in the page.
Specify manual id for a component
When using id${...} we can specify a manual id for current component.
Component ... { ... return thisparse` <div id> ... </div>` }
It doesn't matter to start the id with # or not. j6t omits # from an id if it sees the manual id starts with #.
It is not recommended though to specify a static id for your component, because multiple instances of the component will have the same id which is an incorrect thing in HTML.
Events
If you specify an id using id${...}, you can then specify event handlers using xxx${callback} where xxx is the event name and callback is your event handler function. This simplifies defining events. Events names are case-sensitive and should be lowercased always.
Component ... { ; } { ... return thisparse` <button id onclick> </button>` }
jsfiddle: https://jsfiddle.net/omrani/znksgp6v/52/
Here onclick is assigned to the recent node whose id is specified using id${}. Another example can clarify what elements will be chosen as targets of event.
Component ... { console } { console } { const id title = thisprops; return thisparse` <button id onclick>Up</button> <button id onclick>Down</button> <span></span> ` }
Here the first onclick will be attached for id${0} and the second goes for id${1}.
j6t knows main events in HTML specification, like click, dbclick, mousedown, mouseup, etc. In case we have a custom event, we can specify it by prepending a # sign before the name of the event.
Component ... { ... } { ... return thisparse` <button #onPageResized> </button>` }
Specify events using bind${...}
Sometimes you might want to assign the same event handler for a group of items using a css selector, not a single element. You can specify this event handler using bind${...}. Here is an example.
Component ... { const text = text; ; } { const colors = 'red' 'green' 'blue' 'yellow' 'orange' 'black' 'white' return thisparse` <ul id> ! </ul> bind ` }
jsfiddle: https://jsfiddle.net/omrani/znksgp6v/53/
Here the click event is specified for all <li> elements inside <ul>. There is a single function to handle the event. Note that, we didn't know id of <ul> since its automatically generated, but we are still able to refer to it in our css selector to target the <li> elements. Here, the click events is assigned only to the <li> element inside current component's <ul> not any <li> inside any <ul>.
The structure of the object we pass to bind${ ...} is as follows:
{
event: string, // event name e.g. click, mousedown, keydown, etc.
target: string, // css selector
handler: fnEventHandler // function
}
To refer to the root container of current component in our css selector, we can use #.
bind${{ event: 'click', target: 'ul## li', handler: this.liClicked }}
We can even specify nothing.
bind${{ event: 'click', target: 'ul# li', handler: this.liClicked }}
HTML Tags
In addition to the normal method of using plain tags in the content we return in the render() method, we can make use of j6t HTML tags to have a more concise content. We can create an HTML tag by appending its name to the $ sign and specify its attributes using an interpolated expression. Here is an example:
Component ... { const title description = thisprops; return thisparse` h1 p a ` }
Here, the content contains an <h1>, <p> and an <a> tag.
If the tag we intend to create is a container tag such as <h1>, <p>, <div>, etc., we can specify content of the tag using 'text' or 'html' property in the object we specify in the interpolated expression. The value for the 'text' property will be HTML encoded, while the value of 'html' property will not be encoded and is placed in the content directly.
Nested Components
The benefit of the component-based design is breaking down the UI into smaller reusable parts that are eaiser to maintain. Similar to the way we can create tags using $ sign, we can instantiate and render another component inside a component. Here is an example:
Component { const type message = thisprops; return `<div class="alert alert-"></div>` }Component { const type message = thisprops; return thisparse` Alert ` }
Component's Children
By default when we use tag${...} notation to render tags or create sub-components inside a component using component${...} syntax, j6t adds the created tags or component instance into a collection named 'children' to the parent component. Using the children proeprty in a component we can access its sub-components or tags.
Whereas components derive from a base Component class, all tag objects derive from a BaseTag class. Similarly all attribute objects derive from a BaseAttribute class.
Refreshing components
Components don't have something like state in React. User is allowed to manipulate any components' props and attributes. However, this doesn't update that component's view. In order to update or re-render a component, we need to call its refresh() method manually. Here is an example:
Component { const data = thisprops; return thisparse` <table class="table table-striped" > ! </table>` }Component { const grid = thischildren0; gridpropsdata = name: 'John' job: 'Developer' name: 'David' job: 'Manager' name: 'Mark' job: 'Tester' name: 'Simon' job: 'DBA' ; grid } { const data = ; return thisparse` <button id onclick>Load Grid</button> Grid` }
Forcing tags and attributes
When Component.parse() method sees xxx${...} in the string content, it uses a specific prioritarization on how to render xxx${...}. the details of this process is a little lengthy and could be out of the reader's interest. But here is the short explanation.
j6t distinguishes HTML tags and attributes by default. If xxx is an HTML tag, j6t renders xxx${...} as tag. If xxx is an html attribute, j6t renders it as an attribute. If there is a class named xxx in the current context which has a render() method, j6t instantiates from the xxx class and calls its render() method. And if xxx is a function, j6t calls the function.
It is not far to happen though that j6t acts incorrectly. To force something to be rendered as a tag we can prepend its name with an extra $ sign, like $mytag${...} and to force something to be rendered as an attribute we can prepend the name with ^ like ^myattr${...}.
Iterating Arrays
j6t's Component.parse() method distinguishes arrays in interpolated expressions and preserve them in a local variable. After that we can specify a function in the next expression. Component.parse() automatically iterates over the recent array and passes current item to our function. It is easy to produce HTML lists. Here is an example:
Component { return thisparse` <table class="table table-striped"> <thead> <th>Row</th> <th>Name</th> <th>Occupation</th> </thead> <tbody > ! </tbody> </table>` } const app = data: Name: 'John' Occupation: 'Developer' Name: 'Mark' Occupation: 'Tester' Name: 'David' Occupation: 'Manager' Name: 'Simon' Occupation: 'DBA' j6t
It doesn't matter where you mention the ${array} in the template literal. Component.parse() reserves the array and doesn't produce anything for this expression. But it is important to specify the function for iterating the array in the exact next expression. Also, we need an exclamation mark before the ${fnIterate} if our content is HTML, since as stated earlier, Component.parse() HTML encodes expressions bu default.
jsfiddle: https://jsfiddle.net/omrani/znksgp6v/51/
Commands
j6t provides a mechanism for components to support user-defined command execution. The syntaxt is to use an asterisk or star character before the tag or name that is attached to the $ sign before an interpolated expression. Here is an example:
Component ... { const name = 'mark Twain' return thisparse` <b>lower(name)</b>: *lower <br/> <!--result: mark twain --> <b>upper(name)</b>: *upper <br/> <!--result: MARK TWAIN --> <b>reverse(name)</b>: *reverse <br/> <!--result: niawt kram --> <b>capitalize(name)</b>: *capitalize <br/> <!--result: Mark Twain --> ` }
Each component has an exec() method that is provided for command execution.
exec(command, value) {
...
}
Here 'command' is the tag or name before $ sign. 'value' is the interpolated expression passed to the ${...}.
j6t internally supports various commands that is listed below:
command | description | example | result |
---|---|---|---|
lower | toLowerCase() | *lower${name} | mark twain |
upper | toUpperCase() | *upper${name} | MARK TWAIN |
capitalize | capitalize | *capitalize${name} | Mark Twain |
urlencode | url encode | *urlencode${name} | mark%20Twain |
urldecode | url decode | *urldecode${'mark%20twain'} | mark twain |
htmldecode | html decode | *htmldecode${'mark</>'} | mark |
reverse | reverse string | *reverse${name} | niawT kram |
trim | trim string | *trim${" mark twain "} | "mark twain" |
b | decimal to binary | *b${20} | 10100 |
x | decimal to hex | *x${20} | 14 |
o | decimal to octal | *o${20} | 24 |
bd | binary to decimal | *bd${1101} | 13 |
bx | binary to hex | *bx${1101} | d |
bo | binary to octal | *bo${1101} | 15 |
od | octal to decimal | *od${24} | 20 |
ox | octal to hex | *ox${24} | 14 |
ob | octal to binary | *ob${24} | 10100 |
xd | hex to decimal | *xd${14} | 20 |
xo | hex to octal | *xo${14} | 24 |
xb | hex to binary | *xb${14} | 10100 |
It is possible though to extend the internal commands or even revoke them completely. In order to do this, we need to override exec() method. If we want to extend internal commands, we should check given command first. If it is among those we want to extend or support, we should prepare the result, otherwise we should call super.exec(command, value). Here is an exmplae:
Component { }
Dynamic Tag/Class/Component Invocation
In addition to the normal way of instantiating from tags/classes/sub-components using xxx${...} syntax, j6t provides a flexible and dynamic method of instantiation by which we can instantiate from a tag/class/component whose name is stored in a variable. The syntax is as follows:
@${xxx}${value}
Here is an example:
Component { const tag = 'button' return thisparse` @ ` }
It is important to stick the interpolated expressions together and there shouldn't be anything even a single space between them.