Life, Code & Idiocy

Bloggage of a web coding nutcase

7 Jul 2008

At long last, hiding sidebar blocks

One of the most popular requested features in Enano that I was never able to implement efficiently has just gone in – in under 1KB of code. This feature is hiding certain sidebar blocks except for certain users or groups.

A couple of days ago I rewrote the part of template::tplWikiFormat() that parses the conditional blocks ({if foo…}). The code was inefficient and the parser took forever to validate the code. The new parser does a simpler set of checks that should be just as thorough. But the advantage to the new parser is that the inner function that processes the actual expression (“foo…” in the example above) is a separate function. This led me to wonder, what if I used the same function to allow conditional hiding of the whole block?

It turned out to not be a bad idea, so I added two new tag types into the parser: {restrict} and {hideif}. The first hides the block unless the condition is true, and the second only removes the block if the condition is true. This allows for something like:

{restrict auth_admin}

Putting this into, for example, the Tools sidebar block will hide it from everyone except administrators – a helpful feature and one that people have been asking for for a long time. It’s a bit rudimentary, but I think it gives you more power than you’d get with a simple “check the box next to the groups that will see this block.” Another example would be an advertisement block that is only shown to Internet Explorer users that aren’t logged in:

{restrict msie AND !user_logged_in}

The template parser will also set a boolean value “theme_is_<current theme>” to true now, so you can hide blocks for certain themes. Let’s say your theme has its own search bar. You would add this to the Search block:

{hideif theme_is_leonano}

I’d like to make the feature a little more user-friendly, but as of right now you can add {restrict} and {hideif} checks to any sidebar block of any type and achieve the hiding effect, giving you a bit more power than you’d get with a typical GUI editor. Using the same function call also means I can extend it later, letting you use logic such as checking for membership of a certain group, checking the user level, and more. That might not be implemented for a while, but when and if it is it will be a huge increase in power to Enano’s currently barely-holding-together sidebar framework.

No Responses to “At long last, hiding sidebar blocks” (post new)

 

Leave a Reply