Search code examples
language-designlanguage-featuresscripting-language

How to use [function] in webdna


I have been spending hours digging in Govinda great job in WebdnaCodeSparker.

I must confess I have never been using the [function] and I now wondering how to use it... I went thought the doc, it clear to me how to send variable to a function that will execute it.

But How to use function with a search contexts? exemple :

I put the following code in a [function]

[search mydb.db&neSKUdata:FINDALL] [founditems][/founditems] [/search]

And I want to get the result in

[SKU]-[name]

Maybe function is not meant to do this kind of action?


Solution

  • I would say that if you do not know you want something (like a [function]), then better to avoid adding complexity just because you think you are supposed to do it with complexity). ... like you could just put your [search] inline in your code.

    But for when you are ready, there ARE several great reasons to use [function] in webdna, .. at least these are some reasons:

    1. to make your code easier to read

    2. to make your code easier to maintain

    3. to remove unwanted white space from your code

    4. to make your code easier to write
      (once you have done the extra work to set up a bunch of functions to do various tasks, in a complicated site, then writing new code to use those functions is much faster/easier! You just call the functions!)

    You can think of function like an [include].. it effectively lets you put a bunch of code at one spot in your page.. with just one short line (the function call). I like to use functions like that... as an include... where I often put the function definition even in the same file (if that function's code is unique to just that page), ...but I put the function definition up at the top of the page, out of the way, and NOT in the spot where the call to the function is, where it would make my code harder to read.

    It is also good to use functions as a way to do the same thing over and over again (or doing something similar over and over again) without writing the same code over and over. That makes your code easier to maintain. If there is some change required later (like the client now wants an ordered list (<ol>) instead of an un-ordered list (<ul>)) ... then you only have to change it in one place (in the function definition), instead of in all the places where the function is called.

    Because [function] only outputs the code that is in between [return]...[/return] you can do all your calculations inside the [function...]...[/function] but outside any [return]...[/return].. and then just output the final results that you do want output.. by putting that final result inside [return]...[/return].

    [function name=finalResult]
     [math]a=0[/math]
    
     this 
     is 
     to
     mimic
     a
     big
     super
     long
     block
     of
     code
    
       [text]finalResult=
         [math]a=[a]+1[/math]
       [/text]
    
     with
     tons
     of
     white
     space
     where
     we
     do
     lots
     of
     calculations
    
     [return][finalResult][/return]
    [/function]
    

    To give an example specific of how you might use a function call to return the results of a search, so that the database can change, but the results are always formatted the same, you might do it like this:

    [function name=getResults]
     [search db=[whichDb].db&neSKUdata=[blank]]
       [founditems]
         [return]<div class"myclass">[SKU]-[name]</div>[/return]
       [/founditems]
       [showif [numfound]=0]
         [return]Nothing to see here[/return]
       [/showif]
     [/search]
    [/function]
    

    and on your page

    <div class="resultsContainer">
     [getResults whichDb=myDb]
    </div>
    

    and again somewhere else

    <div class="resultsContainer">
     [getResults whichDb=anotherDb]
    </div>