Jump to content

Shortest way to echo a field only if populated


diogo

Recommended Posts

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";
  • Like 18
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

  • Like 1
Link to comment
Share on other sites

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>";
  • Like 1
Link to comment
Share on other sites

  • 1 year later...

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.

Link to comment
Share on other sites

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"))
Link to comment
Share on other sites

 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. 

Link to comment
Share on other sites

@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): ?>
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...