I'll do it, on one condition
Sometimes, you'll want parts of a page to appear only sometimes, or when certain criteria is met. For this, AutoSite includes conditionals that can check a variable and render out that part of the page only if the condition is met.
Conditional text, like variables, can be used either in a template (where it will check the variable on the relevant In page) or on the In page itself.
Conditionals, like HTML tags, come in pairs. The first tag sets the variable to check, and the second pair features a forward slash (
/) to signify the end of the conditional.
[path=conditionals/index.md]<div id="current">Conditional Text</div>[/path=]
This <div> will only appear if the In page is located at
conditionals/index.md. If it's not, it simply doesn't appear.
You can also have a conditional check if a variable is not true, by placing an exclamation point (
!) before the equals sign (
=) in both tags.
[path!=conditionals/index.md]<div><a href="[#root#]conditionals/">Conditional Text</a></div>[/path!=]
This text will only appear if the In page is not located at
Conditionals don't have to check just file paths; any variable can be checked with a conditional. For instance, if we had a variable for the type of animal the page is about (say,
bird), we could check that variable and load a different stylesheet depending on the type of animal.
<!-- attrib animaltype: mammal --> [animaltype=mammal]<link rel="stylesheet" href="mammal.css">[/animaltype=] [animaltype=lizard]<link rel="stylesheet" href="lizard.css">[/animaltype=] [animaltype=bird]<link rel="stylesheet" href="bird.css">[/animaltype=]
In this example, only the
mammal stylesheet would get rendered on this page.
There's still a few issues with AutoSite's implementation of conditionals (blame regex): you can't nest them, and you can only have one condition per line. Despite this, it's an incredibly powerful feature that's used all over this site.