PHP-Views
A... relatively simple, framework-agnostic, library to compile multiple views together into one.
NOTE Oct 14, 2019
This needs to be updated with more simplicity. It's not currently in a great state. It works. But basically, it's crap. Hopefully it will be cool soon. Might just let it go and use TWIG or something. The README sucks too, from the looks of it.
WARNING
There is currently an error which prevents this lib from working, unless you use the RequestHandler implementation.
Issues
- the script searches for
view
tags with properties =type="output"
ANDname=Head
. Thename
part should be removed. - The instructions are clunky
- It currently assumes that the
Public
directory is being used, for the compiled css & javascript files - Does not delete old JS & CSS files
- Prints out ugly HTML
- Slow, clunky. Creates multiple instances of
simple_html_dom
- Code is sloppy (but functional)
Supported Output Properties
These will be declared in a <view
tag.
-
title
- ex:title="The Greatest Article
- this will output a<meta property="og:title"
and a<title>
tag -
description
- ex:description="It is the greatest ever written"
- this will output a<meta property="og:description" name="description"
tag -
image
- ex:image="/images/the-greatest.jpg"
will output a<meta property="og:image"
tag with the FULL URL of the image on your server. You can provide a full URL, a path relative to the domain (starts with/
) or a path relative to the directory (does not start with/
) -
ogtype
- ex:ogtype="article"
will output a<meta property="og:type"
tag. Couldn't usetype
because that has another function already. -
fb_app_id
- ex:fb_app_id="SOME_LONG_NUMBER"
will output a<meta property="fb:app_id"
tag.
Goals
- Add option to specify the
compile
directory - Add instructions for specifying meta-information, such as title, keywords, and ??
- Improve the instructions, like a million times over. They currently seem tough to follow
Installing
With Composer
"require": { "rof/view" : "1.*" }
Manually
- Consider an Autoloader
- Copy
Controller.php
fromView
to wherever you'd like it on your server.
Usage
- Choose your Document Root (likely the root path of your website)
- Create a View
- Process the view (implementations below)
ReedOverflow's Request Handler Implementation (recommended)
Use RequestHandler with the custom view controller implementation.
This way you just create a View at the requested script and you don't have to write extra processing code.
Single Script Implementation
This way, you will create a singular script that handles the entire process.
<?php
ob_start();
?>
VIEW CONTENT GOES IN HERE (see below)
<?
$content = ob_get_clean(); //shove the content into a variable so it can be processed
$documentRoot = $_SERVER['DOCUMENT_ROOT']
$controller = new \ROF\View\Controller($documentRoot);
$controller->load($content);
$controller->display();
With separate Views directory
This way, you will store Views in a file separate from the script that's being requested.
Create a folder in your document root called View
. The name "ViewName"
correlates to View/ViewName.php
<?php
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$controller = new \ROF\View\Controller($documentRoot);
$controller->displayView("ViewName");
?>
You can create views in subdirectories and call $controller->displayView("Sub/Directory/ViewName");
Create a View
- There are 5 different
type
s of views. They include:-
<view type="self"></view>
which is purely used to specify css and javascript files -
<view type="include"></view>
which will include another view into a view being loaded -
<view type="parent">CONTENT</view>
which will include theparent
view and place content into it -
<view type="child"></view>
which will be within aparent
view. Theparent
view will paste the CONTENT, mentioned in the lasst type description, to thischild
view tag -
<view type="output"></view>
which is to go in the<head>
tag of your page, to contain stylesheet and script links
-
- There are 3 other properties
-
name
, used only withparent
type, like<view type="parent" name="Page">CONTENT</view>
. The child view asking for the parent view specifies the name. The parent view specifies it's name by the file name in theView
folder. -
stylesheets
, to specify css files this view relies upon. Ex:<view type="self" stylesheets="Page.css,Fonts.css"></view>
-
scripts
, to specify javascript files this view relies upon. Ex:<view type="self" stylesheets="FancyButtons.js,DropdownMenu.js"></view>
-
- Create a
Style
directory and aScript
directory in your document root. This is where your css and javascript files will be located. They will get compiled into a single file, saved into acompiled
directory, and then output onto the page as a singular<link>
and<script>
tag, respectively. - Create the view file according to the implementation you chose above. See "Example View" below.
Notes:
- You may specify stylesheets & scripts on any
view
tag, but it is generally recommended to place it on thetype="self"
view
tag. - View files in the
View
folder must have the.php
extension or they will not be accessible.
Including & nesting views
- Create a
View
directory inside your document root - Create a View, like above.
- Then:
-
<view type="include" name="VIEW_NAME"></view>
for a view that will be included from theView
Directory, which will supply all of its own content. -
<view type="parent" name="PARENT_VIEW_NAME">CONTENT GOES IN HERE</view>
which will paste the content into it'schild
view tag
-
Example Views
Whole Page:
Located at View/Page.php
<view type="self" stylesheets="Page.css" scripts="Page.js">
<html>
<head>
<view type="include" name="Head"></view>
<view type="output"></view>
</head>
<body>
<view type="include" name="Header"></view>
<view type="include" name="Navigation"></view>
<view type="include" name="Content"></view>
<view type="include" name="Footer"></view>
</body>
</html>
</view>
Then there will be several other views. The view with name="head"
does not display. It will contain a <meta>
tag and other tags you would place in <head>
. If you can specify stylesheets and scripts in the outermost <view>
tag, that is preferred.
Located at View/Content.php
:
<view type="self" stylesheets="Content.css">
<div id="content_area">
<view type="child"></view>
</div>
</view>
This is the content area. This is not a necessary extra layer, but it's my personal preference. That type="child"
view tag will be replaced with the content from the requested script.
Located at Public/funny-monkey.php
<view type="parent" name="Page"> <!-- this way the whole page HTML will be included around it -->
<h1>Funny Monkey</h1>
<p>I'm a page all about this one really funny monkey that I met when I was at the zoo.</p>
<img src="/pics/funny-monkey.jpg" alt="Funny Monkey" />
</view>
So, what's happening is:
- a user requests
mysite.com/funny-monkey.php
- my Request Handler(optional) will load the script at `DOCUMENT_ROOT.'/Public/funny-monkey.php'.
- This library parses the
<view>
tag and includes the script atDOCUMENT_ROOT.'/View/Page.php'
- This library parses the subsequent
<view>
tags and includes the scripts insideDOCUMENT_ROOT.'/View/'
named Header.php, Navigation.php, Content.php, and Footer.php - This library parses the subsequent
<view>
tags (insideContent.php
). - The
<view type="child"></view>
tag inContent.php
is replaced with the contents offunny-monkey.php
- The
<view>
tags are all replaced with the content from the relevant files. - All scripts and stylesheets from the many
<view>
tags will be compiled into a singular file - A
<script>
tag and<link>
tags will be created, linking to the compiled-versions of the files.