Arc Forumnew | comments | leaders | submitlogin
Super Simple Document Templates and List to <LI>
1 point by coconutrandom 5617 days ago | 5 comments
This document temple "system" is so trivial I hesitated to post it. Uses 'atstrings.

And the <li> generator is something that just came up.

  ;== html/test.html ==
  ;<html>
  ;  <body>@(header)</body>
  ;</html>
  ;== html/top.html ==
  ;<h1>@( companyname )</h1>
  
  ;(def companyname () "Acme Co")
  
  ;(def header () 
  ;  (render "html/top.html"))
  
  ;arc> (render "html/test.html")
  ;"<html>\n  <body><h1>Acme Co</h1>\n\n</html>\n"
  
  (declare 'atstrings t)
  
  (def render (file)
    (eval (filechars file)))
  
  ;<li> for use in <ul> or <ol> 
  ;gentag prs all over the place, need to build strings, eval, THEN print
  
  (def li (l)
    (string 
      (mappend [if (is (type _) 'table)
                  (list 
                      "<li " 
                      (aif (_ 'class) (string "class=\"" it "\""))
                      (aif (_ 'id) (string "id=\"" it "\""))
                      ">" 
                      (_ 'body)
                       "</li>")
                  (list 
                      "<li>"
                      (string _)
                       "</li>")
               ] l )))
  
  ;usage
  ;arc> (li `(,(obj body "hi" id "brown") ,(obj body "world" class "green") "foo"))
  ;"<li id=\"brown\">hi</li><li class=\"green\">world</li><li>foo</li>"


1 point by coconutrandom 5617 days ago | link

Any thoughts or suggestions?

-----

2 points by fallintothis 5617 days ago | link

The 'li function could be simplified. html.arc has the facilities to define tags; coupled with 'tostring, we can capture the output, giving something like:

  (attribute li class opstring)
  (attribute li id    opstring)
  
  (def li items
    (tostring
      (each i items
        (if (isa i 'table)
            (tag (li class i!class id i!id) (pr i!body))
            (tag li (pr i))))))
This also uses a rest parameter so that 'li can take any number of arguments and collect them up into a list:

  arc> (li (obj body "hi" id "brown") (obj body "world" class "green") "foo")
  "<li id=\"brown\">hi</li><li class=\"green\">world</li><li>foo</li>"
Though it doesn't quite replicate the use of 'string you had originally, e.g.,

  arc> (li ''(a b c))        ; the one defined above
  "<li>(quote (a b c))</li>"
  arc> (li (list ''(a b c))) ; yours
  "<li>quoteabc</li>"
You note that 'gentag prints all over the place. I'm sure (because of the game you built) you notice that Arc uses printing in its html-generation tools. So if you wanted to use this 'li function inside, say, a 'defop, you probably don't want to wrap it in the 'tostring -- instead, let it print so that you don't have to make the call to 'pr yourself.

-----

1 point by coconutrandom 5612 days ago | link

Thanks for the info, didn't know the 'tostring did that!

The reason I didn't want it to pr at the moment was the output from this or other functions could be assembled to make a html template system.

-----

2 points by conanite 5617 days ago | link

I don't see how to inject request-scoped variables - it seems you can only call functions in the global namespace. In other words, how would you render

  <span>hello, @(current-user)</span>
for multiple concurrent users?

A separate, and totally subjective, observation: one advantage of having prs all over the place is that output gets sent directly to the client, rather than getting buffered on the server (caveat: depending on how the stream is implemented I suppose). Writing to a string and then printing that string might be more resource-intensive; only an issue for high-volume sites though. The big win I've had with prs all over the place is that when something goes wrong (and arc isn't always very helpful when something goes wrong), you can "view source" in your browser and see where output stopped, and then hopefully make a guess as to where the error is in your code.

-----

1 point by coconutrandom 5612 days ago | link

True and true, current-user would have to be passed to a function to do something with it.

Interesting about buffering the output. Lots to learn still.

-----