Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove $pager and $parent Variable #165

Closed
taufik-nurrohman opened this issue Dec 2, 2021 · 1 comment
Closed

Remove $pager and $parent Variable #165

taufik-nurrohman opened this issue Dec 2, 2021 · 1 comment
Labels
Milestone

Comments

@taufik-nurrohman
Copy link
Member

taufik-nurrohman commented Dec 2, 2021

Use dynamic property feature to generate $page->next, $page->parent and $page->prev properties. These properties, if available, will contains a Page instance:

<?php if ($page->parent): ?>
  <nav class="pager">
    <?php if ($next = $page->next): ?>
      <a href="<?= $next->url; ?>" rel="next">
        <?= i('Next'); ?>
      </a>
    <?php endif; ?>
    <?php if ($prev = $page->prev): ?>
      <a href="<?= $prev->url; ?>" rel="prev">
        <?= i('Previous'); ?>
      </a>
    <?php endif; ?>
  </nav>
<?php endif; ?>

For “pages” mode, we will use link in place of url because URL that ends with numbers usually isn’t a page file (or not related directly to a page file):

<?php if ($next = $page->next): ?>
  <a href="<?= $next->link; ?>" rel="next">
    <?= i('Next'); ?>
  </a>
<?php endif; ?>

To assign custom page property directly to the current page instance:

$page->next = new Page('.\path\to\file.page');
$page->set('next', new Page('.\path\to\file.page'));

To assign custom page property globally to any page instance:

function get_next_page_path($current) { … }

Page::_('next', function() {
    $next = get_next_page_path($this->path);
    return $next ? new Page($next) : null;
});
function get_next_page_path($current) { … }

Hook::set('page.next', function() {
    $next = get_next_page_path($this->path);
    return $next ? new Page($next) : null;
});
@taufik-nurrohman
Copy link
Member Author

Not possible to remove $pager safely because the $page variable likely will be overriden by the temporary variable of $pages’s item after foreach:

echo $page->next->link; // Referenced from the `$GLOBALS['page']` :)

foreach ($pages as $page) {
    echo $page->title; // This `$page` variable is no longer referenced to the `$GLOBALS['page']`
}

echo $page->next->link; // Overriden :(

@taufik-nurrohman taufik-nurrohman added this to the v3.0.0 milestone Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

1 participant