Vibe.d navigation

Steven Schveighoffer schveiguy at gmail.com
Wed Apr 1 02:15:25 UTC 2020


On 3/31/20 4:43 PM, GreatSam4sure wrote:

> Thanks for your reply.
> 
> My problem is that I have two views say page1.dt and page2.dt each are 
> HTML 5 pages with head and body. I want to click a button or a link in 
> page to go page 2 vis-a-vis using webinterface. How do I define the web 
> interface functions and how will reference it in the pages
> 

Vibe does not render templates automatically. You have to render them 
from a function. The function can be in a route that has nothing to do 
with the name of the template.

For instance, here is a simple application which uses page1.dt and 
page2.dt, I have purposely not made the names of the functions different 
from page1 and page2 to show that they are not related to the routes. I 
also added a title string which shows how you can pass D symbols to the 
template:

class WebImpl
{
   void getRouteA(HTTPServerRequest req, HTTPServerResponse res) {
       auto titleVar = "This is page 1";
       res.render!("page1.dt", titleVar);
   }

   void getRouteB(HTTPServerRequest req, HTTPServerResponse res) {
       auto titleVar = "This is page 2";
       res.render!("page2.dt", titleVar);
   }
}

--- views/page1.dt:

doctype html
head
   title #{titleVar}
body
   a(href="route_b") Go to page 2

--- views/page2.dt:

doctype html
head
   title #{titleVar}
body
   a(href="route_a") Go to page 1

here is what your main should look like:

void main()
{
     auto settings = new HTTPServerSettings;
     settings.port = 8080;
     settings.bindAddresses = ["127.0.0.1"];
     auto router = new URLRouter;
     router.registerWebInterface(new WebImpl);
     listenHTTP(settings, router);

     logInfo("Please open http://127.0.0.1:8080/ in your browser.");
     runApplication();
}

As you can see, the routes to the pages are not based on the templates, 
but based on the names of the functions. getRouteA means use GET method, 
name of the route is route_a. It is the render function that says "use 
this template for the current route".

That may be what is confusing you, especially if you come from a 
framework that matches routes with templates directly.

-Steve


More information about the Digitalmars-d-learn mailing list