Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Created July 20, 2017 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielhaim1/80f28937dec5e7b94e4201ba1f1afe0e to your computer and use it in GitHub Desktop.
Save danielhaim1/80f28937dec5e7b94e4201ba1f1afe0e to your computer and use it in GitHub Desktop.
Auto Appender & Nav Builder
<?php
// The PHP version of AutoAppender.js,
// @author Daniel Haim
// @url https://codepen.io/danielhaim1/pen/pwXJLp?editors=1010
class Page {
public $header = array();
public $sections = array();
public function addSection($title, $body) {
$id = $this->titleToId($title);
$this->header[] = '<a data-scroll href="#'.$id.'">'.$title.'</a>';
$this->sections[] = '<section class="section-block" id="'.$id.'">
<div class="container">
<div>
<h4>'.$title.'</h4>
<p>'.$body.'</p>
</div>
</div>
</section>';
}
private function titleToId($title) {
$patterns = array(
'/\s/',
'/[^a-zA-Z0-9\-_]/',
'/^[^a-zA-Z]/',
);
$replacements = array(
'-',
'',
'',
);
return preg_replace($patterns, $replacements, strtolower($title)) . count($this->header);
}
}
$page = new Page();
$page->addSection('Eenie', 'The parent container + nav link are both automatically created based on the h4 element inside this section.');
$page->addSection('#foo', 'The section title starts with a #, which should break the id.');
$page->addSection('Foo', '');
$page->addSection('Bar', '');
?>
<body>
<header>
<nav data-affix="">
<?= implode("", $page->header); ?>
</nav>
</header>
<div data-affix-container="">
<?= implode("", $page->sections); ?>
</div>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment