Zum Inhalt springen
nnhelpers for WordPress

nnhelpers for WordPress

A toolbox-plugin with a huge collection of one-liners to drastically speed up your plugin-development

About

  • What is nnhelpers?

Getting started

  • Installation
  • Creating a plugin

Custom Post Types (CPT)

  • Creating a Custom Post Type (CPT)
  • Adding forms and fields to your CPT
  • Adding forms and fields to WordPress posts
  • CPT – field types and configurations

Custom Widgets and Blocks

  • Creating a custom widget for Elementor

Basic Concepts

  • QueryBuilder
  • Model
  • Repository
  • Fluid Templates
  • Creating custom ViewHelpers
  • Home
  • Docs
  • Basic Concepts
  • Fluid Templates

Fluid Templates

Table of Contents
  • Using the Fluid Template Engine in WordPress
    • Example
    • Setting Partials- / Layouts- / Template-Rootpaths
    • Alternativ to render():
    • Registering additional ViewHelpers
    • Read on
Table of Content

Using the Fluid Template Engine in WordPress #

One of the essential things missing in WordPress, is a template engine. In WordPress, templates are written in PHP – and often this makes the code very illegible.

The nnhelpers plugin ships with a standalone version of the Fluid Template Engine. This is one of the best template engines out there. It has a long development history, is fast, robust and can easily be extended with custom ViewHelpers.

After installing the nnhelpers-Plugin You can use the Fluid-Template-Engine anywhere in the code of your own WordPress-template like this:

Example #

$vars = [
    'name' => 'John', 
    'hobbies' => ['coding', 'eating', 'fishing']
];
echo \nn\wp::Template()->render( 'EXT:yourplugin/path/to/template.html', $vars );

The EXT: is a special placeholder that will be resolved to the path of your plugin (wp-content/plugins/...). Your Template file could look like this:

<h1>Thing that {name} loves doing:</h1>
<ul>
    <f:for each="{hobbies}" as="hobby">
        <li>{hobby}</li>
    </f:for>
</ul>

Setting Partials- / Layouts- / Template-Rootpaths #

$view = \nn\wp::Template();
$view
    ->setLayoutRootPaths(['EXT:myplugin/Resources/Layouts/'])
    ->setPartialRootPaths(['EXT:myplugin/Resources/Partials/'])
    ->setTemplateRootPaths(['EXT:myplugin/Resources/Templates/'])
    ->assignMultiple(['a'=>123])
    ->render('Show');

Alternativ to render(): #

$view = \nn\wp::Template();
$view
    ->setTemplate('Show')
    ->render();

Registering additional ViewHelpers #

The following code will register an additional ViewHelper namespace that can be used inside of the Fluid-template with {vh:name()} and <vh:name />

$view = \nn\wp::Template();
$view
    ->addViewHelperNameSpace('vh', 'My\\Example\\ViewHelpers')
    ->render( 'EXT:example/path/to/Template.html');

Read on #

  • Overview of the Fluid Core ViewHelpers
  • Documentation of the Fluid Standalone Version
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Still stuck? How can we help?

How can we help?

Updated on 3. Juli 2022
RepositoryCreating custom ViewHelpers

Powered by BetterDocs

Table of Contents
  • Using the Fluid Template Engine in WordPress
    • Example
    • Setting Partials- / Layouts- / Template-Rootpaths
    • Alternativ to render():
    • Registering additional ViewHelpers
    • Read on
nnhelpers for WordPress
Mit Stolz präsentiert von WordPress.