Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day385:实现一个简易的模板引擎?(阿里) #388

Open
qappleh opened this issue Aug 26, 2021 · 1 comment
Open

Day385:实现一个简易的模板引擎?(阿里) #388

qappleh opened this issue Aug 26, 2021 · 1 comment

Comments

@qappleh
Copy link
Owner

qappleh commented Aug 26, 2021

示例:

const template = '嗨,{{ info.name.value }}您好,今天是星期 {{ day.value }}';

const data = {
  info: {
    name: {
      value: '张三'
    }
  },
  day: {
    value: '三'
  }
};

render(template, data); // 嗨,张三您好,今天是星期三
@btea
Copy link

btea commented Aug 30, 2021

function render(template, data) {
    return template.replace(/\{\{([\s\S]+?)\}\}/g, function(match, $1) {
        var val = $1.trim();
        return eval('data.' + val)
    })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants