我可以动态应用 Dart 的字符串插值吗? [英] Can I apply Dart's string interpolation dynamically?

查看:25
本文介绍了我可以动态应用 Dart 的字符串插值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(来自 Tristan McNab 的 Dart 讨论邮件列表)

我正在尝试构建服务器端 MVC 框架并基于模板等生成视图,我想知道是否可以动态应用 Dart 的字符串插值.例如,这将是我的视图模板:

<头><元字符集=utf-8"><title>${ViewData["Title>]}</title><link href="/Content/css/site.css";rel=样式表"/><身体><h1>${ViewData["Title>]}</h1><div id="容器"><p>你好世界!</p>

我想使用以下方法应用 ViewData 变量:

static String applyViewData(String html, Map ViewData) {//在这里应用插值}

目前这可能吗?我对 API 的搜索表明它不是.

解决方案

(由 Bill Hesse 发布)

通过将字符串文字包装在一个函数中,该函数将上下文作为一个参数,你可以有一个 Function : context -> String 你可以传递而不是字符串.如果你需要使用一些字符串操作,比如 concat,在这些对象上,你可以实现这些封装这种类型的类上的操作(提升"它们).这似乎是一种将字符串文字合二为一的简单方法放置,并提供您想要插入的数据.

字符串插值总是动态发生的,每次文字使用,并且数据可以很容易地从参数到函数而不是从词汇上下文.

例如:

Function MyTemplate() {返回(上下文上下文){return "<table><tr><td class=${context.leftColumnClass}>Red Sox</td><td>${context.data}</td></tr><;/表>";}}

...

var templateHere = MyTemplate();

...

var output = templateHere(context);

你也可以跳过一个间接层,直接创建

String FillMyTemplate(Context context) =>'''<html><head><title>$context.title</title></head>''';

并在需要模板的地方使用 FillMyTemplate.

(from the Dart discussion mailing list by Tristan McNab)

I'm trying to build a server-side MVC framework and generating views based on templates and whatnot, and I was wondering if I could apply Dart's string interpolation dynamically. For example, this would be my view template:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>${ViewData["Title"]}</title>
    <link href="/Content/css/site.css" rel="stylesheet" />
  </head>
  <body>
    <h1>${ViewData["Title"]}</h1>
    <div id="container">
      <p>Hello world!</p>
    </div>
  </body>
</html>

And I'd like to apply the ViewData variable using:

static String applyViewData(String html, Map ViewData) {
    // apply interpolation here
}

Is this at all possible at the moment? My searching for the APIs indicates that it isn't.

解决方案

(posted by Bill Hesse)

By wrapping the string literal in a function that takes the context as a parameter, you can have a Function : context -> String that you can pass around instead of a String. If you need to use some String operations, like concat, on these objects, you can implement these operations on a class encapsulating this type ("lifting" them). This seems like a straightforward way to give the string literal in one place, and give the data you want to interpolate in another.

String interpolation always happens dynamically, each time the literal is used, and the data can easily come from a parameter to a function rather than from the lexical context.

For example:

Function MyTemplate() {
   return (Context context) {
     return "<table><tr><td class=${context.leftColumnClass}>Red Sox</td><td>${context.data}</td></tr></table>";
   }
}

...

var templateHere = MyTemplate();

...

var output = templateHere(context);

You could also skip a level of indirection and just create

String FillMyTemplate(Context context) => '''
    <html><head><title>$context.title</title></head>
''';

and use FillMyTemplate where you need the template.

这篇关于我可以动态应用 Dart 的字符串插值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