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

11.5 Embedded Templating

Example: Write a tpl file named "my.tpl":

  <p> <%= "This is tpl test!" %> </p>
  <p> <% (format #t "And this is ~a" (getcwd)) %> </p>
  <p> <%= external-var %> </p>

The filename extension ".tpl" - for template - is arbitrary, you can name it whatever you like.

(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.