npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

posthtml-extend

v0.6.5

Published

Templates extending (Jade-like)

Downloads

48,400

Readme

posthtml-extend npm version Build Status

PostHTML plugin that allows a template to extend (inherit) another templates (Jade-like).

Usage

Let's say we have a base template:

base.html

<html>
    <head>
        <title><block name="title"> — Github</block></title>
    </head>

    <body>
        <div class="content">
           <block name="content"></block>
        </div>
        <footer>
            <block name="footer">footer content</block>
        </footer>
    </body>
</html>

Now we can inherit this template. All defined blocks inside <extends> will replace the blocks with the same name in the parent template. If the block is not defined inside <extends> its content in the parent template remains the same.

In the example the blocks title and content will be replaced and the block footer will remain unchanged:

var posthtml = require('posthtml');
var html = `<extends src="base.html">
      <block name="title">How to use posthtml-extend</block>
      <block name="content">Read the documentation</block>
  </extends>`;

posthtml([require('posthtml-extend')({
    encoding: 'utf8', // Parent template encoding (default: 'utf8')
    root: './' // Path to parent template directory (default: './')
})]).process(html).then(function (result) {
    console.log(result.html);
});

The final HTML will be:

<html>
    <head>
        <title>How to use posthtml-extend</title>
    </head>

    <body>
        <div class="content">Read the documentation</div>
        <footer>footer content</footer>
    </body>
</html>

Append/prepend

It's also possible to append and prepend block's content:

var posthtml = require('posthtml');
var html = `<extends src="base.html">
      <block name="title" type="prepend">How to use posthtml-extend</block>
      <block name="content">Read the documentation</block>
      <block name="footer" type="append">— 2016</block>
  </extends>`;

posthtml([require('posthtml-extend')()]).process(html).then(function (result) {
    console.log(result.html);
});

The final HTML will be:

<html>
    <head>
        <title>How to use posthtml-extend — Github</title>
    </head>
    <body>
        <div class="content">Read the documentation</div>
        <footer>footer content — 2016</footer>
    </body>
</html>

Pass data to layouts

In addition to normal html content you can also pass data as a json object to your layout file. To do this add a locals attribute to your <extend> and pass a json string.

Like this:

    var posthtml = require('posthtml');
    var html = `<extends src="base.html" locals='{"bodyclass": "home"}'>
        <block name="content">Read the documentation</block>
    </extends>`;
   
    posthtml([require('posthtml-extend')()]).process(html).then(function (result) {
        console.log(result.html);
    });

Now you can easily access your data inside of your base.html:

<html>
    <head>
        <title>How to use posthtml-extend — Github</title>
    </head>

    <body class="{{ bodyclass }}">
        <div class="content">
           <block name="content"></block>
        </div>
    </body>
</html>

The final HTML will be:

<html>
    <head>
        <title>How to use posthtml-extend — Github</title>
    </head>
    <body class="home">
        <div class="content">Read the documentation</div>
    </body>
</html>

This behaviour can be customized with the option expressions.

Options

encoding

Type: string
Default: utf8

The encoding of the parent template.

plugins

Type: array
Default: []

You can include other PostHTML plugins in your templates. Here is an example of using posthtml-expressions, which allows to use variables and conditions:

var posthtml = require('posthtml');
var options = {
    plugins: [
        require('posthtml-expressions')({ locals: { foo: 'bar'} })
    ]
};
var html = `<extends src="base.html">
      <if condition="foo === 'bar'">
        <block name="content">content value foo equal bar</block>
      </if>

      <if condition="foo !== 'bar'">
          <block name="content"> value foo not equal bar</block>
      </if>
  </extends>`;

posthtml([require('posthtml-extend')(options)]).process(html).then(function (result) {
    console.log(result.html);
});

The final HTML will be:

<html>
    <head>
        <title>How to use posthtml-extend — Github</title>
    </head>
    <body>
        <div class="content">content value foo equal bar</div>
        <footer>footer content — 2016</footer>
    </body>
</html>

root

Type: string
Default: ./

The path to the root template directory.

strict

Type: boolean
Default: true

Whether the plugin should disallow undeclared block names.

By default, the plugin raises an exception if an undeclared block name is encountered. This can be useful for troubleshooting (i.e. detecting typos in block names), but there are cases where "forward declaring" a block name as an extension point for downstream templates is useful, so this restriction can be lifted by setting the strict option to a false value:

const extend = require('posthtml-extend');

const root = './src/html';
const options = { root, strict: false };

posthtml([extend(options)]).then(result => console.log(result.html));

slot/fill

Type: string
Default: block

Tag names used to match a content block with a block for inserting content.

base.html

<html>
    <head>
        <title><slot name="title"> — Github</slot></title>
    </head>

    <body>
        <div class="content">
           <slot name="content"></slot>
        </div>
        <footer>
            <slot name="footer">footer content</slot>
        </footer>
    </body>
</html>
var posthtml = require('posthtml');
var html = `<extends src="base.html">
      <fill name="title">How to use posthtml-extend</fill>
      <fill name="content">Read the documentation</fill>
  </extends>`;

posthtml([require('posthtml-extend')({
    slotTagName: 'slot',
    fillTagName: 'fill'
})]).process(html).then(result => console.log(result.html));

The final HTML will be:

<html>
    <head>
        <title>How to use posthtml-extend</title>
    </head>

    <body>
        <div class="content">Read the documentation</div>
        <footer>footer content</footer>
    </body>
</html>

tagName

Type: string
Default: extends

The tag name to use when extending.

var posthtml = require('posthtml');
var html = `<layout src="base.html">
    <block name="title">Using a custom tag name</block>
    <block name="content">Read the documentation</block>
</layout>`;

posthtml([require('posthtml-extend')({
    tagName: 'layout',
})]).process(html).then(result => console.log(result.html));

The final HTML will be:

<html>
    <head>
        <title>Using a custom tag name</title>
    </head>

    <body>
        <div class="content">Read the documentation</div>
        <footer>footer content</footer>
    </body>
</html>

expressions

Type: object
Default: {}

This option accepts an object to configure posthtml-expressions. You can pre-set locals or customize the delimiters for example.

Head over to the full documentation for information on every available option.