We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Switchee

Development and Programming

Ryan M.'s avatar
Ryan M.
1,511 posts
about 16 years ago
Ryan M.'s avatar Ryan M.

This looks like a nice alternative to MD Detect Page Type. The logic might be a little cleaner using Switchee. Think I’ll give it a try on my current project.

       
Mark Croxton's avatar
Mark Croxton
319 posts
about 16 years ago
Mark Croxton's avatar Mark Croxton

Great stuff, let me know how you get on.

You will also get a very significant reduction in page load over using MD Detect Page Type, as the non-matching cases are removed by Switchee before they get parsed by EE.

       
Ryan M.'s avatar
Ryan M.
1,511 posts
about 16 years ago
Ryan M.'s avatar Ryan M.

Right. Nice work, Mark!

       
biteMedia's avatar
biteMedia
8 posts
15 years ago
biteMedia's avatar biteMedia

Can {member_group} be used as the variable to test against? I’ve got the following in an embedded template but doesn’t seem to work. Is it a parsing order problem / that it’s within an embed?:

{exp:switchee variable="{member_group}" parse="inward"}
    {case value="1|6"}
        admin content
    {/case}
    {case default="Yes"}
        non-admin content
    {/case}
{/exp:switchee}
       
Mark Croxton's avatar
Mark Croxton
319 posts
15 years ago
Mark Croxton's avatar Mark Croxton

Could be - try passing {member_group} as a variable to the embedded template, and referencing as {embed:member_group}

       
biteMedia's avatar
biteMedia
8 posts
15 years ago
biteMedia's avatar biteMedia

Just tried it and it’s not working - my guess is it’s a parsing order issue as the default condition is displaying in each case…

       
Mark Croxton's avatar
Mark Croxton
319 posts
15 years ago
Mark Croxton's avatar Mark Croxton

You’re right, member_group must be parsed later by EE.

If you enable php at input stage for your template you could do it like this:

{exp:switchee variable="<?php global $SESS; print(@$SESS->userdata['group_id']); ?>" parse="inward"}
                    {case value="1|6"}
                        admin content
                    {/case}
                    {case default="Yes"}
                        non-admin content
                    {/case}
                {/exp:switchee}
       
Mark Croxton's avatar
Mark Croxton
319 posts
15 years ago
Mark Croxton's avatar Mark Croxton

… which actually gives me an idea. Would people be interested in having the ability to specify a variable from the various globals arrays and have switchee grab it?

eg [sess_username] or [post_member_id] or [get_id] or [cookie_year] etc

What do you think?

       
biteMedia's avatar
biteMedia
8 posts
15 years ago
biteMedia's avatar biteMedia

Mark, your PHP solution worked, thanks - but in that embedded template I have some other PHP code that must be parsed on output…alas.

But not a huge deal, just looking for ways to optimize speed on some templates that are using a fair number of conditionals.

       
Mark Croxton's avatar
Mark Croxton
319 posts
15 years ago
Mark Croxton's avatar Mark Croxton

The alternative would be to write a small plugin and wrap switchee with it.

$plugin_info = array(
                 'pi_name'          => 'My Plugin',
                 'pi_version'       => '',
                 'pi_author'        => '',
                 'pi_author_url'    => '',
                 'pi_description'   => 'Does stuff',
                 'pi_usage'         => My_plugin::usage()
               );

class My_plugin {

    var $return_data;
   
    function My_plugin()
    {
      global $SESS, $TMPL;

      $this->return_data = $TMPL->swap_var_single('member_group', @$SESS->userdata['group_id'], $TMPL->tagdata);
    }

    function usage()
    {

    }
}

and then:

{exp:my_plugin parse="inward"}
{exp:switchee variable="{member_group}" parse="inward"}
...
{/exp:switchee}
{/exp:my_plugin}
       
biteMedia's avatar
biteMedia
8 posts
15 years ago
biteMedia's avatar biteMedia

