5: Utility Classes

NOUVEAU contains a library of “utility classes” (also sometimes called “helper classes”) that are used throughout the framework core, but which can also be useful to developers.

Generally, each file contains a single class, and each comes preloaded by default – so you can use them immediately when coding.

\NV\HtmlBase

Although it is not directly used in NOUVEAU core, the \NV\HtmlBase class ( nv\utilities\HtmlBase.php ) can be used to generate HTML programmatically. It contains only 5 simple methods…

atts( $atts )

This method takes an associative array and generates HTML attributes.

atts_default( $defaults = array(), $atts = array() )

This is a shortcut for PHP’s array_merge() function… except this one will continue to work even if no arguments or partial arguments are provided.

elem( $tag, $atts = array(), $content = "", $closetag = false )

This method can be used to generate any kind of HTML or XML tag/element automatically.

randomId()

This method simply generates a random id attribute ( e.g. rand-012345 ).

wrap( $tag, $content = "" )

This method can wrap any string in a new HTML or XML tag.

\NV\Html

The \NV\Html class ( nv\utilities\Html.php ) extends the HtmlBase class in order to provide some shortcuts/aliases that simplify the generation of HTML. A small sample of these include:

a( $href = '#', $atts = array(), $content = '', $echo = false )

This method generates an anchor (link) element, e.g. <a href="#"></a>.

img( $src = '', $atts = array(), $echo = false )

This method generates an image element, e.g. <img src="http://placehold.it/20x20/" />.

NV_Requirements

The NV_Requirements() class ( in nv\utilities\NV_Requirements.php ) is used by core to determine if the current server configuration meets NOUVEAU‘s PHP and WordPress version requirements.

Because this function must work with older versions of PHP, it is not namespaced, but uses a prefix instead.

When instantiated, the resulting object stores the results of the requirements check in a property called is_compatible.

In NOUVEAU core, this class is automatically instantiated by NV::init() so that the static method returns the value of NV_Requirements->is_compatible.

\NV\Php

The \NV\Php class ( in nv\utilities\Php.php ) provides functions meant to help with general PHP programming. These functions are occasionally used in core.

array_detach()

Detaches a specified item from an array and returns that item.

array_detach_value()

Detaches a specified item from an array by value and returns that item.

array_reorder()

Moves an item from one position in an array to another position in the array.

\NV\Theme

The \NV\Theme class ( in nv\utilities\Theme.php ) contains most of the basic NOUVEAU methods you will need for a theme. This includes a number of very useful options, and any additional functions you might call directly from a template file should be placed here.

get_header( $name = null, $path = "layout/" )

This is a replacement for WordPress’s built-in get_header() function. This version will automatically look for the header file in NOUVEAU‘s layout folder… but it also allows you to specify a custom file name and a custom path, like so: "{$path}footer-{$name}.php"

get_footer( $name = null, $path = "layout/" )

This works just like \NV\Theme::get_header(), except that it automatically fetches a footer instead.

loop( $part, $nopart="" )

This method provides a shortcut for executing the standard WordPress loop. You simply specify a template part (as a string) for each loop as if using get_template_part().

custom_loop( $custom_query, $part, $no_part='', $var_name='query' )

This serves the same purpose as \NV\Theme::loop() except that it accepts a custom WordPress query. Additionally, you can set the name of the variable you want to use in your template part with the $var_name argument (which prevents conflicts or ambiguity).

output_file_marker( $file )

This method prints out an HTML comment that identifies the (theme-relative) template file currently being used. This should always be passed the __FILE__ magic constant when called.

page_title()

This generates an SEO-friendly HTML title (e.g. <title>) to be used in your header file.

posted_on()

Generates a re-usable snippet for generic “Posted on” text.

\NV\WordPress

The \NV\WordPress class ( in nv\utilities\WordPress.php ) contains basic WordPress-oriented functions that aren’t included in WordPress (but probably should be). These are especially beneficial to some WordPress developers who would otherwise have to handle these use-cases manually.

unregister_post_type( $post_type )

UNregisters the specified post type. Should be used in an init hook.

unregister_taxonomy( $tax )

UNregisters the specified taxonomy. Should be used in an init hook.

is_tax_archive()

Returns boolean indicating whether the current page is a taxonomy archive page.

is_post_type_archive()

Similar to the WordPress function of the same name, but includes is_home() in the check.


This page was last updated on Feb. 20, 2014 – it is currently considered OUTDATED.