2 of 2
2
Plugin: Switchee
Posted: 26 February 2010 10:46 AM   [ Ignore ]   [ # 19 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  258
Joined  10-08-2002

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

Profile
 
 
Posted: 26 February 2010 10:58 AM   [ Ignore ]   [ # 20 ]  
Summer Student
Avatar
Total Posts:  8
Joined  04-18-2006

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…

Profile
 
 
Posted: 26 February 2010 11:40 AM   [ Ignore ]   [ # 21 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  258
Joined  10-08-2002

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} 
Profile
 
 
Posted: 26 February 2010 11:51 AM   [ Ignore ]   [ # 22 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  258
Joined  10-08-2002

... 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?

Profile
 
 
Posted: 26 February 2010 12:04 PM   [ Ignore ]   [ # 23 ]  
Summer Student
Avatar
Total Posts:  8
Joined  04-18-2006

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.

Profile
 
 
Posted: 26 February 2010 12:16 PM   [ Ignore ]   [ # 24 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  258
Joined  10-08-2002

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} 
Profile
 
 
Posted: 02 March 2010 07:37 PM   [ Ignore ]   [ # 25 ]  
Summer Student
Avatar
Total Posts:  8
Joined  04-18-2006

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

Profile
 
 
Posted: 22 March 2010 04:30 PM   [ Ignore ]   [ # 26 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  272
Joined  11-06-2007

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

Profile
 
 
Posted: 30 March 2010 04:23 PM   [ Ignore ]   [ # 27 ]  
Summer Student
Total Posts:  1
Joined  03-18-2010

Piping up: will this be ported to EE2? smile

Profile
 
 
Posted: 10 April 2010 05:53 AM   [ Ignore ]   [ # 28 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1156
Joined  02-02-2007

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.

File Attachments
pi.switchee_v1.5.zip  (File Size: 3KB - Downloads: 84)
pi.switchee2_v1.5.zip  (File Size: 3KB - Downloads: 75)
 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 19 April 2010 12:37 PM   [ Ignore ]   [ # 29 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  258
Joined  10-08-2002

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 smile

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.

Profile
 
 
Posted: 19 April 2010 02:20 PM   [ Ignore ]   [ # 30 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1156
Joined  02-02-2007

Hi Mark,

I’m glad that you liked my addition of “excise” parameter.

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.

Creating a clone of the plugin was quick and dirty solution for enabling to nest {case}{/case} variable pairs. Of course, it would be much better if it were possible to nest them inside the same plugin tag.

BTW, in plugin Find and Replace Plus it is possible to nest {replace_area}{/replace_area} variable pairs.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 24 April 2010 10:51 AM   [ Ignore ]   [ # 31 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  272
Joined  11-06-2007

@Mark,

Can you think of any other way to process a no_results type output without resorting to an embed?  I’m trying to stay clear of embeds to achieve the best performance as I’ve ready it can add a lot of overhead.

thanks…

Profile
 
 
Posted: 26 May 2010 06:33 PM   [ Ignore ]   [ # 32 ]  
Grad Student
Rank
Total Posts:  37
Joined  03-03-2009

Mark, this looks awesome, I can definitely think of several spots I can use it.  But it won’t quite do the trick for the coding issue that made me start looking for something like this in the first place - let me explain.

I’m an old mainframe coder - in Cobol, we have an instruction set that’s similar to switch-case-default, called EVALUATE.  It works like this:

EVALUATE (VARIABLE-NAME)
  WHEN (‘1’)
      code to execute…
  WHEN (‘2’)
      code to execute…
  .
  .
  .
  WHEN OTHER
      default code
END-EVALUATE.

However, it has one very important difference - it allows me to evaluate the reserved word TRUE, and then check the value of various switches and variables in the WHEN clauses, like this:

EVALUATE TRUE
  WHEN (SWITCH-1 = ‘Y’ AND SWITCH-2=‘Y’ AND STATE = ‘MA’)
      code to execute…
  WHEN (SWITCH-1 = ‘Y’ AND SWITCH-2=‘Y’ AND STATE NOT = ‘MA’)
      code to execute…
  .
  .
  .
  WHEN OTHER
      default code
END-EVALUATE.

The first WHEN clause that returns TRUE gets executed - if none of them return true, the default code gets executed.  Can you see any way to make switchee do that?  I think it would remove the need for nesting, and be really, really handy.  Let me know what you think, thanks!

Profile
 
 
Posted: 27 May 2010 04:44 AM   [ Ignore ]   [ # 33 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  258
Joined  10-08-2002

Hi John

that’s very interesting. I think it could be possible to add this but what worries me is the AND/OR/NOT logic between evaluated conditions. Currently Switchee effectively only supports OR (using the pipe | separator). Parsing the case values to evaluate the sum of these conditions would be tricky. Having said that, that’s what EE’s if/else advanced conditionals do so there may be code I can borrow there. I’ll look into it.

By the way, if you or anyone else reading this would like to help improve Switchee, I’m now hosting it on Github. Please fork and I’ll happily pull any changes that work well.

Profile
 
 
   
2 of 2
2