Skip to content

Instantly share code, notes, and snippets.

@MrBenGriffin
Last active November 28, 2017 13:45
Show Gist options
  • Save MrBenGriffin/85c2d2f692b2fec9b840 to your computer and use it in GitHub Desktop.
Save MrBenGriffin/85c2d2f692b2fec9b840 to your computer and use it in GitHub Desktop.
static views using NView.
/*
* !! The HTML files in the code are at the bottom of gist.
*/
/**************** VERSION 1 *******************
* (1 php file)
* File 'main_v1.php', simple static management
* Loads in a view and nav fragment, and returns the resulting view.
**/
<?php
$v= new NView('main.xml');
$n= new NView('nav_1.xml');
$v->set("//*[@data-xp='nav']/child-gap()",$n);
$v->show(true);
?>
/**************** VERSION 2 *******************
* (2 php files + Uses HeaderNav from v2)
/** File 'main_v2.php', simple static management
* Loads in a view, sets nav. and returns the resulting view.
**/
<?php
$v= new NView('main.xml');
$v->set("//*[@data-xp='nav']/child-gap()",HeaderNav::view());
$v->show(true);
?>
/*
* File 'header_nav_v2.php', simple handler.
* Loads nav fragment, and returns it
**/
<?php
class HeaderNav {
public static function view() {
$n= new NView('nav_1.xml');
//Modify nav dynamically here if needs be..
//Also compose sub-fragments here ...
return $n;
}
}
?>
/**************** VERSION 3 *******************
* (2 php files + Uses HeaderNav from v2)
* Version 3 shows container management.
* So with fragment management we can pass upstream
* aspects of our control that need to be dealt with
* by upstream logic.
*/
/** File 'home_v3.php', with container management.
* Loads in a home page view and passes rest over to container management.
**/
<?php
$v= new NView('home.xml');
//one can do non-global dynamic stuff here..
$page_fn= function($has_ck) use (&$v) {
//Do anything SPECIFIC TO HOME PAGE here that
//requires security, or whatever as set in core.
//Here we are testing a cookie status as an example
if($has_ck) {
$message="You have a cookie! ";
} else {
$message="You want a cookie? ";
}
$v->set("//*[@data-xp='ck_msg']",$cookie);
//Alternatively, one can switch out the messages..
$del = $has_ck ? "y" : "n";
$v->set("//*[@data-xp='ck_del_".$del."']"); //delete unwanted message..
return $v; //return our composed view.
};
Core::page($page_fn);
?>
/*
* File 'core_v3.php', container management class.
*/
<?php
class Core {
/*
* static page()
* Do anything to main file here such as sign-in/security/specials
* that are shared across all pages.
*/
static function page(&$fn) {
$mv = new NView("main.xml");
//Do security, session stuff.. here we just test for a cookie as an example.
$mv->set("//*[@data-xp='main_view']",$fn(static::ck()));
$mv->set("//*[@data-xp='nav']/child-gap()",HeaderNav::view());
$mv->show(true);
}
static function ck() {
return isset($_COOKIE["xsession"]);
}
}
?>
/**************** HTML FILES *******************/
/** File 'main.xml', the core page. **/
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Main</title></head>
<body>
<header><nav data-xp="nav" /></header>
<main data-xp="main" />
<footer data-xp="footer" />
</body>
</html>
/** File 'nav.xml', initial static navigation fragment **/
<!DOCTYPE ul>
<ul class="navigation" xmlns="http://www.w3.org/1999/xhtml">
<li><a href="/home">Home</a></li>
<li><a href="/catalogue">Catalogue</a></li>
<li><a href="/about-us">About Us</a></li>
<li><a href="/register">Register</a></li>
</ul>
/** File 'home.xml': Home Page fragment. Used in v3 */
<!DOCTYPE article>
<article xmlns="http://www.w3.org/1999/xhtml">
<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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment