当前位置:首页 > JavaScript

js实现模板引擎

2026-01-30 22:07:43JavaScript

实现简单模板引擎

使用字符串替换方法实现基础模板引擎功能。定义一个模板字符串,通过占位符标记需要替换的部分,利用正则表达式匹配占位符并进行替换。

function simpleTemplate(template, data) {
  return template.replace(/\{\{(\w+)\}\}/g, (match, key) => {
    return data[key] || '';
  });
}

const template = 'Hello, {{name}}! Today is {{day}}.';
const data = { name: 'Alice', day: 'Monday' };
console.log(simpleTemplate(template, data)); // Hello, Alice! Today is Monday.

处理复杂表达式

扩展模板引擎以支持对象属性访问和简单表达式。通过更复杂的正则表达式匹配模板中的JavaScript表达式,并使用Function构造函数动态执行。

function advancedTemplate(template, data) {
  return template.replace(/\{\{(.+?)\}\}/g, (match, exp) => {
    try {
      return new Function('data', `with(data){return ${exp}}`)(data);
    } catch {
      return '';
    }
  });
}

const template = 'User: {{user.name}}, Age: {{user.age + 1}}';
const data = { user: { name: 'Bob', age: 25 } };
console.log(advancedTemplate(template, data)); // User: Bob, Age: 26

实现条件渲染

添加条件判断支持,使用特殊语法标记条件块。通过正则表达式分割模板字符串,解析条件逻辑并决定是否渲染特定部分。

function conditionalTemplate(template, data) {
  const ifRegex = /\{\{if\s+(.+?)\}\}(.+?)\{\{\/if\}\}/gs;
  return template.replace(ifRegex, (match, condition, content) => {
    const test = new Function('data', `with(data){return !!(${condition})}`)(data);
    return test ? content : '';
  });
}

const template = '{{if user.admin}}Admin Access{{/if}}';
const data1 = { user: { admin: true } };
const data2 = { user: { admin: false } };
console.log(conditionalTemplate(template, data1)); // Admin Access
console.log(conditionalTemplate(template, data2)); // (空字符串)

实现循环渲染

支持数组遍历功能,解析循环语法并重复渲染模板片段。处理数组数据时,为每次迭代创建新的上下文。

function loopTemplate(template, data) {
  const eachRegex = /\{\{each\s+(\w+)\s+as\s+(\w+)\}\}(.+?)\{\{\/each\}\}/gs;
  return template.replace(eachRegex, (match, arrayName, itemName, content) => {
    const array = new Function('data', `with(data){return ${arrayName}}`)(data) || [];
    return array.map(item => {
      return content.replace(/\{\{(\w+)\}\}/g, (m, key) => {
        return item[key] || '';
      });
    }).join('');
  });
}

const template = '{{each users as user}}<li>{{user.name}}</li>{{/each}}';
const data = { users: [{ name: 'Alice' }, { name: 'Bob' }] };
console.log(loopTemplate(template, data)); // <li>Alice</li><li>Bob</li>

缓存编译模板

优化性能,预先编译模板为可执行函数。将模板字符串转换为函数并缓存,减少重复解析的开销。

const templateCache = {};

function compileTemplate(template) {
  if (templateCache[template]) return templateCache[template];

  const code = `with(data){return \`${template
    .replace(/\{\{if\s+(.+?)\}\}/g, '`;$1?`')
    .replace(/\{\{\/if\}\}/g, '`:``')
    .replace(/\{\{each\s+(\w+)\s+as\s+(\w+)\}\}/g, '`;${$1}.map($2=>`')
    .replace(/\{\{\/each\}\}/g, '`).join("")+`')
    .replace(/\{\{(.+?)\}\}/g, '${$1}')
  }\`}`;

  templateCache[template] = new Function('data', code);
  return templateCache[template];
}

function cachedTemplate(template, data) {
  const render = compileTemplate(template);
  return render(data);
}

const template = '{{if show}}Hello{{/if}} {{each items as item}}{{item}}{{/each}}';
const data = { show: true, items: [1, 2, 3] };
console.log(cachedTemplate(template, data)); // Hello 123

安全考虑

避免使用with语句可能带来的安全问题,改用更安全的上下文处理方式。限制可访问的变量,防止原型污染。

js实现模板引擎

function safeTemplate(template, data) {
  const allowed = new Set(Object.keys(data));
  const sandbox = Object.create(null);

  Object.keys(data).forEach(key => {
    Object.defineProperty(sandbox, key, {
      enumerable: true,
      get: () => data[key]
    });
  });

  const code = `return \`${template
    .replace(/\{\{(.+?)\}\}/g, '${$1}')
  }\``;

  try {
    return new Function('sandbox', code).call(null, sandbox);
  } catch {
    return '';
  }
}

const template = 'Hello {{name}}! {{toString}}';
const data = { name: 'Charlie' };
console.log(safeTemplate(template, data)); // Hello Charlie! 

标签: 模板引擎
分享给朋友:

相关文章

div css网页制作模板

div css网页制作模板

基础HTML结构 创建HTML文件时需包含DOCTYPE声明、html、head和body标签。DOCTYPE声明确保浏览器以标准模式渲染页面,html标签是文档的根元素,head标签包含元数据和链接…

vue模板实现

vue模板实现

Vue 模板实现 Vue 模板是 Vue.js 的核心特性之一,用于声明式地将数据渲染到 DOM 中。以下是 Vue 模板的实现方式和常见用法。 基本语法 Vue 模板使用 HTML 的模板语法,可…

vue实现模板功能

vue实现模板功能

Vue 实现模板功能的方法 Vue.js 提供了多种方式来实现模板功能,包括使用插槽(Slots)、动态组件(Dynamic Components)以及渲染函数(Render Functions)。以…

css模板制作网页

css模板制作网页

CSS模板制作网页的方法 使用CSS模板可以快速构建美观且响应式的网页。以下是几种常见的方法和步骤: 使用预定义的CSS框架 Bootstrap、Tailwind CSS、Bulma等框架提供现成的…

用css制作模板

用css制作模板

使用CSS创建模板的方法 CSS模板可以通过多种方式实现,以下是几种常见的方法: 定义基础样式 在CSS文件中设置全局样式,包括字体、颜色、间距等。例如: body { font-family…

php实现模板

php实现模板

PHP 实现模板的方法 PHP 实现模板通常是为了将业务逻辑与展示层分离,提高代码的可维护性和可读性。以下是几种常见的 PHP 模板实现方法: 原生 PHP 模板 直接使用 PHP 原生语法作为模板…