# CL-EMB: Examples

# Some examples of CL-EMB usage

1. Combining CL-EMB with CL-WHO
2. A simple loop
3. Build a dropdown
4. Mark invalid form fields
5. Using generic templates

Combining CL-EMB with CL-WHO
Description | You can mix several methods of HTML generating together. Think of Lisp Markup Languages like CL-WHO. (Example code from the CL-WHO documentation.)
ENV | `NIL`
Dependencies | CL-WHO

    <h1>Music links</h1>
    <%
    (cl-who:with-html-output (*standard-output*)
      (loop for (link . title) in
            '(("http://zappa.com/" . "Frank Zappa")
              ("http://marcusmiller.com/" . "Marcus Miller")
              ("http://www.milesdavis.com/" . "Miles Davis"))
            do (cl-who:htm (:a :href link
                               (:b (cl-who:str title)))
                           :br)))
    %>

A simple loop
Description | The "Music links" example with template tags and a loop. This example isn't meant to prove anything! Use the method which fits your problem! The output of the title gets escaped by CL-EMB ("-escape html"). Depending on the situation you'd rather escape the output yourself and don't want to use any complicated modifiers in the template code itself.

ENV

    '(:music-list
      ((:link "http://zappa.com/" :title "Frank Zappa")
       (:link "http://marcusmiller.com/" :title "Marcus Miller")
       (:link "http://www.milesdavis.com/" :title "Miles Davis")))

Dependencies | -

    <h1>Music links</h1>
    <% @loop music-list %>
    <a href="<% @var link %>"><b><% @var title -escape html%></b></a><br />
    <% @endloop %>

Build a dropdown

Description

You can mix template style with embedded Common Lisp style. This example shows how to access the plist ENV. Within the loop (`@loop`) ENV gets bound to every plist in the list.
TBNL is used to access a submitted parameter "product" and compare it to the current value attribute of the option element.
Remember the escaping! Set `cl-emb:*escape-type*` to `:html` and all output of `@var` will be escaped correctly.

ENV

    '(:products
      ((:value "foo1" :text "Super Foo")
       (:value "fooxl" :text "Super Foo XL")
       (:value "bar2000" :text "Ultra Bar 2000")
       (:value "hl2" :text "Half-Life 2")
       (:value "dn4e4" :text "Vaporware")))

Dependencies | TBNL

    <select name="product">
      <% @loop products %>
        <option value="<% @var value %>"<%
          (when (equal (getf env :value) (tbnl:parameter "product"))
        %> selected="selected"<% ) %>><% @var text %></option>
      <% @endloop %>
    </select>

Mark invalid form fields
Description | Validate a form and mark the errors in the ENV plist. Again: Remember the escaping!

ENV

    '(:email "stesch@home" :email-error t)

Dependencies | -

    <% @if email-error %>
      <span class="error">Please provide valid e-mail address</span><br />
    <% @endif %>
    <input type="text" name="email" value="<% @var email %>"/>

Using generic templates
Description | You want to use generic templates which can be called with a defined set of parameters? Then `@with` and `@endwith` is what you are looking for. It sets the current ENV to the one accessed by a given name. See the example below, which calls a template for textinput fields.

ENV

    '(:name (:name "name"
             :length 40)
      :e-mail (:name "email"
               :value "no@no"
               :error t
               :length 120))

Dependencies | -

    Please enter your name:<br />
    <% @with name %>
      <% @include "includes/textinput.tmpl" %>
    <% @endwith %>
    <br />
    Please enter your e-mail address:<br />
    <small>(Use the TLD <em>.invalid</em>
    if you don't want to receive mail</small>
    <% @with e-mail %>
      <% @include "includes/textinput.tmpl" %>
    <% @endwith %>