|
| 1 | +import { CodeBlock } from "@/components/code-block"; |
| 2 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; |
| 3 | +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 4 | + |
| 5 | +export function ExamplesPage() { |
| 6 | + return ( |
| 7 | + <div className="max-w-4xl mx-auto space-y-8"> |
| 8 | + <div className="space-y-4"> |
| 9 | + <h1 className="text-3xl font-bold">Real-World Examples</h1> |
| 10 | + <p className="text-lg text-muted-foreground"> |
| 11 | + Practical examples demonstrating how to use FactoryTools in real scenarios. |
| 12 | + </p> |
| 13 | + </div> |
| 14 | + |
| 15 | + <Card> |
| 16 | + <CardHeader> |
| 17 | + <CardTitle>Newsletter Template</CardTitle> |
| 18 | + <CardDescription> |
| 19 | + Generate a formatted newsletter with articles and conditional offers. |
| 20 | + </CardDescription> |
| 21 | + </CardHeader> |
| 22 | + <CardContent className="space-y-4"> |
| 23 | + <CodeBlock |
| 24 | + code={`var context = new ExecutionContext(); |
| 25 | +context.Set("NewsletterTitle", "Tech Weekly Digest"); |
| 26 | +context.Set("Edition", "Issue #42"); |
| 27 | +context.Set("SubscriberName", "Alex"); |
| 28 | +context.Set("Articles", new List<object> |
| 29 | +{ |
| 30 | + new { Title = "AI Breakthroughs in 2024", |
| 31 | + Summary = "Latest developments in artificial intelligence.", |
| 32 | + Author = "Dr. Smith" }, |
| 33 | + new { Title = "Cloud Computing Trends", |
| 34 | + Summary = "What to expect in cloud infrastructure.", |
| 35 | + Author = "Jane Miller" }, |
| 36 | + new { Title = "Cybersecurity Best Practices", |
| 37 | + Summary = "Protecting your digital assets.", |
| 38 | + Author = "Bob Wilson" } |
| 39 | +}); |
| 40 | +context.Set("HasSpecialOffer", true); |
| 41 | +context.Set("OfferDescription", "Get 50% off our annual subscription!"); |
| 42 | +
|
| 43 | +var template = """ |
| 44 | + ??????????????????????????? |
| 45 | + \${Context.NewsletterTitle} - \${Context.Edition} |
| 46 | + ??????????????????????????? |
| 47 | +
|
| 48 | + Hello \${Context.SubscriberName}! |
| 49 | +
|
| 50 | + Here's what's new this week: |
| 51 | +
|
| 52 | + @foreach (var article in Context.Articles) { |
| 53 | + ? \${article.Title} |
| 54 | + \${article.Summary} |
| 55 | + By: \${article.Author} |
| 56 | +
|
| 57 | + } |
| 58 | + @if (Context.HasSpecialOffer) { |
| 59 | + ? SPECIAL OFFER ? |
| 60 | + \${Context.OfferDescription} |
| 61 | +
|
| 62 | + } |
| 63 | + Thank you for reading! |
| 64 | +
|
| 65 | + Unsubscribe: https://example.com/unsubscribe |
| 66 | + """; |
| 67 | +
|
| 68 | +var result = engine.Render(template, context);`} |
| 69 | + language="csharp" |
| 70 | + /> |
| 71 | + </CardContent> |
| 72 | + </Card> |
| 73 | + |
| 74 | + <Card> |
| 75 | + <CardHeader> |
| 76 | + <CardTitle>Invoice Template</CardTitle> |
| 77 | + <CardDescription> |
| 78 | + Create professional invoices with line items and calculations. |
| 79 | + </CardDescription> |
| 80 | + </CardHeader> |
| 81 | + <CardContent className="space-y-4"> |
| 82 | + <CodeBlock |
| 83 | + code={`var context = new ExecutionContext(); |
| 84 | +context.Set("InvoiceNumber", "INV-2024-0042"); |
| 85 | +context.Set("InvoiceDate", "June 15, 2024"); |
| 86 | +context.Set("DueDate", "July 15, 2024"); |
| 87 | +context.Set("CompanyName", "TechServices LLC"); |
| 88 | +context.Set("CompanyAddress", "789 Commerce Street, Chicago, IL 60601"); |
| 89 | +context.Set("ClientName", "Global Enterprises"); |
| 90 | +context.Set("ClientContact", "Robert Johnson"); |
| 91 | +context.Set("ClientAddress", "321 Corporate Blvd, Dallas, TX 75201"); |
| 92 | +context.Set("LineItems", new List<object> |
| 93 | +{ |
| 94 | + new { Description = "Web Development Services", |
| 95 | + Hours = 40, Rate = "150.00", Amount = "6000.00" }, |
| 96 | + new { Description = "UI/UX Design", |
| 97 | + Hours = 20, Rate = "125.00", Amount = "2500.00" }, |
| 98 | + new { Description = "Project Management", |
| 99 | + Hours = 10, Rate = "100.00", Amount = "1000.00" } |
| 100 | +}); |
| 101 | +context.Set("Subtotal", "9500.00"); |
| 102 | +context.Set("TaxRate", "8%"); |
| 103 | +context.Set("TaxAmount", "760.00"); |
| 104 | +context.Set("Total", "10260.00"); |
| 105 | +context.Set("IsPaid", false); |
| 106 | +
|
| 107 | +var template = """ |
| 108 | + ??????????????????????????????????????????????????? |
| 109 | + ? INVOICE ? |
| 110 | + ??????????????????????????????????????????????????? |
| 111 | +
|
| 112 | + From: \${Context.CompanyName} |
| 113 | + \${Context.CompanyAddress} |
| 114 | +
|
| 115 | + To: \${Context.ClientName} |
| 116 | + Attn: \${Context.ClientContact} |
| 117 | + \${Context.ClientAddress} |
| 118 | +
|
| 119 | + Invoice #: \${Context.InvoiceNumber} |
| 120 | + Date: \${Context.InvoiceDate} |
| 121 | + Due Date: \${Context.DueDate} |
| 122 | +
|
| 123 | + ??????????????????????????????????????????????? |
| 124 | + Description Hours Rate Amount |
| 125 | + ??????????????????????????????????????????????? |
| 126 | + @foreach (var item in Context.LineItems) { |
| 127 | + \${item.Description} \${item.Hours} $\${item.Rate} $\${item.Amount} |
| 128 | + } |
| 129 | + ??????????????????????????????????????????????? |
| 130 | + Subtotal: $\${Context.Subtotal} |
| 131 | + Tax (\${Context.TaxRate}): $\${Context.TaxAmount} |
| 132 | + ????????????????????????? |
| 133 | + TOTAL: $\${Context.Total} |
| 134 | +
|
| 135 | + @if (Context.IsPaid) { |
| 136 | + ? PAID - Thank you for your payment! |
| 137 | + } else { |
| 138 | + Payment due by \${Context.DueDate}. Please remit payment to the address above. |
| 139 | + } |
| 140 | +
|
| 141 | + Thank you for your business! |
| 142 | + """; |
| 143 | +
|
| 144 | +var result = engine.Render(template, context);`} |
| 145 | + language="csharp" |
| 146 | + /> |
| 147 | + </CardContent> |
| 148 | + </Card> |
| 149 | + |
| 150 | + <Card> |
| 151 | + <CardHeader> |
| 152 | + <CardTitle>Template Services</CardTitle> |
| 153 | + <CardDescription> |
| 154 | + Extend FactoryTools with custom services like Humanizer and NCalc. |
| 155 | + </CardDescription> |
| 156 | + </CardHeader> |
| 157 | + <CardContent> |
| 158 | + <Tabs defaultValue="humanizer"> |
| 159 | + <TabsList className="grid w-full grid-cols-2"> |
| 160 | + <TabsTrigger value="humanizer">Humanizer</TabsTrigger> |
| 161 | + <TabsTrigger value="ncalc">NCalc</TabsTrigger> |
| 162 | + </TabsList> |
| 163 | + <TabsContent value="humanizer" className="space-y-4"> |
| 164 | + <p className="text-sm text-muted-foreground"> |
| 165 | + Use Humanizer to pluralize, singularize, and humanize text. |
| 166 | + </p> |
| 167 | + <CodeBlock |
| 168 | + code={`using Humanizer; |
| 169 | +
|
| 170 | +var context = new ExecutionContext(); |
| 171 | +
|
| 172 | +// Register Humanizer services |
| 173 | +context.RegisterService("pluralize", |
| 174 | + input => input?.ToString()?.Pluralize() ?? ""); |
| 175 | +context.RegisterService("singularize", |
| 176 | + input => input?.ToString()?.Singularize() ?? ""); |
| 177 | +
|
| 178 | +context.Set("EntityName", "customer"); |
| 179 | +context.Set("CollectionName", "categories"); |
| 180 | +
|
| 181 | +var template = """ |
| 182 | + Entity: \${Context.Services('singularize')(Context.CollectionName)} |
| 183 | + Collection: \${Context.Services('pluralize')(Context.EntityName)} |
| 184 | + |
| 185 | + Irregular plurals: |
| 186 | + - person ? \${Context.Services('pluralize')('person')} |
| 187 | + - child ? \${Context.Services('pluralize')('child')} |
| 188 | + - goose ? \${Context.Services('pluralize')('goose')} |
| 189 | + """; |
| 190 | +
|
| 191 | +var result = engine.Render(template, context); |
| 192 | +// Output: |
| 193 | +// Entity: category |
| 194 | +// Collection: customers |
| 195 | +// |
| 196 | +// Irregular plurals: |
| 197 | +// - person ? people |
| 198 | +// - child ? children |
| 199 | +// - goose ? geese`} |
| 200 | + language="csharp" |
| 201 | + /> |
| 202 | + </TabsContent> |
| 203 | + <TabsContent value="ncalc" className="space-y-4"> |
| 204 | + <p className="text-sm text-muted-foreground"> |
| 205 | + Use NCalc for runtime mathematical expressions. |
| 206 | + </p> |
| 207 | + <CodeBlock |
| 208 | + code={`using NCalc; |
| 209 | +
|
| 210 | +var context = new ExecutionContext(); |
| 211 | +
|
| 212 | +// Register NCalc calculator service |
| 213 | +context.RegisterService("calc", input => |
| 214 | +{ |
| 215 | + var expr = new Expression(input?.ToString() ?? "0"); |
| 216 | + return expr.Evaluate(); |
| 217 | +}); |
| 218 | +
|
| 219 | +var template = """ |
| 220 | + Invoice Summary |
| 221 | + --------------- |
| 222 | + Items: \${Context.Services('calc')('5')} widgets |
| 223 | + Subtotal: $\${Context.Services('calc')('19.99 * 5')} |
| 224 | + Tax: $\${Context.Services('calc')('19.99 * 5 * 0.08')} |
| 225 | + Total: $\${Context.Services('calc')('19.99 * 5 * 1.08')} |
| 226 | + """; |
| 227 | +
|
| 228 | +var result = engine.Render(template, context); |
| 229 | +// Output: |
| 230 | +// Invoice Summary |
| 231 | +// --------------- |
| 232 | +// Items: 5 widgets |
| 233 | +// Subtotal: $99.95 |
| 234 | +// Tax: $8.00 |
| 235 | +// Total: $107.95`} |
| 236 | + language="csharp" |
| 237 | + /> |
| 238 | + </TabsContent> |
| 239 | + </Tabs> |
| 240 | + </CardContent> |
| 241 | + </Card> |
| 242 | + |
| 243 | + <Card> |
| 244 | + <CardHeader> |
| 245 | + <CardTitle>Combining Services</CardTitle> |
| 246 | + <CardDescription> |
| 247 | + Use multiple services together in the same template. |
| 248 | + </CardDescription> |
| 249 | + </CardHeader> |
| 250 | + <CardContent className="space-y-4"> |
| 251 | + <CodeBlock |
| 252 | + code={`var context = new ExecutionContext(); |
| 253 | +
|
| 254 | +// Register multiple services |
| 255 | +context.RegisterService("pluralize", |
| 256 | + input => input?.ToString()?.Pluralize() ?? ""); |
| 257 | +context.RegisterService("calc", input => |
| 258 | +{ |
| 259 | + var expr = new NCalc.Expression(input?.ToString() ?? "0"); |
| 260 | + return expr.Evaluate(); |
| 261 | +}); |
| 262 | +
|
| 263 | +var template = """ |
| 264 | + Order Summary |
| 265 | + ------------- |
| 266 | + We have \${Context.Services('calc')('5 + 3')} \${Context.Services('pluralize')('order')}. |
| 267 | + Total: $\${Context.Services('calc')('8 * 29.99')} |
| 268 | + """; |
| 269 | +
|
| 270 | +var result = engine.Render(template, context); |
| 271 | +// Output: |
| 272 | +// Order Summary |
| 273 | +// ------------- |
| 274 | +// We have 8 orders. |
| 275 | +// Total: $239.92`} |
| 276 | + language="csharp" |
| 277 | + /> |
| 278 | + </CardContent> |
| 279 | + </Card> |
| 280 | + |
| 281 | + <Card> |
| 282 | + <CardHeader> |
| 283 | + <CardTitle>Custom Template Services</CardTitle> |
| 284 | + <CardDescription> |
| 285 | + Create your own template services by implementing ITemplateService. |
| 286 | + </CardDescription> |
| 287 | + </CardHeader> |
| 288 | + <CardContent className="space-y-4"> |
| 289 | + <CodeBlock |
| 290 | + code={`public class HumanizerService : ITemplateService |
| 291 | +{ |
| 292 | + public string Name => "pluralize"; |
| 293 | + |
| 294 | + public object? Transform(object? input) |
| 295 | + { |
| 296 | + return input?.ToString()?.Pluralize(); |
| 297 | + } |
| 298 | +} |
| 299 | +
|
| 300 | +// Register the service |
| 301 | +var context = new ExecutionContext(); |
| 302 | +context.RegisterService(new HumanizerService()); |
| 303 | +
|
| 304 | +// Use in template |
| 305 | +var result = engine.Render( |
| 306 | + "\${Context.Services('pluralize')('customer')}", |
| 307 | + context); |
| 308 | +// Output: customers`} |
| 309 | + language="csharp" |
| 310 | + /> |
| 311 | + </CardContent> |
| 312 | + </Card> |
| 313 | + </div> |
| 314 | + ); |
| 315 | +} |
0 commit comments