0.5.2 • Published 2 years ago

slam-js v0.5.2

Weekly downloads
51
License
MIT
Repository
github
Last release
2 years ago

Slam-js

A Javascript library for generating static HTML on the client or server. Slam-js is meant to be a complete replacement to HTML templating engines such as Pug, Handlebars, or EJS.

Features

  • Easy templating
  • Component friendly
  • No need to learn a new language/syntax
  • Accurate type declarations
  • Good performance and speed
  • SVG tag support.

Installation

If using on the server:

npm install --save-dev slam-js

If using on the client or as part of a library:

npm install slam-js

Code Example

The function Document in this example returns a string constructed based on the tag functions used.

import { html, head, title, meta, link, body, h1, p } from "slam-js";

export const Document = () =>
  html({ lang: "en" },
    head(
      title("Slam-js"),
      meta({ name: "viewport", content: "width=device-width initial-scale=1, minimum-scale=1" }),
      link({ rel: "icon", href: "assets/favicon.ico" })
    ),
    body(
			h1("Slam-js"), 
			p("A Javascript library for generating static HTML on the server side.")
		)
  );

API

Each element function has the same parameters, with the exception of void elements which won't accept children (e.g. br, meta, link, etc.):

All Slam-js functions return a string with the tag, its attributes, and children:

import { div, p } from "slam-js";

export const Card = () => 
  div({ class: "card" }, 
		p("Slam-js")
	);

console.log(Card());

//Console logs '<div class="card"><p>Slam-js</p></div>'

Examples & Usage

Components

Reusable components can easily be created and then imported into an index file:

//Head.js
import { head, title, meta, link } from "slam-js";

export const Head = () =>
	head(
		title("Slam-js"),
		meta({ name: "viewport", content: "width=device-width initial-scale=1, minimum-scale=1" }),
		link({ rel: "icon", href: "assets/favicon.ico" })
	);
//A template for an "index" page:
import { html, body } from "slam-js";
import { Head } from "./Head.js";

export const indexPage = () => 
  html(
		Head(), 
		body("Index")
	);
//A template for an "about" page:
import { html, body } from "slam-js";
import { Head } from "./Head.js";

export const aboutPage = () => 
	html(
		Head(), 
		body("About")
	);

Components with Variables

Just make sure to return a string.

//Card.js

import { div, img, h4, p } from "slam-js";

export const Card = (imgSrc, imgAlt, header, description) => 
  div({ class: "card" }, 
		img({ src: imgSrc, alt: imgAlt }), 
		h4(header), 
		p(description)
	);
//index.js

import { html, body } from "slam-js";
import { Card } from "slam-js";

const document = () => 
  html(
    body(
      Card("./assets/dog.jpg", "A cute brown dog", "Sparky", "Sparky likes to play fetch an run around"),
      Card("./assets/cat.jpg", "An ugly gray cat", "Sleezy", "Sleezy likes to tear up furniture."),
      Card("./assets/lizard.jpg", "A plain green lizard.", "Plainola", "Plainola just hangs around.")
    )
  );

Inlining SVGs

SVGs can be constructed manually, or they can be easily inlined:

//hamburgerIcon.js

export function HamburgerIcon() {
  return `
    <svg viewBox="0 0 100 80">
      <rect width="100" height="20" rx="8"></rect>
      <rect y="30" width="100" height="20" rx="8"></rect>
      <rect y="60" width="100" height="20" rx="8"></rect>
    </svg>
  `;
}

FAQs

What about the reserved words var and switch?

These functions have an underscore after them:

import { var_, switch_ } from "slam-js";

What about inline styles?

The style attribute takes an object of attribute/value pairs:

import { div } from "slam-js";
export const WideRedBox = () => div({ style: { backgroundColor: "red", width: "100%" } });

How do I integrate with Webpack?

Export the template, and use it with HtmlWebpackPlugin.

//index.js

import { html, body } from "slam-js";

const Document = () => 
	html(
		body("Slam-js")
	);

export default Document;
//webpack.js
{
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.js",
      filename: index.html,
      chunks: [name],
    }),
  ];
}
0.5.0

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.8

3 years ago

0.3.7

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.4.4

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.3.18

3 years ago

0.3.9

3 years ago

0.3.17

3 years ago

0.3.16

3 years ago

0.3.15

3 years ago

0.3.14

3 years ago

0.3.13

3 years ago

0.3.12

3 years ago

0.3.11

3 years ago

0.3.10

3 years ago

0.3.0

3 years ago

0.2.52

3 years ago

0.2.51

3 years ago

0.2.50

3 years ago

0.2.54

3 years ago

0.2.53

3 years ago

0.2.49

3 years ago

0.2.48

3 years ago

0.2.47

3 years ago

0.2.46

3 years ago

0.2.45

3 years ago

0.2.44

3 years ago

0.2.43

3 years ago

0.2.42

3 years ago

0.2.41

3 years ago

0.2.39

3 years ago

0.2.38

3 years ago

0.2.37

3 years ago

0.2.36

3 years ago

0.2.30

3 years ago

0.2.35

3 years ago

0.2.34

3 years ago

0.2.33

3 years ago

0.2.32

3 years ago

0.2.31

3 years ago

0.2.29

3 years ago

0.2.28

3 years ago

0.2.27

3 years ago

0.2.26

3 years ago

0.2.25

3 years ago

0.2.24

3 years ago

0.2.23

3 years ago

0.2.22

3 years ago

0.2.20

3 years ago

0.2.19

3 years ago

0.2.18

3 years ago

0.2.17

3 years ago

0.2.16

3 years ago

0.2.15

3 years ago

0.2.14

3 years ago

0.2.13

3 years ago

0.2.12

3 years ago

0.2.11

3 years ago

0.2.10

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.5

3 years ago

0.2.1

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.4

3 years ago

0.2.0

3 years ago

0.1.17

3 years ago

0.1.16

3 years ago

0.1.15

3 years ago

0.1.14

3 years ago

0.1.13

3 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.12

3 years ago

0.1.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.9

3 years ago

0.1.3

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago