React 组件化思想受到越来越多开发者的关注,组件化思想帮助开发者将页面解耦成一个一个组件,代码更加模块化, 更易扩展。而目前流行的后端模板引擎如 ejs, swig, jade, art 共同的问题是:
针对以上痛点,笔者基于 React 造出了 noox 这样一个工具,专注于后端模板的解析,让模板解析更加简单,易用。
使用方法
安装
npm install noox
简单的 demo
模板代码
首先创建组件目录和增加模板文件
mkdir components && cd components
vi Head.jsx
Head.jsx 内容如下:
<head>
<title>{title}</title>
<meta name="description" content={props.description} />
<link rel="stylesheet" href="./css/style.css" rel="external nofollow" rel="external nofollow" />
</head>
Node.js Code
const noox = require('noox');
const nx = new noox(path.resolve(__dirname, './components'), {title: 'noox'});
let output = nx.render('Head', {description: 'hello, noox.'})
输出
<head>
<title>noox</title>
<meta name="description" content="hello, noox." />
<link rel="stylesheet" href="./css/style.css" rel="external nofollow" rel="external nofollow" />
</head>
原理
Noox 在 React 的 Jsx 的基础上,简化了组件引用和创建,假设创建一个目录结构如下:
components/
Header.jsx
Body.jsx
Layout.jsx
运行如下 nodejs 的代码:
nx = new noox(path.resolve(__dirname, './components'))
将会创建三个组件:
然后通过 nx.render 渲染
nx.render('Body', props)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。