Advertisement
Guest User

insert.d - BaussProjects - daweb

a guest
Dec 4th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.03 KB | None | 0 0
  1. // http://baussprojects.comlu.com/projects.php#daweb
  2. import std.array : replace, join;
  3. import std.conv : to;
  4. import std.string : isNumeric;
  5.  
  6. /**
  7. *   Class test.
  8. *   HelloCreator creates a specific amount of lines with Hello and a number.
  9. */
  10. class HelloCreator {
  11.     int amount;
  12.     this(int amount) {
  13.         this.amount = amount;
  14.     }
  15.    
  16.     string create() {
  17.         string hello;
  18.         foreach (i; 0 .. amount)
  19.             hello ~= "Hello " ~ to!string(i) ~ "!<br />\r\n";
  20.         return hello;
  21.     }
  22. }
  23.  
  24. if (post) {
  25.     // If POST do this
  26.     // Attempts to get "number" from the post data. Gives an empty string if not found.
  27.     string number = postData.get("number", "");
  28.     if (!isNumeric(number)) {
  29.         // If the input is not a number print an error message
  30.         html = replace(html, "@VALUE", "Please type a number! '" ~ number ~ "' is not valid!");
  31.     }
  32.     else {
  33.         // If the input is a number do this
  34.         // Creates a new instance of HelloCreator with the amount of Hello's to be the input number
  35.         auto hello = new HelloCreator(to!int(number));
  36.         // Replaces "@VALUE" within the html with the return value of create()
  37.         html = replace(html, "@VALUE", hello.create());
  38.     }
  39. }
  40. else {
  41.     // if GET do this
  42.     // Creates a new instance of HelloCreator with the amount of Hello's to be 10
  43.     auto hello = new HelloCreator(10);
  44.         // Replaces "@VALUE" within the html with the return value of create()
  45.     html = replace(html, "@VALUE", hello.create());
  46. }
  47.  
  48. if (queryString) {
  49.     // If there is a query string do this
  50.     // Joins all the query string keys into a string with ", " as a separator
  51.     html = replace(html, "@QUERIES", join(queryString.keys, ", "));
  52. }
  53. else {
  54.     // If there is no query string just type N/A
  55.     html = replace(html, "@QUERIES", "N/A");
  56. }
  57. <html>
  58.  
  59. <head>
  60.     <title>Index</title>
  61. </head>
  62. <body>
  63.     @QUERIES
  64.     <p>
  65.         <span style="color: #ffffff; background-color: #000000;">
  66.             @VALUE
  67.         </span>
  68.     </p>
  69.    
  70.     <form action="index.d" method="POST">
  71.         Type a number:<br />
  72.         <input type="text" name="number" /><br />
  73.         <input type="submit" value="Submit" />
  74.     </form>
  75. </body>
  76.  
  77. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement