code_junkie

Templating Engine to Generate Simple Reports in .NET

Nov 12th, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>My Report</title>
  4. </head>
  5. <body>
  6. <% foreach (ReportRow r in ReportData) { %>
  7. <!-- Markup and Code for Report -->
  8. <% } %>
  9. </body>
  10. </html>
  11.  
  12. <viewdata products="IEnumerable[[Product]]"/>
  13. <ul if="products.Any()">
  14. <li each="var p in products">${p.Name}</li>
  15. </ul>
  16. <else>
  17. <p>No products available</p>
  18. </else>
  19.  
  20. <script type="text/html" id="item_tmpl">
  21. <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>">
  22. <div class="grid_1 alpha right">
  23. <img class="righted" src="<%=profile_image_url%>"/>
  24. </div>
  25. <div class="grid_6 omega contents">
  26. <p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p>
  27. </div>
  28. </div>
  29. </script>
  30.  
  31. Dear $User.FullName$,
  32. {%set orders=User.GetOrders() /}
  33. Thank you for your order of $orders.Length$ items, We believe you will be very satisfied with the quality of costume pieces included in each. It is this quality that makes our imaginative play apparel so unique.
  34.  
  35. We created an account for you to track your orders. Here is the login information:
  36. Email: $User.EmailAddress$
  37. Password: $User.Password$
  38.  
  39. Following is the details of your order (OrderId: $OrderId$):
  40. # Part ID Name Quantity Price(per unit) Sub Total
  41. {%set Total=0.0 /}{%foreach orderproduct,i in orders%}{%set Total = Total + orderproduct.Price * orderproduct.Quantity/}
  42. {%rendertemplate orderproducttemplate item=orderproduct/}{$foreach%}
  43. Total: $PadLeft(Format(Total,"$$#.##"),11)$
  44.  
  45. If you have any concern, please call us at 913-555-0115.
  46.  
  47. Sincerely,
  48.  
  49. $CompanyName$
  50.  
  51. {%template orderproducttemplate%}$PadLeft(i,4)$$PadLeft(item.PartId, 7)$ $PadRight(item.ProductName, 15)$ $PadRight(item.Quantity, 5)$ $PadLeft(Format(item.Price,"$$#.##"), 7)$ $PadLeft(Format(item.Price*item.Quantity,"$$#.##"), 12)${$template%}
  52.  
  53. Dear John Borders,
  54.  
  55. Thank you for your order of 3 items, We believe you will be very satisfied with the quality of costume pieces included in each. It is this quality that makes our imaginative play apparel so unique.
  56.  
  57. We created an account for you to track your orders. Here is the login information:
  58. Password: 123abc
  59.  
  60. Following is the details of your order (OrderId: 1625DGHJ):
  61. # Part ID Name Quantity Price(per unit) Sub Total
  62.  
  63. 0 1239 Product A 3 $104.09 $312.27
  64. 1 22 Product B 1 $134.09 $134.09
  65. 2 167 Product C 5 $14.7 $73.5
  66.  
  67. Total: $519.86
  68.  
  69. If you have any concern, please call us at 913-555-0115.
  70.  
  71. Sincerely,
  72.  
  73. My Company Name
  74.  
  75. class OrderProduct
  76. {
  77. private int _partId;
  78. private string _productName;
  79. private int _quantity;
  80. private float _price;
  81.  
  82. public int PartId
  83. {
  84. get { return _partId; }
  85. set { _partId = value; }
  86. }
  87.  
  88. public string ProductName
  89. {
  90. get { return _productName; }
  91. set { _productName = value; }
  92. }
  93.  
  94. public int Quantity
  95. {
  96. get { return _quantity; }
  97. set { _quantity = value; }
  98. }
  99.  
  100. public float Price
  101. {
  102. get { return _price; }
  103. set { _price = value; }
  104. }
  105. }
  106.  
  107. class User
  108. {
  109. private string _fullName;
  110. private string _emailAddress;
  111. private string _password;
  112.  
  113. public string FullName
  114. {
  115. get { return _fullName; }
  116. set { _fullName = value; }
  117. }
  118.  
  119. public string EmailAddress
  120. {
  121. get { return _emailAddress; }
  122. set { _emailAddress = value; }
  123. }
  124.  
  125. public string Password
  126. {
  127. get { return _password; }
  128. set { _password = value; }
  129. }
  130.  
  131. public OrderProduct[] GetOrders()
  132. {
  133. OrderProduct[] ops = new OrderProduct[3];
  134.  
  135. ops[0] = new OrderProduct();
  136. ops[0].PartId = 1239;
  137. ops[0].Price = 104.09f;
  138. ops[0].ProductName = "Product A";
  139. ops[0].Quantity = 3;
  140.  
  141. ops[1] = new OrderProduct();
  142. ops[1].PartId = 22;
  143. ops[1].Price = 134.09f;
  144. ops[1].ProductName = "Product B";
  145. ops[1].Quantity = 1;
  146.  
  147. ops[2] = new OrderProduct();
  148. ops[2].PartId = 167;
  149. ops[2].Price = 14.7f;
  150. ops[2].ProductName = "Product C";
  151. ops[2].Quantity = 5;
  152.  
  153. return ops;
  154. }
  155. }
  156.  
  157. private void btnRun_Click(object sender, EventArgs e)
  158. {
  159. try
  160. {
  161. dt.LoadFromString(txtSource.Text);
  162. dt.SetValue("CompanyName", "My Company Name");
  163.  
  164. User u = new User();
  165. u.EmailAddress = "[email protected]";
  166. u.FullName = "John Borders";
  167. u.Password = "123abc";
  168. dt.SetValue("User", u);
  169. dt.SetValue("OrderId", "1625DGHJ");
  170.  
  171. txtOutput.Text = dt.Run();
  172. }
  173. catch (Exception exc)
  174. {
  175. MessageBox.Show("An error occurred: " + exc.Message);
  176. }
  177. }
Add Comment
Please, Sign In to add comment