Search code examples
templatesexpressionengine

ExpressionEngine -> Template inclusion(s)


ExpressionEngine template code:

<div class="container_16">
  <div class="grid_16">
{if "{segment_1}" == "home"}
  {embed="include/title" title="welcome"}
{if:elseif "{segment_1}" == "login"}
  {embed="include/title" title="login"}
{if:else}
<?php
  if(isset($_SESSION['loggedin'])) {
    if($_SESSION['loggedin']) {
?>
      {embed="include/title" title="welcome"}
<?php
    } else {            
?>    
      {embed="include/title" title="login"}
<?php 
    }
  }
?>          
{/if}
  </div> <!-- grid_16 -->
</div> <!-- container_16 -->

The problem I'm having with the code above is that when segment_1 is empty, I want it to show the title template with the embed title=login only if the $_SESSION['loggedin'] is not true.

I KNOW that the session variable is not even set so it should show the title template with login but it is not showing anything except this:

<div class="container_16">
  <div class="grid_16">

  </div> <!-- grid_16 -->
</div> <!-- container_16 -->

If I have home as segment_1, it shows the title template with the embed title=welcome.

Is there a better way to do this?


Solution

  • Can you not use {logged_in} and {logged_out} variables?

    Either way add additional conditionals:

    {if "{segment_1}" == "home"}
      {embed="include/title" title="welcome"}
    {if:elseif "{segment_1}" == "login" && logged_out}
      {embed="include/title" title="login"}
    {if:else}
      {if logged_in}
        {embed="include/title" title="welcome"}
      {if:else}
        {embed="include/title" title="login"}
      {/if}
    {/if}
    

    In older versions of EE I've had issues with the if:else not working on the logged_in conditional, so you may just need to replace with:

    {if logged_in}
    {/if}
    {if logged_out}
    {/if}
    

    If you have to use your session variable, ensure PHP is turned on in the template, and parsing is set to before EE. And you'd need to change the conditional slightly:

    {if:elseif "{segment_1}" == "login" && "0" == "<?= (isset($_SESSION['loggedin'])) ? "1" : "0"; ?>"}
    

    Although you might be able to get away with this (you'd need to test to confirm):

    ... && <?= (isset($_SESSION['loggedin'])) ? true : false; ?>}