OllieMackJames Posted April 9, 2013 Share Posted April 9, 2013 EDIT: this topic is solved, I added the solution I now use in this first section. With thanks to all the contributors in the thread. OK, the question was: How to port the following modx Ditto call: especially the tpl and tplAlt thing. [!Ditto? &parents=`28` &depth=`2` &tpl=`best-article-tpl` &tplAlt=`best-article-tplAlt` &randomize=`1` &display=`4` &showInMenuOnly=`1` &filter=`id,[*id*],2|id,69,2|id,116,2|id,117,2|id,124,2`!] The results should be formatted with alternating: <!--best-article-tpl--> <div class="testiecol"> <div class="testie"><h4>[+testie-header+]</h4> <div class="testiequote">[+testie-text+]</div> <h5>[+testie-name+]</h5></div> </div> <!--best-articel-tplAlt--> <div class="testiecolalt"> <div class="testie"><h4>[+testie-header+]</h4> <div class="testiequote">[+testie-text+]</div> <h5>[+testie-name+]</h5></div> </div> Here's how I ended up doing it, thanks to onjegolders, soma, kongondo, diogo, macura73 and others. Ok Here's the code: <?php $randomTesties = $pages->find('template=testie, sort=random, limit=10, id!='.$page->id); if(count($randomTesties)){ foreach ($randomTesties as $randomTestie) { $class = ($count & 1) ? 'testiecolalt' : 'testiecol'; ?> <div class="<?php echo $class; ?>"> <div class="testie"><h4><?php echo $randomTestie->testie_header; ?></h4> <div class="testiequote"><?php echo $randomTestie->body; ?></div> <h5><?php echo $randomTestie->testie_name; ?></h5></div> </div> <?php } } ?> Works like a charm! Link to comment Share on other sites More sharing options...
onjegolders Posted April 9, 2013 Share Posted April 9, 2013 Hi Ollie, if I understand correctly you would want to add a $count variable and then use modulus to check if the iteration is even/odd if ($count % 2 == 0) { // even } Then output your alt for the odd or even version. As an example: <?php $array = array("one", "two", "three", "four", "five", "six"); $count = 0; $class = ""; foreach ($array as $e) { $count++; if ($count % 2 == 0) { $class = "testiecolalt"; } else { $class = "testiecol"; } ?> <div class="<?php echo $class; ?>"> <div class="testie"><h4>Header <?php echo $count ?></h4> <div class="testiequote">Quote</div> <h5>Name</h5></div> </div> <?php } ?> Note: There may well be better ways of achieving this! 2 Link to comment Share on other sites More sharing options...
OllieMackJames Posted April 9, 2013 Author Share Posted April 9, 2013 (edited) thanks onjegolders! I do not understand all the code, but managed to adapt it to the following: <?php $randomTesties = $pages->find('template=testie, sort=random, limit=10, id!='.$page->id); $count = 0; $class = ""; foreach ($randomTesties as $randomTestie) { $count++; if ($count % 2 == 0) { $class = "testiecolalt"; } else { $class = "testiecol"; } ?> <div class="<?php echo $class; ?>"> <div class="testie"><h4><?php echo $randomTestie->testie_header; ?></h4> <div class="testiequote"><?php echo $randomTestie->body; ?></div> <h5><?php echo $randomTestie->testie_name; ?></h5></div> </div> <?php } ?> And it works! Thanks lots! The following code I do not understand what it does: $count = 0; // Suppose this is just setting the starting point for counting? $class = ""; // Suppose this makes it empty, waiting to be filled? foreach ($randomTesties as $randomTestie) { $count++; //keep counting? if ($count % 2 == 0) { // if count divided by 2 equals zero? Just trying to bend my head around php here. It works anyways so thanks again! Edited April 9, 2013 by diogo format code Link to comment Share on other sites More sharing options...
diogo Posted April 9, 2013 Share Posted April 9, 2013 $count = 0; // sets the start of the count and $count++ adds 1 to it at each loop passage. onjegolders set the count to 0 and added 1 in the beggining of the loop, but it could also be set to 1, and raised immediately before the closing of the loop. $class = ""; // creates an empty string, this is usually needed so when the variable is set and you don't get an error when it's called later. In this case you don't really need this part because the variable is set later in both the "if" and the "else", so it will be set for sure when it's called later. You can delete that line if you want. $count++; // as I told before, adds 1 to the current count. if ($count % 2 == 0) // the % sign is the modulo operator http://php.net/manual/en/language.operators.arithmetic.php, the result is the remainder of a division operation. You know that a number is even if the result of % 2 is 0. 2 Link to comment Share on other sites More sharing options...
kongondo Posted April 9, 2013 Share Posted April 9, 2013 Ollie, $count = 0; // Suppose this is just setting the starting point for counting? Yes $class = ""; // Suppose this makes it empty, waiting to be filled? Yes. Create an empty variable. $count++; //keep counting? Yes. Push the counter forward once each time. if ($count % 2 == 0) { // if count divided by 2 equals zero? Nearly..Means if remainder of where the count is when you divide by 2 equals zero (i.e. even number). If there is a reminder, it is an odd number Diogo strikes again! Oh well.. Just my 2 cents...am no coder either cheers /k 3 Link to comment Share on other sites More sharing options...
OllieMackJames Posted April 9, 2013 Author Share Posted April 9, 2013 Thanks kongondo and diogo, now the code starts to make sense AND it works, before it only worked, thanks to onjegolders! This to me seems a nice example of being able to do all sorts without needing an extra templating engine like ditto! Starting to enjoy myself more and more with PW! 2 Link to comment Share on other sites More sharing options...
Soma Posted April 9, 2013 Share Posted April 9, 2013 I personally would code it like this is a more simple way: $randomTesties = $pages->find('template=testie, sort=random, limit=10, id!='.$page->id); // always test if anything found or you'll end up with empty markup or errors if(count($randomTesties)){ foreach ($randomTesties as $key => $testy) { // short version of what shown before $class = ($key % 2 == 0) ? 'testiecolalt' : 'testiecol'; $testy->altclass = $class; // temp save class to the page object $testy->template->filename = $config->paths->templates . "views/testy.php"; // set template file echo $testy->render(); } } And the template view: <!-- template file /views/testy.php --> <div class="<?php echo $page->altclass; ?>"> <div class="testie"><h4><?php echo $page->testie_header; ?></h4> <div class="testiequote"><?php echo $page->body; ?></div> <h5><?php echo $page->testie_name; ?></h5></div> </div> 4 Link to comment Share on other sites More sharing options...
kongondo Posted April 9, 2013 Share Posted April 9, 2013 Thanks kongondo and diogo, now the code starts to make sense AND it works, before it only worked, thanks to onjegolders! This to me seems a nice example of being able to do all sorts without needing an extra templating engine like ditto! Starting to enjoy myself more and more with PW! I know am beginning to sound like a broken record but once PW clicks, you wonder why you didn't use it before. I used to shudder at the thought of getting in the guts of MODX to write my own snippets and would hide behind template engine syntax. Nothing wrong with that approach but am now convinced the PW way is better. I am glad I gave PW a spin. For instance, although both PW and WP do not use template engines, you try creating/editing a WP template! I totally hated it. The cool (maybe the coolest thing? I need to do a poll about this;)) thing with PW is its powerful and friendly selectors. Noobs like me can actually query the database in complex ways and retrieve data with minimal effort; yikes! Seriously, how do you beat $pages->find(); It can't get simpler than that! /k 5 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 9, 2013 Share Posted April 9, 2013 @OllieMackJames Take a look at Nik's module: SelectorTest , here you can play with selectors. You can quickly test if the selectors work. Can't thank Nik enough for this beautiful module. Link to comment Share on other sites More sharing options...
kongondo Posted April 9, 2013 Share Posted April 9, 2013 I personally would code it like this is a more simple way: $testy->altclass = $class; Soma, I don't get this bit about altclass. Where is its value coming from? Please explain. I was expecting $class to be used directly. Thanks. /k Link to comment Share on other sites More sharing options...
Soma Posted April 9, 2013 Share Posted April 9, 2013 kongodo, this is just a temporary property set to the page object. $page->somevalue = 123; echo $page->somevalue; // 123 1 Link to comment Share on other sites More sharing options...
kongondo Posted April 9, 2013 Share Posted April 9, 2013 kongodo, this is just a temporary property set to the page object. $page->somevalue = 123; echo $page->somevalue; // 123 Aah, thanks. /k Link to comment Share on other sites More sharing options...
Soma Posted April 9, 2013 Share Posted April 9, 2013 My example has a fault. The template render part should be in the foreach... Im on mobile so if someone with edit right sees this. Thx. Link to comment Share on other sites More sharing options...
Macrura Posted April 9, 2013 Share Posted April 9, 2013 also, can't you do this: if ($count % 2) instead of this: if ($count % 2 == 0) or maybe bitwise - supposedly faster if ($count & 1) 6 Link to comment Share on other sites More sharing options...
diogo Posted April 9, 2013 Share Posted April 9, 2013 I didn't know the bitwise version. Thanks! Link to comment Share on other sites More sharing options...
OllieMackJames Posted April 9, 2013 Author Share Posted April 9, 2013 (edited) Soma, thanks, I already have this whole code section as an include so did not want to go to another include again. I did try your abbreviations but only get one (1) result to show, here's the code I tried in stead of the earlier version: <?php $randomTesties = $pages->find('template=testie, sort=random, limit=10, id!='.$page->id); if(count($randomTesties)){ foreach ($randomTesties as $key => $randomTestie) { $class = ($key % 2 == 0) ? 'testiecolalt' : 'testiecol'; } ?> <div class="<?php echo $class; ?>"> <div class="testie"><h4><?php echo $randomTestie->testie_header; ?></h4> <div class="testiequote"><?php echo $randomTestie->body; ?></div> <h5><?php echo $randomTestie->testie_name; ?></h5></div> </div> <?php } ?> What am I missing here? Edited April 9, 2013 by diogo format code Link to comment Share on other sites More sharing options...
Soma Posted April 9, 2013 Share Posted April 9, 2013 My example has no include. Its more of a controller render a partial view. I just corrected my code as said earlier. This is a little cleaner and maintainable code than what you have. Also a minor point is that $key is zero based, so a $key+1 would make it behave first odd 1,2,3,4 rather than first even 0,1,2,3. (Not really a matter, just depends what class you set for odd and even.) Edit: you got the fault copied from my example.. the html is outside foreach. Link to comment Share on other sites More sharing options...
OllieMackJames Posted April 9, 2013 Author Share Posted April 9, 2013 (edited) Thanks for all the input! I incorporated a bit of different input to make something short and it works nicely. I shortened it to: <?php $randomTesties = $pages->find('template=testie, sort=random, limit=10, id!='.$page->id); if(count($randomTesties)){ foreach ($randomTesties as $randomTestie) { $class = ($count & 1) ? 'testiecolalt' : 'testiecol'; ?> <div class="<?php echo $class; ?>"> <div class="testie"><h4><?php echo $randomTestie->testie_header; ?></h4> <div class="testiequote"><?php echo $randomTestie->body; ?></div> <h5><?php echo $randomTestie->testie_name; ?></h5></div> </div> <?php } } ?> Edited April 9, 2013 by diogo format code 2 Link to comment Share on other sites More sharing options...
diogo Posted April 9, 2013 Share Posted April 9, 2013 Ollie, use the code formatting tool to show code on the forum as it's much more readable. This time I did it for you. Link to comment Share on other sites More sharing options...
OllieMackJames Posted April 9, 2013 Author Share Posted April 9, 2013 Diogo, thanks, will do next time. Link to comment Share on other sites More sharing options...
horst Posted April 9, 2013 Share Posted April 9, 2013 Ollie, use the code formatting tool to show code on the forum as it's much more readable. This time I did it for you. Sorry for be a bit OT, but I was wondering when viewing the Forum with IE10 it doesn't realize Linebreaks within the code. Is this only on my machine or have others experienced this too? (I normally don't use IE) Link to comment Share on other sites More sharing options...
kongondo Posted April 9, 2013 Share Posted April 9, 2013 @horst Same here (IE10)...and the font looks horrible as well! . Nway, I don't use IE too 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now