Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I apply Dart's string interpolation dynamically?

(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.

like image 683
Seth Ladd Avatar asked Aug 09 '12 15:08

Seth Ladd


People also ask

How do you make a dynamic variable in Dart?

You can use normal global variables in Dart like explained in Global Variables in Dart. final Map<String,int> myGlobals = <String,int>{}; to create a map that stores integer values with string names. Set values with myGlobals['someName'] = 123; and read them with print(myGlobals['someName']); .

How do you interpolate a Dart string?

To interpolate the value of Dart expressions within strings, use ${} . The curly braces {} are skipped if the expression is an identifier .

What is dynamic keyword in flutter?

dynamic: can change the TYPE of the variable, & can change the VALUE of the variable later in the code. var: can't change the TYPE of the variable, but can change the VALUE of the variable later in code.

What is interpolation in flutter?

GestureForceInterpolation interpolation. The function used to convert the raw device pressure values into a value in the range 0.0 to 1.0. The function takes in the device's minimum, maximum and raw touch pressure and returns a value in the range 0.0 to 1.0 denoting the interpolated touch pressure.


1 Answers

(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.

like image 194
Seth Ladd Avatar answered Sep 27 '22 21:09

Seth Ladd