diogo Posted February 23, 2014 Posted February 23, 2014 I didn't know this one Shortest way to echo a variable only if populated (using pw fields of course ): echo $page->field?:''; and: echo $page->field ?: "field is empty"; // echoes field content echo $page->empty_field ?: "field is empty"; // echoes "field is empty" or even: echo $page->field1 ?: $page->field2 ?: "they are all empty"; 18
netcarver Posted February 23, 2014 Posted February 23, 2014 @diogo Short ternary operators were added in PHP 5.3 so your examples may not work for everyone (I think PW 2.3 runs on older versions of PHP.) Good examples regardless. 1
adrian Posted February 23, 2014 Posted February 23, 2014 I just discovered this myself last week - great to have a cleaner way to present this type of logic.
renobird Posted February 24, 2014 Posted February 24, 2014 Thanks Diogo, I was helping on a site a couple months ago where the dev used these — threw me for a loop for a second. Looked them up and realized exactly what Steve mentioned above, they won't work on my PHP 5.2.17 installs. Yet another reason to push for a PHP upgrade on our vhosts.
Soma Posted February 24, 2014 Posted February 24, 2014 Shortest is to just echo $page->field. If empty it will not output anything. 8
Joss Posted February 24, 2014 Posted February 24, 2014 hmmm - like this. Could neaten up some of my image fields....
onjegolders Posted February 27, 2014 Posted February 27, 2014 I like being verbose! If this { do that } or else { do this } or if not { do this } ok go on then { do this instead } Much cleaner 5
felix Posted March 4, 2014 Posted March 4, 2014 +1 for verbosity. It's much more readable, easier to extend and as a plus avoids goto fail bugs. 1
Joss Posted March 4, 2014 Posted March 4, 2014 Yeah, not too sure about "or else" Sounds a bit threatening! 1
clsource Posted March 9, 2014 Posted March 9, 2014 For the template files you can use the alternative syntax http://cl1.php.net/manual/en/control-structures.alternative-syntax.php <?php if($something): ?> <!-- Html --> <p>My content inside an if </p> <?php endif ?> You can use them with foreach, for, while, switch, etc. 1
Nico Knoll Posted March 15, 2014 Posted March 15, 2014 I always use kind of this: <?php echo 'bla bla'.(($page->bla) ? $page->bla : '').' lorem'; ?> 1
dotnetic Posted March 21, 2014 Posted March 21, 2014 I know this is a lil bit off-topic but most times i want to echo a variable prepended by an label or followed by a br tag. But the line with the variable and br should not be generated when the variable is empty. So i use function echoField($variable, $prepend='', $append = false){ if ($append === true) { $append = '<br>'; } if ($variable) echo $prepend.$variable.$append; } and output it with <?= echoField($pages->get("/einstellungen/")->telefon,'Telefon :',true); ?> <?= echoField($pages->get("/einstellungen/")->email,'E-Mail: ',true); ?> What do you think of this? 1
diogo Posted March 21, 2014 Author Posted March 21, 2014 I understand and approve the suggestions to be verbose from some people. Usually it's better to be verbose to avoid confusion. That said, just for the record, I would still like to give a short alternative to what @jmartsch posted: if ( $thisField = $pages->get("/einstellungen/")->telefon ) echo "Telefon :" . $thisField . "<br>"; 1
Reid Bramblett Posted September 17, 2015 Posted September 17, 2015 Great for usage within a page, but what about for subpages? This works when all you are dealing with is immediate children : if ($page->child->field) ... but for some reason this doesn't work if you want to canvas all children, grandchildren, etc.: if ($page->children->field) Nor does the array version work: if (count($page->children->field)) I know I can get it elsewise, for example: if ($pages->count("has_parent=$page, field!=0")) ...but it just seems like the $page->children->field should work.
thetuningspoon Posted September 17, 2015 Posted September 17, 2015 What would you expect $page->children->field to return? An array containing the values of the field for every child page? Or are you thinking it should return true if any of the children have that field populated?
LostKobrakai Posted September 17, 2015 Posted September 17, 2015 An array containing the values of the field for every child page? $page->children->explode("fieldName"); Or are you thinking it should return true if any of the children have that field populated? (bool) $page->children->get("fieldName!=''");
Reid Bramblett Posted September 19, 2015 Posted September 19, 2015 Sorry for the confusion. I was using "field" generically, as a stand-in for whatever particular field you are after. Say you want to find all children that have the field "address" populated. In that instance this works to find all immediate child pages that have something in the "address" field: if ($page->child->address) ... but this does not work to do the same if you want to canvass all children, grandchildren, etc. down the page hierarchy: if ($page->children->address) Though this does: if ($pages->count("has_parent=$page, address!=0"))
diogo Posted September 19, 2015 Author Posted September 19, 2015 if ($page->children->address) Here you are trying to get the address of many pages. Doesn't work. $page child gets the first page from the children, just like if you would do $page->children->first(), and being a single page, you can get it's fields without looping.
tpr Posted September 19, 2015 Posted September 19, 2015 Offtopic: I just love Latte for such things: <p n:if="$page->bla">{$page->bla}</p>
Michael Murphy Posted September 23, 2015 Posted September 23, 2015 @Reid Bramblett Have you tried adding a selector in the $page->children call? $page->children("address!=0") Maybe you could combine this with the $page->siblings $page->siblings("address!=0") Regarding the original post by @diogo, how do people check for fields that return an array like image and page fields? This is what I do, but maybe there is a more elegant way? <?php if($page->images->count() > 0): ?>
rick Posted September 23, 2015 Posted September 23, 2015 <?php if( is_array( $page->images ) ) { } ?> 1
LostKobrakai Posted September 23, 2015 Posted September 23, 2015 Or this: if($page->images->eq(0)){ } 1
Recommended Posts