0

Is there a better way to write this?

{if logged_in_group_id == "1" || logged_in_group_id == "6" || logged_in_group_id == "7" || logged_in_group_id == "11"}
Hello Members...
{/if}

It seems very long.

Version 2.9.0

2 Answers 2

1

To add to Blatant's comprehensive answer and keeping native without PHP...

{if "|1|6|7|11|" *= "|".logged_in_group_id."|"} ... {/if}

Advanced Conditionals

6
  • Hi Peter, I would like to keep it native, would it be appropriate to add a snippet with the group_id's eg: {if "{snippet}" *= "|".logged_in_group_id."|"} ... {/if} Commented Feb 23, 2016 at 12:47
  • 1
    Yes, that should work as snippets are added before conditionals: docs.expressionengine.com/latest/templates/template_engine.html Commented Feb 23, 2016 at 12:49
  • Thank you, will this advanced conditional work in ee2 and what are the benefits of it over my original syntax? I am just trying to better understand, thanks. Commented Feb 23, 2016 at 12:51
  • It will only work in EE2.9 or upwards (Ref: ellislab.com/blog/entry/new-conditionals-parser-in-2.9). The only benefits from your original is the length and ease of understanding the conditional. There may be fractional performance issues with my suggestion, but I doubt these would be measurable. Commented Feb 23, 2016 at 12:56
  • Brilliant, I am on 2.9.0, thanks for help. Commented Feb 23, 2016 at 13:01
0

Well, you don't say what version of EE you're using, so I'm gonna assume v2, though this solution should work just fine in v3 too.

the quickest method would be PHP's in_array() function, which you can use in your template natively, however so that I don't give a PHP answer first here's a plugin that adds in_array to EE :

https://devot-ee.com/add-ons/in-array

{if '{exp:in_array value=“2” array="1|6|7|11"}'}
  Hello Members...
{/if}

If you rather add plugins that should get you there, you can shorten your EE tags as well, as group_id will be 0 for non logged in members :

{if group_id == '1' || group_id == '6' || group_id == '7' || group_id == '11'}
  Hello Members...
{/if}

Or if you're not afraid of a bit o PHP and the parse order :

EE v2.X

<?php if ( in_array( ee()->session->userdata('group_id') , array ( 1, 6, 7, 11 ) ) ) { ?>
  Hello Members...
<?php } ?>

EE v3.X

<?php 
  global $SESS; 
  if ( in_array( $SESS->userdata('group_id') , array ( 1, 6, 7, 11 ) ) ) { 
?>
    Hello Members...
<?php } ?>
1
  • thank you for in-depth answer Blatant, this has definitely given me some options Commented Feb 23, 2016 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.