Skip to content

render engine

Alberto Parziale edited this page Aug 30, 2019 · 1 revision

Render Engine

Want to use a custom template in your code? Aeria is great for that.

Let's say we want to display a welcome message in admin pages, showing the admin name. We can create a directory to contain the templates, for example [your_theme]/views/. Now, we're gonna insert a php template inside it and save it as TestTemplate.php. Any parameter needs to be contained inside the $data array.

<h1>Welcome to this great Caffeina website, <?=$data["name"];?>.</h1>

Aeria doesn't know where to look for templates! So, we're gonna register a new directory to look in, in our functions.php file:

aeria('render_engine')->addRootPath(__DIR__."/views");

Now, we only need to ask the RenderEngine to render the template at the right time, so we're gonna hook it to in_admin_header.

add_action('in_admin_header', function (){
  $data["name"] = wp_get_current_user()->get("user_login");
  aeria('render_engine')->render('test_template', $data);
});

Please note that Aeria converts filenames to snake_case, and you can fetch your templates by inserting their snake_cased name in render(string $name, array $data).

Admin notices

Every user needs a bit of conversation! You can display admin notices using Aeria's render engine. Aeria needs 3 informations:

  • $data['type'](string, required): the admin notice type. You can choose between:
    • 'error' for a red notice.
    • 'warning' for a yellow notice.
    • 'success' for a green notice.
    • 'info' for a blue notice.
    • '' for a white notice.
  • $data['dismissable'](boolean, optional): by default false, when true makes the notice dismissable by an 'x'.
  • $data['message'](string, required): the displayed message.

You can invoke the engine's admin notice template with the key 'admin_notice_template'.

Look at the previous example. We now want to display our welcome message inside an admin notice. Nothing easier than that.

add_action('admin_notices', function (){
  $username = wp_get_current_user()->get("user_login");
  $data = [
    'type' => 'info',
    'dismissable' => true,
    'message' => "Welcome to this great Caffeina website, ".$username
  ];
  aeria('render_engine')->render('admin_notice_template', $data);
});
Clone this wiki locally