1.7.2 • Published 3 years ago

posthtml-part v1.7.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

NPM Deps Tests Coverage

npm i -D posthtml-part
const { readFileSync } = require('fs')

const posthtml = require('posthtml')
const part = require('posthtml-part')

const html = readFileSync('index.html')

posthtml([ part({ encoding: 'utf8' }) ])
    .process(html)
    .then((result) => console.log(result.html))

Options

root: Root folder path for part. Default ./

encoding: Default utf-8

posthtmlExpressionsOptions: Array to pass options posthtml-expression

Component options

locals: Object containing any local variables that you want to be accessible inside the part component

index.html

<html>
<head>
    <title>index.html</title>
</head>
<body>
    <part path="components/button.html" locals='{
        "text": "Button"
    }'></part>
</body>
</html>

components/button.html

<button class="button"><div class="button__text">{{ text }}</div></button>
const { readFileSync } = require('fs')

const posthtml = require('posthtml')
const part = require('posthtml-part')

const html = readFileSync('index.html')

posthtml([ part({ encoding: 'utf8' }) ])
    .process(html)
    .then((result) => console.log(result.html))
<html>
<head>
  <title>index.html</title>
</head>
<body>
  <button class="button">
    <div class="button__text">Button</div>
  </button>
</body>
</html>

You can also pass your locals directly on the \ content, just drop a JSON there. When doing it, all the "\n" chars will be removed from your data. If you need "\n" chars on your data, you can still use the "locals" attribute.

<part src="components/button.html">
  {
    "text": "Button"
  }
</part>

Note: Also supports multi nesting.

MIT License (MIT)

Copyright (c) 方坤

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.