0

I have nested if statement for template selection - seems a bit cumbersome is there a better way?

{if member_group == 5 }
            {if last_segment == "edit" }
              {embed="mypassport/personal-profile-edit} 
              {if:else}
               {embed="mypassport/personal-profile-view} 
            {/if}


          {if:elseif member_group ==  6}
          {if last_segment == "edit" }
              {embed="mypassport/business-profile-edit}
              {if:else}
              {embed="mypassport/businessprofileview}
            {/if}
      {/if}
0

You're trying to use variables parsed at two different parsing stages on your code.

If there wasn't need to check member_group, you could use this:

{embed='mypassport/{if last_segment == "edit" }personal-profile-edit{if:else}personal-profile-view{/if}'}

But you can't because, while last_segment is parsed at stage 5, member_groupis parsed at stage 26, even after the embed templates. So all the embeds are being parsed all the times and then being discarded.

Let's try to improve it a little bit. Be aware that you can't to enable page caching on this or members of different groups will see each others info.

{exp:switchee variable = "global:member_group" parse="inward"}
    {case value="5"}
        {embed='mypassport/{if last_segment == "edit" }personal-profile-edit{if:else}personal-profile-view{/if}'}
    {/case}
    {case value="6"}
        {embed='mypassport/{if last_segment == "edit" }business-profile-edit{if:else}business-profile-view{/if}'}
    {/case}
{/exp:switchee}

If member_group wasn't a global variable, which needs the global prefix to be parsed early, we could test for both variables at once.


This is an interesting idea. I gonna suggest it to the developer of switchee, Mark Croxton.

1
  • this looks great - first EE project so the parse order something that is tripping me up a bit.
    – LeBlaireau
    Mar 24 '14 at 19:20
1

It's certainly going to be more resource heavy that way, to be sure. One thing that could definitely help would be to use the early parsing conditional add-ons, switchee and ifelse to evaluate the conditionals. Your outer condition is a good spot for switch case logic, while your interior could use ifelse:

{exp:switchee variable="{member_group}" parse="inward"}
  {case value="5"}
     {exp:ifelse parse="inward"}
       {if last_segment == "edit" }
          {embed="mypassport/personal-profile-edit} 
       {if:else}
          {embed="mypassport/personal-profile-view} 
       {/if}
     {/exp:ifelse}
  {/case}
  {case value="6"}
       {if last_segment == "edit" }
          {embed="mypassport/business-profile-edit}
       {if:else}
          {embed="mypassport/businessprofileview}
       {/if}
  {/case}
{/exp:switchee}

Another alternative would be to combine some of your conditionals. Rather than evaluate the member group as a wrapper and then have conditionals inside them - since they appear to exist in exclusion of one another - you could do something like:

  {if member_group == "5" AND last_segment == "edit"}
      {embed="mypassport/personal-profile-edit}
  {if:elseif member_group == "5" and last_segment != "edit"}
      {embed="mypassport/personal-profile-view}
  {if:elseif member_group == "6" AND last_segment == "edit"}
      {embed="mypassport/business-profile-edit}
  {if:elseif member_group == "6" AND last_segment != "edit"}
      {embed="mypassport/businessprofileview}
  {if:else}
  {/if}

But I might still recommend using the ifelse add-on to ensure the conditional parses as early and efficiently as possible. Also, if you can get away without an embed and use a snippet instead, I'd recommend doing so - another efficiency gain to be realized if that's an option for you.

Hope that helps.

0
0

I'll demonstrate the Resource Router way of handling this.

$config['resource_router'] = array(
    'mypassport' => function ($router) {
        switch(ee()->session->userdata('group_id'))
        {
            case 5:
                $router->setTemplate('mypassport/personal-profile-view');
                break;
            case 6:
                $router->setTemplate('mypassport/business-profile-view');
                break;
            default:
                $router->redirect('login');
        }
    },
    'mypassport/edit' => function ($router) {
        switch(ee()->session->userdata('group_id'))
        {
            case 5:
                $router->setTemplate('mypassport/personal-profile-edit');
                break;
            case 6:
                $router->setTemplate('mypassport/business-profile-edit');
                break;
            default:
                $router->redirect('login');
        }
    },
);
2
  • this would be my preferred option - is this part of core as of v2.8?
    – LeBlaireau
    Mar 24 '14 at 19:23
  • No, the core Template Routes would not allow you to do member group based routes. Mar 24 '14 at 20:32
0

If your last segment is either "edit" or "view" it would be best to do this:

  {if member_group == "5"}
      {embed="mypassport/personal-profile-{last_segment}"}
  {if:elseif member_group == "6"}
      {embed="mypassport/business-profile-{last_segment}"}
  {if:else}
      No results template
  {/if}
3
  • For one moment, I believed your answer is awesome, but then I remembered of some times the logic of trust the user broke my plans.
    – Sobral
    Mar 24 '14 at 22:36
  • Not sure I follow you... you are using your "logic of trust" in your solution by testing if the last segment is "edit" show the profile edit view and else show the profile view template. Mar 25 '14 at 14:06
  • In my solution, there's a fallback page, profile-view, at least. But neither yours or mine cover all possibilities.
    – Sobral
    Mar 25 '14 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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