joe_ma Posted March 7, 2014 Share Posted March 7, 2014 Hello I am playing around with the bootstrap profile. I'd like to use the carousel to display images, but only, if there are any. So I tried to write a function that I can use in the template. The function looks like this: function slider() { $albumimages = wire("page")->slider; $out =" "; if ($albumimages) { $out .= "<div id='myCarousel' class='carousel slide' data-ride='carousel'><div class='carousel-inner'>"; $i=0; foreach ($albumimages as $albumimage) { if ($i == 0) { $out .= "<div class='item active'>"; } else { $out .= "<div class='item'>"; } $i++; $out .= "<img class='img-responsive' src='{$albumimage->size(1170, 550)->url}'>"; $out .= "<div class='container'></div></div>"; } $out .="</div><a class='left carousel-control' href='#myCarousel' data-slide='prev'><span class='glyphicon glyphicon-chevron-left'></span></a><a class='right carousel-control' href='#myCarousel' data-slide='next'><span class='glyphicon glyphicon-chevron-right'></span></a> </div>"; echo $out; } else { $out =" "; echo $out; } } It works ok on pages that do have images. But on pages without images, it still is writing all the html. I don't find out, where I went wrong with the if-statement. Help is therefore very much needed and appreciated. Thanks 1 Link to comment Share on other sites More sharing options...
3fingers Posted March 7, 2014 Share Posted March 7, 2014 Hi joe_ma, not sure if this is going to work (not tested) but you can try this: function slider() { $albumimages = wire("page")->slider; $out =" "; if (count($albumimages)) { // COUNT IF SLIDER FIELD IS POPULATED WITH IMAGES, NOT ONLY IF IT EXISTS // REST OF THE CODE... Basically now you are checking if your slider exists in your template, and it always does. If you add count() function you can check if it is populated with images. Try it, let me know if it solves your problem Cya! Link to comment Share on other sites More sharing options...
joe_ma Posted March 7, 2014 Author Share Posted March 7, 2014 Yessssssssssss!! Thank you very much 3fingers. You made my day. Link to comment Share on other sites More sharing options...
3fingers Posted March 7, 2014 Share Posted March 7, 2014 You're welcome Link to comment Share on other sites More sharing options...
Pete Posted March 7, 2014 Share Posted March 7, 2014 It doesn't particularly need to be in a function, also I'm not sure why you're using wire() since in a template you can just use $page directly - anyway, you can get rid of the } else { if you start it a bit like this instead: $out = ''; // Starting with this means you don't need } else { as it's defined and empty to begin with if ($page->slider->count() > 0) { // There's no overhead in just doing this count directly instead of assigning it to another variable // ... more of your code here foreach ($page->slider as $albumimage) { // Again, you can just user $page->slider directly here // ... the rest of your code here 2 Link to comment Share on other sites More sharing options...
joe_ma Posted March 7, 2014 Author Share Posted March 7, 2014 Thanks Pete I have tried your solution. But that throws an error: Error: Call to a member function count() on a non-object Or can't I use your solution in a function, but only directly in the template? Link to comment Share on other sites More sharing options...
kongondo Posted March 7, 2014 Share Posted March 7, 2014 (edited) In a function you want to use wire('page') or $wire->page....same is true for other PW variables...Something about PHP scope whose name I always forget... If in a class (module)....there's also $this.... Edited March 7, 2014 by kongondo 1 Link to comment Share on other sites More sharing options...
joe_ma Posted March 7, 2014 Author Share Posted March 7, 2014 Thanks kongondo for the clarification. Link to comment Share on other sites More sharing options...
Pete Posted March 7, 2014 Share Posted March 7, 2014 Yeah, in my version you don't need it to be in a function unless you are using this code on many different templates? If it's just in one template then the function isn't needed. 1 Link to comment Share on other sites More sharing options...
joe_ma Posted March 11, 2014 Author Share Posted March 11, 2014 Obviously I still don't seem to get it right. I have just encountered another one I can't solve. I have a template, that I use for different kinds of news. The newsposts are in different folders. For news in one of the folders I want to display sort of a «Back»-button, news in the other folder don't need that button. So I have tried this: if ($page->parentID=1016) { echo '<p class="retour"><a class="btn btn-default" role="button" href="'.$pages->get("title=Anlässe")->url .'">Zurück zur Übersicht</a></p></div>; } But that has no effect at all. The «Back»-Button is displayed in all the newsposts of both folders, not only in the folder with the ID=1016. I have also tried if ($page->parentID="1016") { and if ($page->parent->title="Notti") { with the same results. Thank you for help. Link to comment Share on other sites More sharing options...
onjegolders Posted March 11, 2014 Share Posted March 11, 2014 if ($page->parent->id == "1016") { } EDIT: You may not need the "" there. also if ($page->parent->title == "Notti") { echo... } 1 Link to comment Share on other sites More sharing options...
joe_ma Posted March 11, 2014 Author Share Posted March 11, 2014 Small cause, great impact … Thank you onjegolders, that solved it. 1 Link to comment Share on other sites More sharing options...
Pete Posted March 11, 2014 Share Posted March 11, 2014 Just to explain what went wrong - firstly, parentID doesn't exist. You would use $page->parent->id as in the corrected example above, so you are saying "this page's parent's ID" (or "the ID belonging to the parent of this page" if you read it from right to left, which sort of makes more sense if you read it to yourself that way). The other issue was = versus == which I'll cover below: // A single equals sign means you are assigning a value of 1016 to $page->parent->id in this case: $page->parent->id = 1016 // So literally, $page->parent->id now has a value of 1016 because you told it to. Similarly, $something = 'a value'; means that if you echo $something it will output "a value". // A double equals sign means "equals", so if ($page->parent->id == 1016) { // means "if $page->parent->id is equal to 1016" There are lots more ternary operators besides == which you can read up on here, as you'll probably use some of them a lot: http://uk3.php.net/ternary 2 Link to comment Share on other sites More sharing options...
joe_ma Posted March 11, 2014 Author Share Posted March 11, 2014 Thank you Pete for these explanations. I really appreciate help in php matters. parentID doesn't exist Ah, but in the cheatsheet it does. And in fact if ($page->parentID == 1016) is working like expected. 2 Link to comment Share on other sites More sharing options...
Pete Posted March 11, 2014 Share Posted March 11, 2014 You're abosolutely right. It's a tad more efficient than $page->parent->id as well as ProcessWire isn't having to load the parent page into memory to get the ID. If you click on $page->parentID on the cheatsheet there's also an alias of parentID called parent_id which does the same thing. 2 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