ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

possible to assign variable within php conditional, which is in an embed file

March 28, 2010 11:56pm

Subscribe [2]
  • #1 / Mar 28, 2010 11:56pm

    Brad Morse

    428 posts

    I want to assign a variable within an embed file that has a php conditional in it.

    The code for the php conditional, which is stored within template: include/header

    <?php
    if($user_in_group) {
        ?>{assign_variable:access_to_page="YES"}<?php
    }
    ?>

    Then the template it is embedded within is like

    {embed="include/header"}
    {if "{embed:access_to_page}" == "YES"}<h1>has access to page</h1>{/if}
    {embed="include/footer"}

    I also tried different variations of the EE conditional

    {if "{access_to_page}" == "YES"}<h1>has access to page</h1>{/if}
    {if {access_to_page} == "YES"}<h1>has access to page</h1>{/if}
    {if access_to_page == "YES"}<h1>has access to page</h1>{/if}

    None of those worked.

    I imagine it is not possible doing it this way. If it is, I’d love to find out how.

    I would like to use the native EE conditional in this situation.

  • #2 / Mar 29, 2010 10:38am

    ender

    1644 posts

    EE processes the main template to completion before any embedded templates run, so it isn’t going to be possible to assign any type of variable using an embedded template.

  • #3 / Mar 29, 2010 3:02pm

    Tom Jaeger

    497 posts

    The other thing you can do is enable PHP parsing on input for the template you are currently using, and they mix PHP variables with EE variables like this

    <?php
    if(1==1) 
    {
        $this->varr = "YES";
    }
    else
    {
        $this->varr = "NO";
    }
    ?>
    
    
    
    {if <?=$this->varr;?> == "YES"}<h1>has access to page</h1>{/if}

    Obviously you will need your own condition in the if statement where I have 1==1

    If you use a series of embeds and toggle the PHP parsing order, you can pass variables between embeds (as well as variables between EE content and PHP).  You will just have to use $this-> before PHP variables to make it globally accessible.

    However as mentioned above you can’t pass a variable from an embed back to the parent template.

    Does all of that make sense?

  • #4 / Apr 02, 2010 11:03am

    Brad Morse

    428 posts

    Think I could use this to throw in the php code and get the variable from it and test it w/i the conditional on the parent template? well there won’t be an embed, since the fresh variable would take the place of the embed.

    http://expressionengine.com/downloads/details/fresh_variables/

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases