Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fbparis/NOCMS

CMS for l33t
https://github.com/fbparis/NOCMS

Last synced: 3 months ago
JSON representation

CMS for l33t

Awesome Lists containing this project

README

        

NOCMS
=====

NOCMS is a minimalist yet powerful system to serve and cache HTML / PHP pages. You can see a live demo on http://nocms.fbparis.com/ or a full website using it here: http://monguide.org/.

It comes with some goodies such as sample robots.txt, sitemap.php, tips to refresh the cache and some example pages but all you need to start is:
* a configuration file for your web server (a working configuration for nginx is provided)
* the provided index.php file in your document root folder
* to create 3 directories in your document root folder: .pages/, .templates/ and .cache/ (.cache/ must be readable and writable by your web server)

How does it work ?
------------------

Using your favorite editor, you can create your template(s) in the .templates folder. Templates files must have a .php extension and you can insert any php and html code you want inside. In the place where you want the content of each specific page to be displayed, add this: (or , see NOCMS API).

You'll need at least a default template in /.templates/default.php and a template for your 404 page in /.templates/404.php. Off course, the 404 template does not need to display NOCMS::$content.

Once you have at least a default template, for each page you want on your web site you need to create this page in your .pages folder with the exact same name and a php extension. For example, let say you want a page named http://example.com/other/about.html, you'll have to create a file named about.html.php in .pages/other/. If you want a page for your home on http://example.com/ you'll have to create the file .pages/.php, and so on.

The rendering of files in .pages/ will be assigned to NOCMS::$content in your template file. Again, you can insert any php and html you want in these files.

**Important notice:** _this means the file in .pages/ will be interpreted **BEFORE** your template file in .templates/. This is why you can define some custom php variables in your .pages/ files and then retrieve them in your template file._

What about caching ?
--------------------

Each time a page is requested and generated by index.php, the resulting html will be stored in .cache/ folder if:
* the URL has been requested via a GET or a HEAD method and,
* no arguments have been passed through a query string and,
* the matching page and template exist and,
* no call have been done to the NOCMS::nocache() method (see NOCMS API) and,
* no call have been done to the NOCMS::add_header() method (see NOCMS API) and,
* no call have been done to the NOCMS::is() method (see NOCMS API).

Each time a page is requested, the web server will render a static html page instead of index.php if:
* the URL has been requested via a GET or a HEAD method and,
* a matching file in .cache/ folder exists.

**Important notice:** _by default, no maintenance or something is done with the cached files. This means pages are cached permanently. You may want to refresh the cache sometimes, the best way is probably to empty the .cache/ folder (manually or automatically). A script named "update-cache.php" is also provided in the utils/ folder to quickly (re)generate the static files in .cache/._

NOCMS API
---------

NOCMS class provides a few public properties and methods you can use in your templates and / or files.

### In your templates:

* **property NOCMS::$content**

_string NOCMS::$content_

The output rendered by the requested page (the php file in your .pages/ folder matching the requested URL).
* **method NOCMS::the_content()**

_void NOCMS::the_content()_

This is a shortcut for "echo NOCMS::$content;".

### In your pages:

* **method NOCMS::template()**

_void NOCMS::template(string $template)_

Set the template which will be used to render the page. By default the template in use is "default". The parameter $template must be the name of a template file in your .templates/ folder without the .php extension (ie "default" for ".templates/default.php").

_string NOCMS::template()_

Will return the name of the template currently in use (ie "default" for ".templates/default.php").

### In your templates or pages:

* **property NOCMS::$lastModified**

_integer NOCMS::$lastModified_

Set this to NULL to prevent sending a Last-Modified header (you should'nt need it anyway).
* **method NOCMS::lastModified()**

_integer NOCMS::lastModified([integer $timestamp])_

If you pass a timestamp and this timestamp is a more recent date than the last modified date automatically found by NOCMS, it will be used in the Last-Modified header sent. You can use it for example if you're displaying content from a database and you want to sent an accurate Last-modified header.

Returns the current Last-Modified timestamp.
* **method NOCMS::uri()**

_string NOCMS::uri()_

Returns $_SERVER\['REQUEST_URI'\] without the query part (ie for "http://example.com/test.php?args=1" it will return "/test.php").
* **method NOCMS::args()**

_string NOCMS::args()_

Returns $_SERVER\['QUERY_STRING'\] (ie for "http://example.com/test.php?args=1" it will return "args=1").
* **method NOCMS::nocache()**

_void NOCMS::nocache()_

Prevents a page to be cached.
* **method NOCMS::file_get_contents()**

_string NOCMS::file_get_contents(string $filename)_

Same as file_get_contents($filename) but it will update NOCMS::$lastModified on success. You should use it instead of file_get_contents() so.
* **method NOCMS::add_header()**

_void NOCMS::add_header(string $name, string $value\[, boolean $force = false\])_

Use this method if you need to send custom headers. Calling this method will also prevent the page to be cached.

Also, with this method headers sent in the page (file in .pages/ folder) will override headers sent in your template file.

If you want some header sent in your template file not to be overriden, just call the method with $force parameter set to "true" in your template file.
* **method NOCMS::is()**

_boolean NOCMS::is(string $user_agent\[, string $regex_hostname = ''\])_

Use this method if you need to serve a different content according to the User-Agent or Hostname of the visitor. Calling this method will also prevent the page to be cached.

First parameter is a string you want to test the presence or not in the User-Agent string of the Visitor.

Second parameter is a regular expression you want to use to check the host name of the visitor.

Here are some examples:
* Detecting Internet Explorer browser: NOCMS::is('msie');
* Detecting the googlebot: NOCMS::is('google', '\.google(bot)?\.com$');

Tips and tricks
---------------

This documentation is longer than the code itself so you better check the index.php file or try it if you want to go further.

Anyway, I'll add some other templates and pages examples, just take a look.