The plugin route worked (!) now I need to see how much of a performance improvement it makes…

       
Joe Wolin's avatar
Joe Wolin
206 posts
15 years ago
Joe Wolin's avatar Joe Wolin

Mark,

I’m using your plugin and finding that my “no_results” weblog checks are not working properly. Is this because of the parsing order? Can you think of a way around this?

{exp:switchee variable="{segment_3}" parse="inward"}   
{case value="#^P(\\d+)$#|''"}

{exp:weblog:entries weblog="profile_blogs" sort="asc" limit="2" }
  <h4><a href="/blogs/{segment_2}/{url_title}">{title}</a></h4>
  {if no_results}    
    <div class="no_results">   
     No results       
    </div>
  {/if}
{/exp:weblog:entries}
{/case}

{/exp:switchee}

In the above code the “no results” conditional does not work if the parse=”inward” parameter is set on the switchee plugin

       
Royal Glenora Club's avatar
Royal Glenora Club
1 posts
15 years ago
Royal Glenora Club's avatar Royal Glenora Club

Piping up: will this be ported to EE2? 😊

       
Laisvunas's avatar
Laisvunas
879 posts
15 years ago
Laisvunas's avatar Laisvunas

Hi Mark,

I did some hacking with Switchee plugin:

1) I added “excise” parameter.

“excise” parameter accepts two values - “max” (default) and “min”. It allows you to specify if tagdata which surrounds matched case should be outputted (min) or not (max). E.g. in case {variable_to_test} has as its value the value1 the code

{exp:switchee variable = "{variable_to_test}" excise="max"  parse="inward"}
blah blah blah
{case value="value1"}
AAAAAAAAA
{/case}
blah blah blah
{case value="value2"}
BBBBBBBB
{/case}
blah blah blah
{/exp:switchee}

will output

AAAAAAAAA

But the code

{exp:switchee variable = "{variable_to_test}" excise="min"  parse="inward"}
blah blah blah
{case value="value1"}
AAAAAAAAA
{/case}
blah blah blah
{case value="value2"}
BBBBBBBB
{/case}
blah blah blah
{/exp:switchee}

in case {variable_to_test} has as its value the value1 will output

blah blah blah AAAAAAAAA blah blah blah blah blah blah

2) It is not possible to nest plugin tags. In order to circumvent tthis limitation I created the clone of Switchee plugin - Switchee2. Using Switchee and Switchee2 it is possible to write the code as this:

{exp:switchee variable = "{variable_to_test}" parse="inward"}
blah blah blah
{case value="value1"}
blah blah blah
{exp:switchee2 variable = "{variable_to_test}" parse="inward"}
{case2 value="value3"}
AAAAAAAAA
{/case2}
{case2 value="value4"}
CCCCCCCCCCCC
{/case2}
{/exp:switchee2}
blah blah blah
{/case}
blah blah blah
{case value="value2"}
BBBBBBBB
{/case}
blah blah blah
{/exp:switchee}

If you like my additions, feel free to incorporate them into official version.

       
Mark Croxton's avatar
Mark Croxton
319 posts
15 years ago
Mark Croxton's avatar Mark Croxton

Hmm I seem to have been unsubscribed from this thread for some reason. Sorry all for not replying sooner.

@Joe Wolin If you put {exp:weblog:entries} tag in an embedded template then your no_results tag will work as expected.

@Royal Glenora Club Just as soon as I do a job with EE2 I’ll port it. Should be dead simple.

@Laisvunas Those are very interesting additions. The ‘excise’ parameter is very neat indeed, I think I’ll add that to the ‘official’ version 😊

Nesting - it seems somewhat unelegant to have to clone the plugin to get nesting, so I’m not sure about that one. It does suggest another solution - parsing the tag data and inserting aliases for nested tags, then parsing the aliases recursively. I’m going to have a think about it a bit.

       
1 2 3

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.