[All Adaptavist Apps]
Flags allow you simplify switching logic and improve performance within your themes...
Note: For best results we recommend using Theme Builder 3.3.6 or later as many more macros were made flag-aware.
A flag is a simple named register that stores a state:
true- The flag is set
false- The flag is not set
If you use a flag that's not been defined, it's treated as having state
Several macros are flag-aware, allowing you to toggle them on or off based on the state of one or more flags. You can also use flags to toggle the effect of parts of your CSS style sheets.
Flags are cleared after every page view so they are unique to the current page that the current user is viewing.
To set, and subsequently un-set a flag, you use the set-flag macro, for example the following markup will set a flag called foo, and then un-set it:
You can set flags in a wiki page or in a theme panel.
In most cases, you'll want to conditionally set a flag based on some other criteria, for example:
The markup above would set the 'in-the-forum' flag only when the user is looking a page called 'Forum' or any of it's child pages (due to the 'recurse=true').
The macros most commonly used to set and un-set flags in this manner are:
These show/hide macros will show or hide their contents based on the parameters you set, allowing you to determine when the set-flag macro is triggered to set or un-set a flag.
To un-set (clear) a flag, set it's state to false:
You can also completely remove a flag by setting remove:
You can use flags to toggle the rendering of other macros and wiki markup. For example, using our 'in-the-forum' flag above we could show some extra content in a theme sidebar when that flag is set:
Or we could show some content when the flag is not set:
The following macros have
notflag parameters (requires Builder 3.3.6 or above):
The flag parameters are as follows:
flag– defines which flags must be set in order for the macro to be processed
notflag– defines which flags must not be set in order for the macro to be processed.
Flags become useful in more complex theme customisations because they allow you to separate out a lot of the logic that determines what should be displayed to the end-user. We'll cover some of the key use cases below...
There are many cases where you'll want to set a default state and then override it with a known state based on various criteria. If none of the known-states are found, you'll be left with the default state.
A simple example is determining whether a user is logged in or not, and whether they are a member of staff:
The result will be:
Important: Flags should never be treated as a security mechanism. In the example above, a user could set the staff-user flag from a wiki page. You could override any flags set by users by defining some more defaults at the start of your flag logic, for example:
There are lots of times when a state will be used several times in a theme layout. Although lots of caching is used in Builder and Confluence, if you're repeatedly checking for a specific state (particularly complex states based on several criteria) it can degrade performance and make your theme config really messy.
One of the most common scenarios we've found so far in themes we've developed for customers is whether or not to show an edit button and other links related to page editing. The general logic runs along these lines:
There's obviously quite a few things that need to be checked to confirm whether the edit features should be shown, and in many cases the outcome will be performed in several locations and across multiple theme panels. An ideal use case for flags!
Based on the flags set by earlier examples, the logic to do this would be:
You can now toggle the edit links anywhere in your theme based on the 'show-edit' flag. This way you don't need to keep checking where you are in a space (top level pages, home page, documentation section) and the type of user (registered or staff).
Any developer looking at the flag logic above will see several optimisations that can be made - this is key reason for separating out the flag logic in to a wiki page so it's easier to modify...
We generally recommend creating a wiki page to store the flag logic for the following reasons:
Obviously, the page will need to be available to users (otherwise the theme won't be able to import it) so we normally put it in a space that anonymous users can see but not edit.
Once you've made the page, you can use the import macro to import it in to a theme panel and render it in the context of the page the end-user is looking at. Don't use the 'include' macro as that would render it in the context of the flag logic page and not the page being viewed.
We recommend importing the flags in to the Header panel - it's the first panel to be rendered and thus the flags will be available to all panels in the theme layout.
If you can't store flag logic on a wiki page, you could put it in to a user macro (in Confluence administration console) - this will make it available to all users regardless of whether they are logged in.
When a flag is set, a class will be added to the HTML <body> tag. Let's take the 'in-the-forum' flag we described earlier as an example:
As you can see, the class is prefixed with 'flag-' so remember that when you're using it in your style sheet.
So, let's say you want to change the colour of the heading 1 style when you're in a forum, you could use this CSS in the CSS Tab: