On one condition
| DiscussionI had some time to kill, so like any ExpressionEngine developer would do I grabbed the EE source code and spent some time digging through it. What I found is something super secret nobody is supposed to know about. Luckily for you I am very bad at keeping secrets, so here goes.
In the Template parser there is this little feature that is available when processing advanced conditionals. Usually when you want to check if a member belongs to a certain group, you would do something like:
{if logged_in_member_group == 1}
// Show something super secret only VIP member are allowed to see.
{/if}
This is fine if you only need to check for one member_group, but as soon as you have multiple, things get crazy. You end up with a bunch of {if}'s and {else}'s or writing a custom plugin if you want to keep your template somewhat simple. But the folk over at EllisLab have added something to the template parser that they've kept quiet... a special conditional solely for doing this exact thing.
This is the little piece of code that can make a huge difference in keeping your template code clean:
// /system/expressionengine/libraries/Template.php (line 3030)
// Member Group FALSE function, Super Secret! Shhhhh!
if (preg_match_all("/in_group\(([^\)]+)\)/", $str, $matches))
{
$groups = (is_array($this->EE->session->userdata['group_id'])) ? $this->EE->session->userdata['group_id'] : array($this->EE->session->userdata['group_id']);
for($i=0, $s=count($matches[0]); $i < $s; ++$i)
{
$check = explode('|', str_replace(array('"', "'"), '', $matches[1][$i]));
$str = str_replace($matches[0][$i], (count(array_intersect($check, $groups)) > 0) ? 'TRUE' : 'FALSE', $str);
}
}
What this basically allows you to do is the following:
{if in_group('1|7|8')}
<p><q>You shall not p... oh, you passed.</q></p>
{/if}
As you can see this cuts down on template code considerably, but what intrigues me the most is the syntax. I've never even seen anything like this. Not in native add-ons or third-party add-ons, so where did it come from and when was this added?




