Skip to content

Commit

Permalink
Add render to respond with template engine (4d tags as default)
Browse files Browse the repository at this point in the history
  • Loading branch information
e-marchand committed Jun 20, 2020
1 parent 3dc7c19 commit 21a2679
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 15 deletions.
8 changes: 7 additions & 1 deletion Project/Sources/Classes/Response.4dm
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,10 @@ Function sendRawData
/* According Accept header return a specific response by providing <context>=$1 and object <mimetype/response>*/
Function format
C_OBJECT:C1216($0;$1;$2)
$0:=cs:C1710.ResponseFormat.new($1;$2;This:C1470)// according to header, send a sub response. html, json; text; xml
$0:=cs:C1710.ResponseFormat.new($1;$2;This:C1470)// according to header, send a sub response. html, json; text; xml

/* proceed template file or text with passed data */
Function render
C_VARIANT:C1683($1)
C_OBJECT:C1216($0;$2)
$0:=cs:C1710.ResponseTemplate.new($1/*file or text*/;This:C1470.code;This:C1470.headers;$2/*data*/)
23 changes: 23 additions & 0 deletions Project/Sources/Classes/ResponseTemplate.4dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Class extends Response

Class constructor
C_VARIANT:C1683($1)// file or text
C_LONGINT:C283($2)
C_OBJECT:C1216($3)
C_OBJECT:C1216($4)
/*C_OBJECT($5)*/
Super:C1705($1;$2;$3)
This:C1470.data:=$4// data to process
/*This.context:=$5 to choose template engine? */

If (Value type:C1509(This:C1470.response)=Is object:K8:27)
If (OB Instance of:C1731(This:C1470.response.getText;4D:C1709.Function))// less restrictive thn file
This:C1470.response:=This:C1470.response.getText()
End if
End if

This:C1470.response:=cs:C1710.Template4DTag.new().render(This:C1470.response;This:C1470.data)

Function doSend
Super:C1706.doSend()

6 changes: 6 additions & 0 deletions Project/Sources/Classes/Template4DTag.4dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

Function render
C_OBJECT:C1216($2)
C_TEXT:C284($0;$1;$outputText)
PROCESS 4D TAGS:C816($1;$outputText;$2)
$0:=$outputText
4 changes: 4 additions & 0 deletions Project/Sources/Methods/_new router test.4dm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ $router.get("/format";Formula:C1597($2.format($1;New object:C1471(\
))))

$router.get("/textplain";"hey")
$router.get("/template";Formula:C1597($2.status(202).render("Name: <!--#4DEVAL $1.firstname --> <b><!--#4DEVAL $1.lastname --></b>";\
New object:C1471("firstname";"eric";"lastname";"mesopelagique"))))
$router.get("/template/file";Formula:C1597($2.status(202).render(Folder:C1567(fk resources folder:K87:11).file("template.html");\
New object:C1471("firstname";"phimage";"lastname";"mesopelagique";"menu";New collection:C1472("Home";"Info";"Contact")))))

$router.get("/router.http";Formula:C1597($2.status(200).attachment("router.http").send($router.restClientHTTP($1))))

