TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Simple dynamic templating in JS

3 点作者 jgalvez大约 7 年前
How do you guys deal with this scenario?<p><pre><code> const renderTemplate = (template, obj) =&gt; { const args = Object.keys(obj) const body = `return (\`${template}\`)` const renderFunc = new Function(...args.concat([body])) return renderFunc(...Object.values(obj)) } const dynamicallyLoadedTemplate = &#x27;${a} and ${b}&#x27; console.log( renderTemplate(dynamicallyLoadedTemplate, { &#x27;a&#x27;: &#x27;1&#x27;, &#x27;b&#x27;: 2 }) ) </code></pre> Is this elegant enough?

2 条评论

sotaan大约 7 年前
It&#x27;s how `vue-template-compiler` works to transform a template into render functions.<p>checkout here: <a href="https:&#x2F;&#x2F;github.com&#x2F;vuejs&#x2F;vue&#x2F;blob&#x2F;dev&#x2F;packages&#x2F;vue-template-compiler&#x2F;browser.js#L4512" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vuejs&#x2F;vue&#x2F;blob&#x2F;dev&#x2F;packages&#x2F;vue-template-...</a><p>also checkout last &#x27;state of Vue.js&#x27; on YT where the creator talks about it ;)
评论 #16957486 未加载
fiatjaf大约 7 年前
eval? You can&#x27;t say that&#x27;s elegant.
评论 #16957977 未加载
评论 #16956901 未加载