(3) Controllers :-
we will talk about :-
  • Controllers Using .
  • Main Directory .
  • How to Send variables from Controllers To Templates ?

Controllers Using

The Controllers directory in the folder "WebSite" as there you can put all controllers.


As we learned we have to import System.Web Packages at the first line of any controller.
Main Directory

The main directory for controllers is starting from 'website' folder and you do not have to write the full path ...
So In this example I put the hello.ring in the folder website so in the route.ring we have

#!ring -cgi 
load 'dragon-ring.ring'
import system.Web
app.route('HelloWorld',:ringfile,"hello.ring")

And I put the hello.html in the folder website/templates so I have to put this code in website/hello.ring
dring{
        html(template('hello.html',null))
     }

In the WebSite/templates/hello.html I have this code
<h1>Hello World From Template .</h1>

it displayed the same result ...


How to Send variables from Controllers To Templates ?

I will use the same files names ...

There is many ways to send Variables to templates

(1) The First Way

Define Your Variables in the route.ring file

In the route.ring we have this code
#!ring -cgi 
load 'dragon-ring.ring'
import system.Web
name = "Mohannad"
app.route('HelloWorld',:ringfile,"hello.ring")

In the website/hello.ring we have this code
import system.Web
dring{
        html(template('hello.html',self))
     }

In the website/templates/hello.html we have this code
Hello {%=name%}

The Page returns :-
Hello Mohannad

Note:- You can Define Your Variables in route.ring as null and edit them in the controller as in this example :-

In the route.ring we have this code
#!ring -cgi 
load 'dragon-ring.ring'
import system.Web
name = null
app.route('HelloWorld',:ringfile,"hello.ring")

In the website/hello.ring we have this code
import system.Web
name = "Mohannad"
dring{
        html(template('hello.html',self))
     }

In the website/templates/hello.html we have this code
Hello {%=name%}

The Page returns :-
Hello Mohannad

(2) Send Variables Using Functions


Use SendVars(var_Name) function to send Variables .

The var_Name Can be a list of Variables,but you have to put them as a string like this example


In the route.ring we have this code
#!ring -cgi 
load 'dragon-ring.ring'
import system.Web
app.route('test_vars',:ringfile,"test_vars.ring")

In the website/test_vars.ring we have this code
import system.Web
SendVars('name')
SendVars('age')
SendVars('salary')
/*or*/
SendVars(['name','age','salary'])
dring{
  Name = 'Mohannad' 
  Age = 20
  Salary = 1200
  html(template('test_vars.html',self))
}

In the WebSite/templates/test_vars.html we have this code
Hello {%=name%} , Your age is :- {%=age%} ,Your salary is :- {%=salary%}$.

The Page returns :-
Hello Mohannad , Your age is :- 20 ,Your salary is :- 1200$.

(3) Send Variables Using Classes


In the route.ring we have this code
#!ring -cgi 
load 'dragon-ring.ring'
import system.Web
app.route('test_vars',:ringfile,"test_vars.ring")

In the website/test_vars.ring we have this code
import system.Web
new test_vars{start()} 
class test_vars
   name = "Mohannad"
   func start
   dring{
   html(template('test_vars.html',this))
     }

In the WebSite/templates/test_vars.html we have this code
Hello {%=name%}

The Page returns :-
Hello Mohannad

I hope it was easy for you ...