A practical, real-world example of integration: using the Transformer and Expression Parser together to create a simple template engine that replaces placeholders with evaluated data.
ID: 1359
using uCalcSoftware;
var uc = new uCalc();
// Define the data context for our template
uc.DefineVariable("user = 'Alice'");
uc.DefineVariable("score = 95");
// Define the template with placeholders
var myTemplate = "User: {user}, Score: {score * 10}, Status: {IIf(score > 90, 'Excellent', 'Good')}";
var t = uc.NewTransformer();
// This single rule finds placeholders like {...}
// and uses the Expression Parser ({@@Eval}) to evaluate the content inside.
t.FromTo("'{' {expr} '}'", "{@@Eval: expr}");
Console.WriteLine(t.Transform(myTemplate));
User: Alice, Score: 950, Status: Excellent using uCalcSoftware; var uc = new uCalc(); // Define the data context for our template uc.DefineVariable("user = 'Alice'"); uc.DefineVariable("score = 95"); // Define the template with placeholders var myTemplate = "User: {user}, Score: {score * 10}, Status: {IIf(score > 90, 'Excellent', 'Good')}"; var t = uc.NewTransformer(); // This single rule finds placeholders like {...} // and uses the Expression Parser ({@@Eval}) to evaluate the content inside. t.FromTo("'{' {expr} '}'", "{@@Eval: expr}"); Console.WriteLine(t.Transform(myTemplate));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Define the data context for our template
uc.DefineVariable("user = 'Alice'");
uc.DefineVariable("score = 95");
// Define the template with placeholders
auto myTemplate = "User: {user}, Score: {score * 10}, Status: {IIf(score > 90, 'Excellent', 'Good')}";
auto t = uc.NewTransformer();
// This single rule finds placeholders like {...}
// and uses the Expression Parser ({@@Eval}) to evaluate the content inside.
t.FromTo("'{' {expr} '}'", "{@@Eval: expr}");
cout << t.Transform(myTemplate) << endl;
}
User: Alice, Score: 950, Status: Excellent #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Define the data context for our template uc.DefineVariable("user = 'Alice'"); uc.DefineVariable("score = 95"); // Define the template with placeholders auto myTemplate = "User: {user}, Score: {score * 10}, Status: {IIf(score > 90, 'Excellent', 'Good')}"; auto t = uc.NewTransformer(); // This single rule finds placeholders like {...} // and uses the Expression Parser ({@@Eval}) to evaluate the content inside. t.FromTo("'{' {expr} '}'", "{@@Eval: expr}"); cout << t.Transform(myTemplate) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Define the data context for our template
uc.DefineVariable("user = 'Alice'")
uc.DefineVariable("score = 95")
'// Define the template with placeholders
Dim myTemplate = "User: {user}, Score: {score * 10}, Status: {IIf(score > 90, 'Excellent', 'Good')}"
Dim t = uc.NewTransformer()
'// This single rule finds placeholders like {...}
'// and uses the Expression Parser ({@@Eval}) to evaluate the content inside.
t.FromTo("'{' {expr} '}'", "{@@Eval: expr}")
Console.WriteLine(t.Transform(myTemplate))
End Sub
End Module
User: Alice, Score: 950, Status: Excellent Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Define the data context for our template uc.DefineVariable("user = 'Alice'") uc.DefineVariable("score = 95") '// Define the template with placeholders Dim myTemplate = "User: {user}, Score: {score * 10}, Status: {IIf(score > 90, 'Excellent', 'Good')}" Dim t = uc.NewTransformer() '// This single rule finds placeholders like {...} '// and uses the Expression Parser ({@@Eval}) to evaluate the content inside. t.FromTo("'{' {expr} '}'", "{@@Eval: expr}") Console.WriteLine(t.Transform(myTemplate)) End Sub End Module