2

I created a module for GitHub's API and am having trouble preventing the module's function {exp:cl_github:repo_contents_archive} from running given a conditional. The reason this is imperative is that the function actually sends a file to the browser (sets headers, echos, and dies).

{if logged_out}{redirect="member/login"}{/if}

{exp:channel:entries require_entry="yes" parse="inward"}
    {addon_github}
        {exp:cl_github:repo_contents_archive 
            owner="{owner}" 
            repo="{repo}" 
            ref="{ref}"}
    {/addon_github}
{/exp:channel:entries}

I've tried a few things like putting {if logged_out}limit="0"{/if} in the {exp:channel:entries} tag.

Another solution I thought of was to pass into my {exp:cl_github:repo_contents_archive} a parameter like is_authenticated="{logged_in}" but for some reason that wouldn't parse the value of {logged_in}.

I don't really want to hardcode checking for logged_in in the module itself. Can you guys/gals think of any other workaround or something I am missing?

2

If you look at the Template Parsing Order, you'll see that the redirect, stage 20, is parsed after the modules, stage 15. So, let's try to prevent the parsing of exp:channel:entries.

I thought you could try this:

{if logged_out}{redirect="member/login"}{/if}

{if logged_in}
{exp:channel:entries require_entry="yes" parse="inward"}
    {addon_github}
        {exp:cl_github:repo_contents_archive 
            owner="{owner}" 
            repo="{repo}" 
            ref="{ref}"}
    {/addon_github}
{/exp:channel:entries}
{/if}

But, your tests proved that {if logged_in} and {if logged_out} are advanced conditionals. I didn't realize this until to read your comment. This makes a lot sense, since their parsing need to be done after the page caching, to avoid logged_out users to see a logged_in cached page.

There's a way to use global variables, that make all conditionals to be advanced, on early parsing stages: using switchee!

Please, try this:

{exp:switchee variable = "global:logged_in_member_id" parse="inward"}
    {case value="0"}
        {redirect="member/login"}   
    {/case}
    {case default="yes"}
        {exp:channel:entries require_entry="yes" parse="inward"}
            {addon_github}
                {exp:cl_github:repo_contents_archive 
                owner="{owner}" 
                repo="{repo}" 
                ref="{ref}"}
            {/addon_github}
        {/exp:channel:entries}
    {/case}
{/exp:switchee}

The prefix global: allow us to access the global variables that, otherwise, aren't available until the later stages of parsing.

6
  • I tried this and it didn't work but I actually don't think that parse order is the issue. Instead I think EE parses everything and then decides what to do with the output of each tag later. I can sort of prove this by making the module tag output a string instead of literally "echoing" the file directly which I have to do to get this module to work. – spacesuitdiver Sep 3 '13 at 15:48
  • Your test just told us that "if logged_in" and "if logged_out" are advanced conditionals. – Robson Sobral Sep 3 '13 at 15:59
  • I updated my answer. – Robson Sobral Sep 3 '13 at 16:38
  • Ah you are right about the advanced conditional, I tried a {if TRUE} / {if FALSE} and that worked as expected. Let me give this a try. – spacesuitdiver Sep 3 '13 at 16:41
  • Using switchee didn't fix the problem as it still parses the module tag. The solution is to use something like mo_variables it seems. Thanks for pointing me on the correct path, my solution is below :) – spacesuitdiver Sep 3 '13 at 17:59
1

Something like this works with the mo_variables extension which early parses for use in simple conditionals (I should extend mo_variables to support a {logged_in/out} var as well):

{if "{logged_in_member_id}" == FALSE}{redirect="member/login"}{/if}

{exp:channel:entries require_entry="yes"}
    {addon_github}
        {exp:cl_github:repo_contents_archive 
        owner="{owner}" 
        repo="{repo}" 
        ref="{ref}"
        }
    {/addon_github}
{/exp:channel:entries}

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.