Some ClearSilver Questions/Examples

  1. How do I access multiple form values from ClearSilver?
  2. How do I walk a tree in ClearSilver?
1. How do I access multiple form values from ClearSilver?
If you have the following in your HTML Form:

     <select name=myselect size=5 multple>
       <option value=foo>Foo</option>
       <option value=bar>Bar</option>
       <option value=biff>Biff</option>
Single values would off course show up in HDF as Query.myselect. When there are multiple values, the ClearSilver CGI Kit will update the value of Query.myselect with the latest one it finds, and start a sub list of all the values. This means the HDF will look like the following:

        1 value => Query.myselect = value

        2 values => Query.myselect = second value
                    Query.myselect.0 = first value
                    Query.myselect.1 = second value

        3 values => Query.myselect = third value
                    Query.myselect.0 = first value
                    Query.myselect.1 = second value
                    Query.myselect.2 = third value
From python, one could use:

    def allQuery (hdf, s):   
        l = []
        if hdf.getValue ("Query.%s.0" % s, ""):
          obj = hdf.getChild ("Query.%s" % s)
          while obj:
            obj = obj.next()
          t = hdf.getValue ("Query.%s" % s, "")
          if t: l.append(t)
        return l
From CS, one could use:

    <?cs if:Query.myselect.0 ?>
      myselect =
      <?cs set:first = #1 ?>
      <?cs each:myselect = Query.myselect ?>
         <?cs if:#first ?><?cs set:first = #0 ?><?cs else ?>, <?cs /if ?>
         <?cs var:myselect ?>
      <?cs /each ?>
    <?cs else ?>
        myselect = <?cs var:Query.myselect ?>
    <?cs /if ?>
2. How do I walk a tree in ClearSilver?
We're going to assume that you have more than two levels in your tree. Because of this, you are going to have to write a recursive macro. First, your HDF is going to look something like the following:

    Files.0.Name = Desktop
    Files.0.Type = dir
    Files.0.Sub.0.Name = Bookmarks.html
    Files.0.Sub.0.Type = file
    Files.0.Sub.1.Name = History.txt
    Files.0.Sub.1.Type = file
    Files.0.Sub.2.Name = Resume.doc
    Files.0.Sub.2.Type = file
    Files.1.Name = .cshrc
    Files.1.Type = file
    Files.2.Name = .login
    Files.2.Type = file
    Files.2.Name = Mail
    Files.2.Type = dir
    Files.2.Sub.0.Name = inbox
    Files.2.Sub.0.Type = file
    Files.2.Sub.1.Name = received
    Files.2.Sub.1.Type = file
    Files.2.Sub.2.Name = sent
    Files.2.Sub.2.Type = file
    Files.2.Sub.2.Name = postponed
    Files.2.Sub.2.Type = file
Ok, first the simple version:

      <?cs def:display_files(files) ?>
         <?cs each:file = files ?>
           <li><?cs var:file.Name ?></li>
           <?cs if:file.Sub.0.Name ?>
             <?cs call:display_files(file.Sub) ?>
           <?cs /if ?>
         <?cs /each ?>
      <?cs /def ?>

      <?cs call:display_files(Files) ?>
This version relies on HTML to format your list. Notice how we check both the existance of the sub element before we recurse, because otherwise we'll get an extra ul pair, which in most browsers will cause extra whitespace. Perhaps you don't want to use the HTML like this, for spacing or other issues. You could always pass an string that grows down the recurse that provides the indention:

      <?cs def:display_files(files, spc) ?>
         <?cs each:file = files ?>
           <?cs var:spc ?><?cs var:file.Name ?><br>
           <?cs if:file.Sub.0.Name ?>
             <?cs call:display_files(file.Sub, spc + "&nbsp;") ?>
           <?cs /if ?>
         <?cs /each ?>
      <?cs /def ?>

      <?cs call:display_files(Files, "") ?>
This version keeps growing a string of non-breakable spaces to place in front of the file name, which makes for a slightly indented view. You could then add something which checked the type and issued and icon between the spaces and the name, etc.