Skip to content

Instantly share code, notes, and snippets.

@MrBenGriffin
Created May 19, 2015 12:52
Show Gist options
  • Save MrBenGriffin/c48dd5db8a2a94e3fae7 to your computer and use it in GitHub Desktop.
Save MrBenGriffin/c48dd5db8a2a94e3fae7 to your computer and use it in GitHub Desktop.
using nview with consume...
<?php
mb_internal_encoding('UTF-8');
require_once("../lib/nview.iphp");
/*
This HAS been tested.
Uses a file master.html (see at bottom of this file)
*/
class Core {
static private $mv = NULL;
static private $nv = NULL;
static private $arts = array();
static function initialise($signature=NULL) {
static::$mv = new NView("master.html"); //Use master.
static::$nv = new NView(static::$mv->consume("//*[@data-xp='nav']")); //consume nav
static::$nv->set("//*[@data-nav='".$signature."']/@class","nav-self");
static::$nv->set("//*[@data-nav]/@data-nav"); //remove data attributes (demo)
$articles = static::$mv->consume("//*[@data-page]");
foreach($articles as $article) { //all the pages as DOMNodeList
$a = new NView($article);
$n=$a->consume("/*/@data-page");
static::$arts[$n]=$a;
}
return @static::$arts[$signature]; //return null if not matched. no error.
}
static function page(&$fn) {
static::$mv->set("//*[@data-xp='main_container']/child-gap()",$fn(static::ck()));
static::$mv->set("//*[@data-xp='nav_container']/child-gap()",HeaderNav::view(static::$nv));
static::$mv->set("//*[@data-xp]/@data-xp"); //remove data attributes (demo)
print static::$mv->show();
}
static function ck() {
return isset($_COOKIE["xsession"]);
}
}
class HeaderNav {
public static function view($n = NULL) {
if(is_null($n)) {
$n= new NView('nav_1.xml');
}
return $n;
}
}
/** Home Page
* Loads in a home page view and passes rest over to container management.
**/
$v = Core::initialise('home'); //allow core to override
if(is_null($v)) {
$v= new NView('home.xml');
}
$page_fn=function($has_ck) use (&$v) {
if($has_ck) {
$message="You have a cookie! ";
} else {
$message="You want a cookie? ";
}
$v->set("//*[@data-xp='ck_msg']",$cookie);
$del = $has_ck ? "y" : "n";
$v->set("//*[@data-xp='ck_del_".$del."']"); //delete unwanted message..
return $v; //return view.
};
Core::page($page_fn);
/* ------- master.html -----
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Main</title></head>
<body>
<header><nav data-xp="nav_container">
<ul data-xp="nav" class="navigation">
<li><a data-nav="home" href="/home">Home</a></li>
<li><a data-nav="catalogue" href="/catalogue">Catalogue</a></li>
<li><a data-nav="about" href="/about-us">About Us</a></li>
<li><a data-nav="register" href="/register">Register</a></li>
</ul>
</nav></header>
<main data-xp="main_container">
<article data-page="home">
<h2>Welcome to the Home Page</h2>
<h4><span data-xp="ck_msg">Placeholder cookie message.</span></h4>
<span data-xp="ck_del_n">You have a cookie!</span>
<span data-xp="ck_del_y">You want a cookie?</span>
</article>
<article data-page="about">
<h2>About Us and who we are</h2>
</article>
<article data-page="catalogue">
<h2>What we have to offer you</h2>
</article>
<article data-page="register">
<h2>Put yourself on our mailing list!</h2>
</article>
</main>
<footer data-xp="footer" />
</body>
</html>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment