In Nijor projects, we don't write code in HTML files ; we write code in *.nijor
files instead. Each *.nijor
file is a component in a Nijor Project.
A Nijor Component is a capsule of HTML, CSS and Javascript code in a single file in which the HTML, CSS and JS are scoped which allows developers to name their CSS classes and Javascript functions anyway we like without worrying about messing up with the scopes of other components.
A Nijor Component can be used as a custom HTML tag (with custom attributes) by other Nijor Component. It can also be used as a Page for routing.
<template>
<!-- Your HTML goes here -->
</template>
<style>
/* Your CSS goes Here */
</style>
<script>
// Your JS goes here
</script>
There are 3 types of script tag in a Nijor component. It is not neccessary to use all of them in a single project.
<template specs={name}>
<p>Hello {name} !</p>
</template>
<script>
// Code will get executed before the component Renders. It's code is in the global scope
// console.log(name); // Will result in error as this code gets executed before the component even renders.
// Variables defined here can be read inside the template tag using {variable} syntax
// Used for defining functions which get executed on events like on:click, on:customevent, onclick, etc
</script>
<script mid>
/* Very similar to the script tag above but with a slight difference; this script is scoped within the component itself.
console.log(name); // Won't result in error as this code gets executed after the component renders.
// Variables defined here can be read inside the template tag using {variable} syntax
</script>
<script defer>
// Code will get executed after the component Renders.
console.log(name); // Won't result in error as this code gets executed after the component renders.
// Variables defined here can't be read inside the template tag using {variable} syntax
</script>
specs is a special attribute available in the template tag . The specs attribute contains an object of all the custom attributes passed to the component from another component. _text_ is a property in the specs object which returns the innerHTML of the component when imported by another component.
<template specs={name}>
<p>Hello {name} !</p>
</template>
<script>
// Code will get executed before the component Renders. It's code is in the global scope
// console.log(name); // Will result in error as this code gets executed before the component even renders.
// Variables defined here can be read inside the template tag using {variable} syntax
// Used for defining functions which get executed on events like on:click, on:customevent, onclick, etc
</script>
<script mid>
/* Very similar to the script tag above but with a slight difference; this script is scoped within the component itself.
console.log(name); // Won't result in error as this code gets executed after the component renders.
// Variables defined here can be read inside the template tag using {variable} syntax
</script>
<script defer>
// Code will get executed after the component Renders.
console.log(name); // Won't result in error as this code gets executed after the component renders.
// Variables defined here can't be read inside the template tag using {variable} syntax
</script>
<template specs={attr1,attr2}>
Attribute1 : {attr1}<br>
Attribute2 : {attr2}<br>
</template>
<script>
console.log(attr1,attr2);
</script>
or
Inside the template tag, you can use the value of a JS variable of the same component inside any HTML tag by enclosing the name of the variable by curly brackets.
In Nijor, instead of calling events like onclick
, onload
, etc we use events like on:click
, on:load
, etc.
Let's assume that there is a component blogpost.nijor in the components/
directory. We can import it any Nijor file by the following syntax.