On Tue, 10 Dec 2019 22:42:29 -0500, Steven Schveighoffer wrote:

On 12/10/19 9:54 PM, selimozel wrote:

On Sun, 8 Dec 2019 13:26:07 -0500, Steven Schveighoffer wrote:

On 12/6/19 10:40 PM, selimozel wrote:

Hi There. This is my first post!

I am building a web application using Vibe-D. My source and diet templates are based on examples provided by the community.

I wonder if there is a way to write a simple function that returns an argument and call that function from a diet template. I checked calling embedded code section [1] but what I really want to have is to call functions from the source code directly.

[1] http://vibed.org/templates/diet#embedded-code

Yes, you can, just import the module that contains the function you need
to call. Then call it from D-mode.

e.g.:

  • import std.stdio;
  • writeln("hello, world!");

Now, when you render your diet template, the server should print hello
world to the console.

Thank you Steve! I managed to run native D from diet templates. No issues there. Turns out what I was really looking for was HTTP requests sent to my server. I took a look at web_ajax example [1] and it is exactly what I need. I am new to web development so there's a bit of a learning curve.

I got another question now. Some of the examples under vibe-d are using "shared static this()" to define the main part of the program. I get a linker error when I try to build those ones using an atomic dub.sdl file. My hack for now is to just convert them back to "void main()" and move along. Would be happy to hear what experienced people think about this one.

That is the old way of doing things. Vibe.d used to define it's own main
function and run the event loop after your static constructors ran. Now,
you should define your own main, and model it just like it is at the
bottom of the main description.

e.g.:

import vibe.vibe;

void main()
{

 // set up your listener/router and everything here

 // returns false if a help screen has been requested and displayed 

(--help)

 if (!finalizeCommandLineOptions())
      return;
 lowerPrivileges();
 runEventLoop();

 // tear down your listener/etc here.

}

The example text really should be updated to reflect this...

-Steve

P.S. nice to see a WPI student ;) I'm class of '98 (or '99, I graduated
slightly late, so not sure).

Thanks for the great answer Steve! I suspected it was a legacy issue. Some of the examples run perfect right outside the box. I think some of them have the old way doing things and throw the error when I compile on my machine. Either way, the above fix got me going forward for now.

Great to meet another alumni here! I graduated with my Ph.D. in 2018. Now I'm working for a small robotics company in PNW.

S