Expand Down
8 changes: 7 additions & 1 deletion Project/Sources/folders.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"ResponseFile",
"ResponseFormat",
"ResponseRaw",
"ResponseRedirect"
"ResponseRedirect",
"ResponseTemplate"
]
},
"Router": {
Expand All @@ -51,6 +52,11 @@
"Router"
]
},
"Template": {
"classes": [
"Template4DTag"
]
},
"Web": {
"methods": [
"WebGetHTTPHeaders",
Expand Down
3 changes: 2 additions & 1 deletion Project/Sources/settings.4DSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?><preferences stamp="4">
<?xml version="1.0" encoding="UTF-8"?><preferences stamp="5">
<com.4d>
<server>
<network>
Expand All @@ -13,5 +13,6 @@
<compiler>
<options compilation_path="3" symbol_file="false"/>
</compiler>
<general execute_host_database_event="true"/>
</com.4d>
</preferences>
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Tricho 🕷
# Tricho 🕷

[![language](https://img.shields.io/static/v1?label=language&message=4d&color=blue)](https://developer.4d.com/)
[![language-top](https://img.shields.io/github/languages/top/mesopelagique/Tricho.svg)](https://developer.4d.com/)
Expand All @@ -12,6 +12,7 @@ Helpers functions for web development and a web router:
$router:=tricho.router()
$router.get("/hello";"Hello world")
$router.post("/create";Formula($2.status(201).download("path/of/file"))
$router.get("/employee";Formula($2.render($templateFile;$employeeObject)))
...
```

Expand Down Expand Up @@ -46,6 +47,7 @@ $router:=tricho.router()
$router.get("/hello";"Hello world")
...
```

> For test purpose you could create it at each client request but for efficiency in production mode cache it into a variable
### Handle the request
Expand All @@ -72,14 +74,16 @@ $router.all("/hello";Formula("This is a "+$1.method))

Last parameters is the data to return to the HTTP client.

If you use a formula,
If you use a formula,

- the code could be dynamic ie. executed each times
- you can call an other methods to manage response
- you receive
- you receive
- as $1 a context/request object with some useful features (to get headers, variables, ...)
- as $2 a reponse builder to be able to change status code, provoque a file download, add headers/cookies, etc..

If you return
If you return

- an object or a collection, it will be JSON stringifyed
- a `File`, it will be send as blob (with mime type according to file extension)

Expand All @@ -105,27 +109,27 @@ The class must defined the `path` and `methods` attributes.

```4d
Class constructor
This.methods:=New collection(HTTPMethod .GET)
This.path:="/a/class/path"
This.methods:=New collection(HTTPMethod .GET)
This.path:="/a/class/path"
```

and must define a function to return the data.

```4d
Function respond
C_VARIANT($0)
C_OBJECT($1) // $context
$0:="Hello" // Return a String, an Object(JSON), 4D.File...
C_VARIANT($0)
C_OBJECT($1) // $context
$0:="Hello" // Return a String, an Object(JSON), 4D.File...
```

alteratively you can defined function by HTTP method if you do not defined `methods` attribute

```4d
Function get
$0:="Hello"
$0:="Hello"
Function post
$0:=New object("success";True)
$0:=New object("success";True)
```

## Handler
Expand Down Expand Up @@ -185,4 +189,3 @@ Router is inspired by numerous packages of different languages such as Flask for
- Name come from [Trichobothria](https://en.m.wikipedia.org/wiki/Trichobothria)

[<img src="https://mesopelagique.github.io/quatred.png" alt="mesopelagique"/>](https://mesopelagique.github.io/)

94 changes: 94 additions & 0 deletions Resources/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<html>

<head>
<style>
body {
margin: 0;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: cornsilk;
}

ul {
padding: 0;
list-style-type: none;
}

ul li {
box-sizing: border-box;
width: 15em;
height: 3em;
font-size: 20px;
border-radius: 0.5em;
margin: 0.5em;
box-shadow: 0 0 1em rgba(0, 0, 0, 0.2);
color: white;
font-family: sans-serif;
text-transform: capitalize;
line-height: 3em;
transition: 0.3s;
cursor: pointer;
}

ul li:nth-child(odd) {
background: linear-gradient(to right, orange, tomato);
text-align: left;
padding-left: 10%;
transform: perspective(500px) rotateY(45deg);
}

ul li:nth-child(even) {
background: linear-gradient(to left, orange, tomato);
text-align: right;
padding-right: 10%;
transform: perspective(500px) rotateY(-45deg);
}

ul li:nth-child(odd):hover {
transform: perspective(200px) rotateY(45deg);
padding-left: 5%;
}

ul li:nth-child(even):hover {
transform: perspective(200px) rotateY(-45deg);
padding-right: 5%;
}

.name {
box-sizing: border-box;
width: 15em;
height: 8em;
font-size: 20px;
border-radius: 0.5em;
margin: 0.5em;
box-shadow: 0 0 1em rgba(0, 0, 0, 0.2);
background: linear-gradient(to right, orange, tomato);
color: white;
font-family: sans-serif;
text-transform: capitalize;
line-height: 3em;
}
</style>
</head>

<body>
<div>
<div class="name">
<b>
<!--#4DEVAL $1.lastname --></b><br />
<!--#4DEVAL $1.firstname -->
<br />
</div>
<ul>
<!--#4DEVAL $i:=0-->
<!--#4DLOOP ($i<$1.menu.length)-->
<li><!--#4DEVAL $1.menu[$i]--></li>
<!--#4DEVAL $i:=$i+1-->
<!--#4DENDLOOP-->
</ul>
</div>
</body>

</html>

0 comments on commit 21a2679

Please sign in to comment.