Example: Write a tpl file named "my.tpl":
<html> <p> <%= "This is tpl test!" %> </p> <p> <% (format #t "And this is ~a" (getcwd)) %> </p> <p> <%= external-var %> </p> </html>
The filename extension ".tpl" is mandatory when using the MVC. Since the MVC will find the template by detecting controller name automatically.
If you don’t use the MVC, and are rather writing all in one script file loading GNU Artanis modules. Then you don’t need to follow this rule.
NOTE: Don’t wrap code in double-quotes, for example:
<a href="<%= my-url %>">click me</a> <!-- Wrong! --> <a href=<%= my-url %> >click me</a> <!-- Correct! -->
If you need to output a double-quoted string, please use object->string
to convert in Scheme first.
<a href=<%= (object->string my-url) %> >click me</a> <!-- If my-url is not properly quoted -->
(get "/test" (lambda (rc) (let ((external-var 123)) (tpl->response "my.tpl" (the-environment))))) (run #:port 8080)
In this case, make sure to put my.tpl in the same path as your GNU Artanis code.
Since external-var is defined outside the file "my.tpl", and it’s bound in let with 123, you have to pass (the-environment). Or the template render will complain aobut not being able to find the variable external-var.
If you don’t need to refer to any external vars, just use (tpl->response "file.tpl")
.
To test, access http://localhost:3000/test in your browser.