<![CDATA[ Why does PHx evaluate the "else" clause when it's false? - My Forums]]> https://forums.modx.com/thread/?thread=45415 <![CDATA[Re: Why does PHx evaluate the "else" clause when it's false?]]> https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false?page=2#dis-post-463843
if id=1 then load chunk main-home
if id=4 then load chunk main-gallery
else = chunk main-standard

But main-home has also some ditto call into it, this doesn't work.

So I can probably use a phx:modifier which allows dittocalls into it?!
https://github.com/Jako/PHx-Modifier

But how do I use it?

I think I did do one some time ago but can't remember how I did it and on what project.

Documentation says:
[+string:switch=`xx:{{Chunk}}|yy:[*DocVar*]|default:[+TemplateVar+]+]


I have paste the snippet code and called the snippet switch but doesn't work:
[+string:switch=`1:{{main-home}}|4:{{main-gallery}}|default:{{main-standard}}+]


Anyone can point me in right direction?
Can't load a snippet into a snippet


####EDIT####


I guess it does work when I call my Ditto uncached!
For peoples who might want to use this:


My call:
[!main-switch?
&parent_ids=`1,4`
&chunk_names=`main-home,main-pizza`
&def_chunk_name=`main-default`
!]



My snippet:
<?php
// set the default ids of the top level parent containers
$default_parent_ids = array();
 
// set the default names of the chunks to display
// based on the same order as the $parent_ids
$default_chunk_names = array();
 
// set the overall default chunk name
$default_chunk_name = isset($def_chunk_name) ? $def_chunk_name : '';
 
// ===== end setup ===== //
 
 
$parent_ids = isset($parent_ids) ? explode(',',$parent_ids) : $default_parent_ids;
$chunk_names = isset($chunk_names) ? explode(',',$chunk_names) : $default_chunk_names;
 
if(count($parent_ids) > 0 && count($chunk_names) > 0){
 
    $uparent = $modx->runSnippet('UltimateParent');
    $check_array = array($modx->documentObject['id'],$modx->documentObject['parent'],$uparent);
 
    for($x=0; $x < count($parent_ids); $x++){
        if(in_array($parent_ids[$x],$check_array)){
            $print_chunk = $chunk_names[$x];
        }
    }
     
    // if $print_chunk is empty set to overall default
    if($print_chunk ==''){
        $print_chunk = $default_chunk_name;
    }
     
}
 
return $modx->getChunk($print_chunk);
?>



Main-home chunk has this dittocall:
[[Ditto? 
&parents=`9`
&total=`3`
&showPublishedOnly=`1` 
&orderBy=`menuindex ASC` 
&noResults=`No news..` 
&tpl=`news-ditto`
]]
]]>
fourroses666 Apr 22, 2013, 06:10 AM https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false?page=2#dis-post-463843
<![CDATA[Re: Why does PHx evaluate the "else" clause when it's false?]]> https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false?page=2#dis-post-462509 sottwell Apr 11, 2013, 04:48 AM https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false?page=2#dis-post-462509 <![CDATA[Re: Why does PHx evaluate the "else" clause when it's false?]]> https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false?page=2#dis-post-462472
]]>
fourroses666 Apr 11, 2013, 02:35 AM https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false?page=2#dis-post-462472
<![CDATA[Re: Why does PHx evaluate the "else" clause when it's false?]]> https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false?page=2#dis-post-462455
$id = $modx->documentObject['id'];
$parent = $modx->documentObject['parent'];

$output ='';
if( $id == 1 || $parent == 1 ){
    $output .='[[Ditto . . .]]';
}

return $output;
]]>
breezer Apr 10, 2013, 10:45 PM https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false?page=2#dis-post-462455
<![CDATA[Re: Why does PHx evaluate the "else" clause when it's false?]]> https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false?page=2#dis-post-462446

$id = $modx->resource->get('id');
$parent = $modx->resource->get('parent');

if (($id == 1) || ($parent == 1)) {
    return '[[Ditto . . .]]';
}
return '';


Oh, Oh. I just realized that if it's Ditto, you're probably using Evolution. You could leave out the first line, but I can't remember how to get the parent ID in Evo. It might be set, if so, you can just delete the first two lines.]]>
BobRay Apr 10, 2013, 09:16 PM https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false?page=2#dis-post-462446
<![CDATA[Re: Why does PHx evaluate the "else" clause when it's false?]]> https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false#dis-post-462350
I need to "say", when id or parent = 1 then x
The output needs to be a chunk with ditto into it.

<?php
/* ShowStuff Snippet */
$output = '';
switch($id) {
   case 1:
   case 2:
       $output = 'a';
       break;
 
   default:
       $output = 'z';
       break;     
 
}
 
return $output;


Think I need to use Switch for this
]]>
fourroses666 Apr 10, 2013, 04:31 AM https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false#dis-post-462350
<![CDATA[Re: Why does PHx evaluate the "else" clause when it's false?]]> https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false#dis-post-457880

I should mention that there are cases where a custom snippet is not noticeably faster than a well-written output modifier. There are other cases where the snippet is *way* faster. There are no cases where the output modifier will be faster than a good snippet. wink]]>
BobRay Mar 07, 2013, 03:09 AM https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false#dis-post-457880
<![CDATA[Re: Why does PHx evaluate the "else" clause when it's false?]]> https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false#dis-post-457862 ]]> fourroses666 Mar 07, 2013, 02:00 AM https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false#dis-post-457862 <![CDATA[Re: Why does PHx evaluate the "else" clause when it's false?]]> https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false#dis-post-457651
[!ShowStuff? $docId=`[[*id]]`!]



<?php
/* ShowStuff Snippet */
$output = '';
switch($id) {
   case 1:
   case 2:
       $output = 'a';
       break;

   default:
       $output = 'z';
       break;      

}

return $output;



]]>
BobRay Mar 05, 2013, 09:41 PM https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false#dis-post-457651
<![CDATA[Re: Why does PHx evaluate the "else" clause when it's false?]]> https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false#dis-post-457648
[*id:is=`1`:then=`a`:or id:is=`2`:then=`b`:else=`z`*]
You can use :or just in this way:
[*id:is=`1`:or:is=`2`:then=`a`:else=`z`*]

A snippet (using a switch statement) would be more adequate (and performing faster).
]]>
ottogal Mar 05, 2013, 09:17 PM https://forums.modx.com/thread/45415/why-does-phx-evaluate-the-else-clause-when-it-s-false#dis-post-457648