Michael Slater
posted this on December 19, 2010, 23:30
We've added the ability to define variables in WebvantaScript, and to access their values.
To define a variable, use the following code:
<w:var name="my_variable" value="This string is stored in the variable" />
OR
<w:var name="my_variable">This string is stored in the variable</w:var>
The first form is useful if you want to quickly capture a simple, static value. The double tag form is useful when you want to put more complex data into a variable including the results of other WebvantaScript. The lines between the opening and closing <w:var> tags will be interpreted and the results are captured for you.
You can then insert the value of the variable into your HTML using the self-closing version of the same tag (notice, the absence of the value= attribute):
<w:var name="my_variable" />
In addition, Global Settings automatically behave as variables, so you don't need any w:var statement to use them. For example, if you have a setting named my_setting, you can access that value as {{my_setting}} (as described below) without defining the variable explicitly.
You can access the value of a variable in other WebvantaScript statements using the notation {{my_variable}}
This is especially useful because it allows you to effectively nest WebvantaScript statements, which is not otherwise possible. You can also use variables to pass parameters into snippets.
Here is an example that takes advantage of both of these capabilities. Suppose the following code is stored in the snippet "list_items":
<w:kb:item:each category="{{item_category}}">
<p><w:name /></p>
</w:kb:item:each>
You can then set the item type as part of your code on a page, in a template, or in another snippet, and then list all of the items with that category ("Education" in this example) as follows:
<w:var name="item_category">Education</w:var>
<w:snippet name="list_items" />
Alternatively, you could use the ability to pass a parameter to the snippet using the two-part form of the tag:
<w:snippet name="list_items">Education</w:snippet>
And then set the variable inside the snippet:
<w:var name="item_category"><w:yield /></w:var>
Variables are dependent on the order in which they are appear in your markup. You must define a variable _before_ you use it.
Variables are currently supported only in the following WebvantaScript contexts:
If you have a need to use it in another context, please let us know and we'll see if we can extend it.
Comments
The second example has an error: the closing tag should be </w:var> not </var> as shown. Sadly, this did not fix the case where I wanted to use a variable. Here's what I wanted:
<w:kb:item:each type="LeadStory" by="published_at" limit="{{nLeads}}" >
This is really just a work around, I would prefer that a <w:data> tag could be used instead here. I tried to define the variable with the content of the global setting as follows:
<w:var name="nLeads"><w:data name="nLeadStories" /></w:var>
but sadly it seems that the kb:item:each does not support variables -- Could you add this?
Better yet, could there be a similar notation for global settings?
Thanks,
Jim
Jim,
Thanks for the correction. I've updated the article.
I will submit a request to add support for variables in this case.
Nested WebvantaScript statements are difficult to implement, because WebvantaScript is an XML grammar, and you can't have nested XML statements.
I doing the same thing as the first poster but would like to use it on the type attribute instead of the limit attribute.
Jim, we now support variables in the limit attribute. And we've gone one step further -- Global Settings are now variables by default, so you don't need to define them explicitly. You can simply refer to them as {{setting_name}}.
Arron, unfortunately we do not currently support variables in the type attribute, but I'll add this to the wish list.