Next: , Previous: , Up: Layouts and Rendering in GNU Artanis   [Contents]

13.5 Embedded Templating

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 NOT trivial, since the MVC will find the template by detecting controller name automatically. But if you don’t use MVC, say, you just write a simple .scm file for loading GNU Artanis modules. then the extension filename ".tpl" is trivial.

(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 to the same path with your GNU Artanis code.

Because 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 blame that it can’t find variable named external-var.

If you don’t have any external var needs to be referenced, just use (tpl->response "file.tpl") is fine.

Then see http://localhost:3000/test in your browser.