RejectedSoftware Forums

Sign up

Pages: 1 2

Calling D Functions Inside Diet Templates

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

Re: Calling D Functions Inside Diet Templates

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.

-Steve

Re: Calling D Functions Inside Diet Templates

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

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

I decided to test this application. And created a new module hayvan.kedi (Which means animal.cat)

module havyan.kedi;


string isim()
{
    return "Minnoş";
}

index.dt is like that

- import hayvan.kedi : isim;
- string başlık = "Gömülü D kodu örneği";


doctype html
head
  title= başlık.toUpper
body
  h1 #{ başlık }

  - foreach (i; 1..4)
    p Bu paragrafın numarası #{i}

  - int enBüyük (int ilk, int ikinci)
    - if (ilk > ikinci) return ilk; else return ikinci;

  p 5 ve 12 sayıları arasında en büyük olan #{ enBüyük(5, 12) } 'dir.


  p Aramızda bir kedi mi dolaşıyor? #{ isim() }

Even though I've imported the function which returns the name of the cat declared in the module file, still compiler complains.

index.dt(2,9): Error: module havyan.kedi from file source/hayvan/kedi.d must be imported with 'import havyan.kedi;'
/home/erdem/.dub/packages/diet-ng-1.6.0/diet-ng/source/diet/html.d(116,7): Error: template instance vibe.http.server.staticTemplate!"index.dt".staticTemplate.__lambda1.exec!(StreamOutputRange!(InterfaceProxy!(OutputStream), 1024LU)) error instantiating
/home/erdem/.dub/packages/vibe-d-0.8.6/vibe-d/http/vibe/http/server.d(345,66):        instantiated from here: compileHTMLDietFileString!(StreamOutputRange!(InterfaceProxy!(OutputStream), 1024LU))
/home/erdem/.dub/packages/vibe-d-0.8.6/vibe-d/http/vibe/http/server.d(270,6):        instantiated from here: render!("index.dt", req)
source/app.d(6,27):        instantiated from here: staticTemplate!"index.dt"

Re: Calling D Functions Inside Diet Templates

Am 08.12.2019 um 23:17 schrieb Erdem:

(...)

index.dt(2,9): Error: module havyan.kedi from file source/hayvan/kedi.d must be imported with 'import havyan.kedi;'
/home/erdem/.dub/packages/diet-ng-1.6.0/diet-ng/source/diet/html.d(116,7): Error: template instance vibe.http.server.staticTemplate!"index.dt".staticTemplate.__lambda1.exec!(StreamOutputRange!(InterfaceProxy!(OutputStream), 1024LU)) error instantiating
/home/erdem/.dub/packages/vibe-d-0.8.6/vibe-d/http/vibe/http/server.d(345,66):        instantiated from here: compileHTMLDietFileString!(StreamOutputRange!(InterfaceProxy!(OutputStream), 1024LU))
/home/erdem/.dub/packages/vibe-d-0.8.6/vibe-d/http/vibe/http/server.d(270,6):        instantiated from here: render!("index.dt", req)
source/app.d(6,27):        instantiated from here: staticTemplate!"index.dt"

There is a typo in the package name - the folder is named "hayvan",
whereas the module declaration says "havyan". I only spotted this after
looking for possible Unicode differences in a hex editor ;-)

Re: Calling D Functions Inside Diet Templates

On Tue, 10 Dec 2019 14:04:05 +0100, Sönke Ludwig wrote:```

There is a typo in the package name - the folder is named "hayvan",
whereas the module declaration says "havyan".

Thanks. But after making the necessary changes I still get the following error:

/home/erdem/.dub/packages/vibe-d-0.8.6/vibe-d/http/vibe/http/server.d(269,9): Error: cannot implicitly convert expression __lambda1 of type void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) @system to void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe
/home/erdem/.dub/packages/vibe-d-0.8.6/vibe-d/http/vibe/http/server.d(269,9): Error: cannot implicitly convert expression __lambda1 of type void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) @system to void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe
source/app.d(8,27): Error: template instance vibe.http.server.staticTemplate!"index.dt" error instantiating

We have access to toUpper() function within the html template. So we should also be able to access another function defined in a module, right?

Re: Calling D Functions Inside Diet Templates

On 12/10/19 10:43 AM, Erdem wrote:

On Tue, 10 Dec 2019 14:04:05 +0100, Sönke Ludwig wrote:```

There is a typo in the package name - the folder is named "hayvan",
whereas the module declaration says "havyan".

Thanks. But after making the necessary changes I still get the following error:

/home/erdem/.dub/packages/vibe-d-0.8.6/vibe-d/http/vibe/http/server.d(269,9): Error: cannot implicitly convert expression __lambda1 of type void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) @system to void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe
/home/erdem/.dub/packages/vibe-d-0.8.6/vibe-d/http/vibe/http/server.d(269,9): Error: cannot implicitly convert expression __lambda1 of type void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) @system to void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe
source/app.d(8,27): Error: template instance vibe.http.server.staticTemplate!"index.dt" error instantiating

We have access to toUpper() function within the html template. So we should also be able to access another function defined in a module, right?

I have a feeling it's because your isim function is not declared
@safe. Because it's not a template, it doesn't get inferred safety. I
think this then cascades to some code that is being generated by the
diet parser (i.e. the __lambda1).

Try marking it safe and see what happens.

-Steve

Re: Calling D Functions Inside Diet Templates

On Tue, 10 Dec 2019 15:54:52 -0500, Steven Schveighoffer wrote:

I have a feeling it's because your isim function is not declared
@safe. Because it's not a template, it doesn't get inferred safety. I
think this then cascades to some code that is being generated by the
diet parser (i.e. the __lambda1).

Try marking it safe and see what happens.

Marking this function as safe solved the problem. Thanks.

After necessary corrections, the source code of module hayvan.kedi is as follows.

module hayvan.kedi;

string isim() @safe
{
    return "Minnoş";
}

Re: Calling D Functions Inside Diet Templates

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.

-Steve

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.

Best,
S

[1] https://github.com/vibe-d/vibe.d/tree/master/examples/web_ajax

Re: Calling D Functions Inside Diet Templates

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).

Re: Calling D Functions Inside Diet Templates

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

Pages: 1 2