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

@yizhi/render

v1.0.1

Published

使用此模板引擎,可以方便的将数据渲染到网页上。

Downloads

119

Readme

HTML模板引擎

使用此模板引擎,可以方便的将数据渲染到网页上。

安装

npm install @yizhi/render

使用

使用render函数,传入模板字符串和数据对象,即可渲染出网页内容。

import { render } from '@yizhi/render';
const html = render("/path/to/template.html", {name:"Join", age:20, love:["eat", "sleep", "code"] });
console.log(html);

内容输出

使用{{ 表达式 }}语法,可以将表达式的值输出到网页。

<h1>{{ data.name }}</h1>

经过渲染后,输出的HTML内容为:

<h1>Join</h1>

条件判断

在标签上使用:if:else来进行条件判断,如果else有值,则表示elseif。

<div :if="data.age < 3">你还是个宝宝</div>
<div :else="data.age < 18">你还未成年</div>
<div :else>你已经长大了</div>

经过渲染后,输出的HTML内容为:

<div>你已经长大了</div>

循环

在标签上使用:for来进行循环,支持两种循环方式:

  • :for="数据项 in 数据"
  • :for="(索引, 数据项) in 数据"

其中,数据项可以是数组或对象。

示例:

<ul>
  <li :for="item in data.love">{{ item }}</li>
</ul>

<ul>
	<li :for="(index, item) in data.love">{{ index }} - {{ item }}</li>
</ul>

<ul>
	<li :for="(key, value) in data">{{ key }} : {{ value }}</li>
</ul>

经过渲染后,输出的HTML内容为:

<ul>
  <li>eat</li>
  <li>sleep</li>
  <li>code</li>
</ul>

<ul>
	<li>0 - eat</li>
	<li>1 - sleep</li>
	<li>2 - code</li>
</ul>

<ul>
	<li>name : Join</li>
	<li>age : 20</li>
	<li>love : eat,sleep,code</li>
</ul>

模板定义

可以使用template来定义模板,通过name属性来指定模板名称。

<template name="my-template">
  <h2>{{ data.name }}</h2>
  <ul>
    <li :for="item in data.love">{{ item }}</li>
  </ul>
</template>

通过上面方式,我们就定义了一个名为my-template的模板。

模板使用

通过use标签,可以将模板内容渲染到网页上,data属性指定数据对象。

<div>
  <h1>模板使用</h1>
  <use name="my-template" data="data"/>
</div>

如果模板文件在其他位置,还可以通过from属性指定模板文件路径。

<div>
  <h1>模板使用</h1>
  <use from="/path/to/my-template.html" name="my-template" data="data"/>
</div>

经过渲染后,输出的HTML内容为:

<div>
  <h1>模板使用</h1>
  <h2>Join</h2>
  <ul>
    <li>eat</li>
    <li>sleep</li>
    <li>code</li>
  </ul>
</div>