als-layout
v7.0.1
Published
HTML layout constructor with dynamic meta, styles, and scripts
Maintainers
Readme
als-layout Documentation
als-layout is a JavaScript library designed to simplify and enhance the process of constructing and managing web page layouts. It provides a comprehensive API for modifying HTML documents dynamically, allowing developers to add, update, and manipulate various elements such as meta tags, styles, scripts, and more.
The als-layout library is versatile, suitable for:
- Building dynamic web pages that require frequent updates to their metadata, styles, or scripts.
- Creating templating systems where multiple page layouts share similar structures but differ in content or styling.
- Developing web applications that need to dynamically adjust their UI based on user interactions or data changes.
Installation and Import
Install via npm:
npm i als-layoutNode (CommonJS)
const Layout = require('als-layout')ESM (Module)
import Layout from 'als-layout'Browser (with bundle)
<script src="layout.js"></script>
<script>
const layout = new Layout()
</script>Change Log for V7
⚠ Breaking changes: not backward compatible.
Removed
uglify js and cssstatusmethodendmethodminifiedattributepropsToClone
Changed
- All code is merged into a single file
- Added ESM module build (
index.mjs) - Added browser bundle (
layout.js) that includesals-documentand exposes a globalLayout
Basic Usage
Initialization
const layout = new Layout();Adding Elements
const layout = new Layout()
.viewport() // default: width=device-width, initial-scale=1.0
.title('Test title') // sets <title> and meta[og:title]
.favicon('/favicon.png') // adds/updates favicon link
.keywords(['some', 'keyword']) // updates meta[name=keywords]
.image('/main-image.png') // sets og:image, twitter:image, twitter:card
.description('Cool site') // sets meta description tags
.url('/some', 'http://site.com') // sets canonical + og:url
.style('body {margin:0; background-color:whitesmoke;}') // appends CSS to style tag
.link('/styles.css') // adds stylesheet link if not already present
.script({src:'/app.js'}) // external script
.script({}, 'console.log("hello world")', false) // inline script in body
// Accessors
layout.body // getter for <body>
layout.head // getter for <head>
layout.html // getter for <html>
// Outputs
layout.rawHtml // raw HTML string
layout.clone // cloned Layout instanceCloning
Why Clone?
Cloning creates a complete, independent copy of a Layout instance. Useful when generating multiple pages from a base template.
Example
const newLayout = layout.clone;
newLayout.title('Cloned page');- Efficiency – avoids rebuilding from scratch
- Isolation – modifications to clones don’t affect the original
- Performance – fast even for larger layouts
Advanced Usage
const raw = /*html*/`<html></html>`
const host = 'http://example.com';
const layout = new Layout(raw, host).lang('fr');
console.log(layout.rawHtml);
// <!DOCTYPE html><html lang="fr"><head></head><body></body></html>
const homePage = layout.clone;
homePage.title('Home page');
homePage.body.innerHTML = `<h1>Home page</h1>`;
console.log(homePage.rawHtml);
const autoReload = layout.clone;
autoReload.script({}, 'setTimeout(() => window.location.reload(), 60000);', false);API
Constructor
new Layout(html?: string, host?: string)- html: optional HTML string
- host: base host for resolving relative URLs
Properties
layout.rawHtml– returns the current document as HTML stringlayout.clone– returns a cloned instance
Methods
lang(lang: string)– sets<html lang>title(title: string)– sets document<title>and Open Graph titledescription(description: string)– sets description meta tagsfavicon(href: string)– sets or updates faviconmeta(props: object)– sets or updates a<meta>tagkeywords(keywords: string[])– sets or appends tometa[name=keywords]viewport(viewport: string = 'width=device-width, initial-scale=1.0')– sets viewport metaimage(image: string)– sets OG and Twitter image meta tagsstyle(styles: string)– appends CSS styles into<style>url(url: string, host?: string)– sets canonical + og:urlscript(attrs: object = {}, innerHTML: string = '', head: boolean = true)– adds<script>taglink(href: string, attributes: object = { rel: "stylesheet", type: "text/css" })– adds stylesheet link
Notes
- Version 7 removed status, end, minified, and propsToClone.
- Use
clonefor creating independent layouts instead of extending clone properties. - Use
layout.jsin the browser for quick setup (exposes globalLayout).
