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

老雷 #48

Open
leizongmin opened this issue Apr 11, 2014 · 5 comments
Open

老雷 #48

leizongmin opened this issue Apr 11, 2014 · 5 comments

Comments

@leizongmin
Copy link

Github: @leizongmin
Topic: 异步的模板引擎
Weibo: @雷宗民

介绍一个可以在模板中调用异步函数的模板引擎 tinyliquid 和简单的应用


比如,在PHP中,如果要在模板中输出某个用户ID的昵称,可能会这样写:

昵称:<?php echo get_user_display_name(user_id) ?>

如果是在Node.js中,则需要在渲染模板之前查询出来:

get_user_display_name(user_id, function (err, name) {
  res.locals.display_name = name;
  res.render('template');
});

然后才能才模板中使用:

<%= display_name %>

某些可以允许用户自己修改模板的场合,只能把所有可能用到的数据查询出来,然后再渲染。

而使用 tinyliquid 模板引擎时,则可以在模板引擎中注册一个异步函数来获取数据, 仅当模板中用到时再查询数据,相当灵活

setAsyncFilter('get_user_display_name', get_user_display_name);

然后直接在模板中调用:

昵称:{{ user_id | get_user_display_name }}
@JacksonTian
Copy link
Contributor

为嘛关掉呢?

@leizongmin
Copy link
Author

后来仔细想了想,好像也没啥高上大的内容

@JacksonTian
Copy link
Contributor

可以作为一个light topic. 5分钟演讲。

在 2014年4月15日,下午4:22,雷宗民 [email protected] 写道:

后来仔细想了想,好像也没啥高上大的内容


Reply to this email directly or view it on GitHub.

@leizongmin leizongmin reopened this Apr 15, 2014
@clonn
Copy link

clonn commented Apr 15, 2014

reopen 的好啊,真想聽聽 @leizongmin 的 lightining talk.

@xiaojue
Copy link

xiaojue commented Apr 17, 2014

支持

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

No branches or pull requests

4 participants