Jump to content

How to get a standard-value from another page?


Manuel
 Share

Recommended Posts

Hi there!

I'm using two fields on multiple pages, called "meta_keywords" and "meta_description".
You're right, I'm using them for individual meta-tags on every page.

Here's my question:
Is there a smart way using $page->get(), to recieve a standard value from a specific page,
if the page itself, does not have a value?

I'm looking for something like this:

<?php echo $page->get('meta_description|page(1).meta_description');

Unfortunately this syntax will not work, but i hope you get the idea.
page(1) would be the standard-value from the page with the id 1.

Thanks for any ideas,
Manuel

Link to comment
Share on other sites

Hi @Edison,

4 minutes ago, Edison said:

echo $page->meta_description ?? $pages()->get(1)->meta_description;

That's the solution (PHP7 / Null coalescing operator) I'm using right now, while using PW i've learned there is very often a shorter or build-in way to solve that kind of problems.
Thank you any way ?

Link to comment
Share on other sites

1 hour ago, Edison said:

Hi @Manuel, I am afraid not... What about using the "Null coalescing operator" (if you are using php 7) or otherwise the ternary operator:


echo $page->meta_description ?? $pages()->get(1)->meta_description;

Just written in the browser w/o testing, hope it can help.

Hi again, after testing the script with the ?? Null coalescing operator I know it's not working, because it checks if a var is set (yeah it's always set, even when it's empty...)
So I came up with another solution, wich is still a bit unwieldy - but it's doing it's job.

<?php echo !empty($page->meta_description) ? $page->meta_description : $pages(1)->meta_description; ?>
<?php echo !empty($page->meta_keywords) ? $page->meta_keywords : $pages(1)->meta_keywords; ?>

Maybe we get a better snippet for that sort of code...

Have a nice evening!

Link to comment
Share on other sites

1 hour ago, Manuel said:

Maybe we get a better snippet for that sort of code...

I'd use ?: instead of ??. At least if you can trust the truthy-ness of the field's value if set (which should be okay for keywords and description).

And... though not necessarily better, I sometimes add a generic method to the Page class to abstract away logic from the template.

// in site/ready.php:

wire()->addHook("Page::getWithDefault", null, function(HookEvent $event) use($pages) {
	$page = $event->object;
	$property = $event->arguments(0);
	return $page->get($property) ?: $pages->get(1)->get($property);
});

Then your template code boils down to

<?= $page->getWithDefault("meta_description") ?>

 

  • Like 3
Link to comment
Share on other sites

H i@Manuel good point! I recalled get() to return NULL. But this happens only when field is not found (doubled checked in the API) ! So you are right, the variable is set but empty.

I think @BitPoet is right, for this purpose ?: is the right operator:

$a ?: $b Testing Conditional Function
$a ?: $b {Null}: b
$a ?: $b {Empty String}: b
$a ?: $b {0 Integer}: b
$a ?: $b {0 String}: b
$a ?: $b {False}: b
$a ?: $b {True}: 1
$a ?: $b {Full String}: a

 

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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