Monday, April 02

Blog

Tag Soup

So what's new for today?

Conditions, calculations and variables!

Minx already had four conditional tags, namely [if], [ifn], [iff], and [iffn].

[if] tests a single variable. If that variable exists and has a true (non-zero / non-null / non-empty) value, the template code within the bracket of the [if] and the [/if] is evaluated.

If the variable is false (zero / null / empty), the code is not evaluated.

And if the variable does not exist, the [if] tag itself is not evaluated, and is instead included directly in the output. So if you get the name of the variable wrong, and type [if post.coments] instead of [if post.comments], you will be able to see your mistake right there in the page. In this case, the bracketed code is processed as if the [if] tag did not exist.

[ifn] works exactly the same, except that the condition is reversed. Code inside the [ifn] [/ifn] is evaluated if the variable is false.

[iff] and [iffn] are similar, with one big exception: The bracketed code is evaluated if and only if the variable evaluates to true (or false, for [iffn]). If the variable does not exist, the code is skipped, just as if the variable had been false.

Comparison Tags

[if.eq], [if.ne], [if.gt], [if.lt], [if.ge], [if.le]

These extended conditional tags compare two values, to see if, respectively, they are equal or unequal, or that the first value is greater than, less than, greater than or equal, or less than or equal to the second value. They must be closed by a corresponding [/if.XX] tag such as [/if.eq], not by a [/if].

The values can be either variables or constants. The way Minx determines which is the case is somewhat simplistic: If a variable exists which has the name of the value, it assumes that's what you mean; if not, then it assumes the value is a constant. So unlike the simple [if] tag, there's never a case where the variable doesn't exist.

These tags will perform integer comparisons if both values are integers; otherwise they will perform string comparisons. So [if.gt 10 2] will evaluate as true, just as you'd expect, and [if.gt a10 a2] will evaluate as false.

[I had held off on implementing these for the sake of keeping the template language simple, but it turns out I need them to properly manage context-sensitive selection lists - such as on my new forum pages - so in they went.]

Variables and Calculations

[set]

You can set a variable with the [set] tag. Like the extended conditionals, [set] can take either another variable or a constant, so [set a post.title] or [set a "Wombats in Paradise"] are valid examples. Again like the extended conditionals, [set] assumes a variable if the variable name exists, and a constant otherwise.

The variable can be accessed again with the prefix var. So [var.a] will print the value of a, and [if var.a] will test it. You can use the var.a terminology inside set as well, and it will be interpreted appropriately.

Variables can be used anywhere a normal data tag could be used.

[calc.add], [calc.sub], [calc.mul], [calc.div], [calc.mod], [calc.min], [calc.max]

The [calc] tag performs a calculation based on two values, and assigns the result to a variable. The values must be integer variables or constants; anything else will be evaluated as zero.

So [calc.add x var.y var.z] will set the variable x to be the sum of y and z.

[inc], [dec]

Just want to increment or decrement a counter? These are the tags for you! [inc a] or [dec a] and you're done.

If the variable did not exist beforehand, it is created as zero, and then incremented or decremented.

Note that none of these tags can alter the value of an existing data tag, only user variables.

Posted by: Pixy Misa at 12:37 AM | Comments (5) | Add Comment | Trackbacks (Suck)
Post contains 546 words, total size 6 kb.

1 Interesting. The bbcode parser was spending about 5ms on that post, even though none of the tags are valid bbcode. That's about 100μs per [, which seems a bit excessive.

Posted by: Pixy Misa at Monday, April 02 2007 02:20 PM (PiXy!)

2 Thank heavens!  I've been all a-twitter without my "Wombats in Paradise".

Posted by: Ted at Monday, April 02 2007 07:21 PM (+OVgL)

3 Pixy, look at the fizz_buzz entry. It's SCREWED. Following the
url ends in a page which just has a bunch of broken text with "Fizz" and "Buzz". It's not just in the feed: look were "comments" link is pointing off the main page.

Posted by: Pete Zaitcev at Tuesday, April 03 2007 02:48 AM (9imyF)

4 Oops.  The demo of the FizzBuzz program superseded the FizzBuzz individual entry page.  I've fixed this by changing the URL for the demo folder. (That broken text is the output of the program.)

Posted by: Pixy Misa at Tuesday, April 03 2007 03:01 AM (PiXy!)

5 To explain that a bit more clearly: The "screwed" page you were seeing was the output of a program written in the Minx Template Language.  I'd accidentally attached that program to the same URL as the page for that entry, and the way Minx follows its directory tree, it will always find the program first.

Posted by: Pixy Misa at Tuesday, April 03 2007 07:41 AM (PiXy!)

Hide Comments | Add Comment

Comments are disabled. Post is locked.
53kb generated in CPU 0.0169, elapsed 0.1356 seconds.
56 queries taking 0.1249 seconds, 362 records returned.
Powered by Minx 1.1.6c-pink.