Jump to content

TemplateFileHelper


pwFoo
 Share

Recommended Posts

Module: http://modules.processwire.com/modules/template-file-helper/
Repo: https://bitbucket.org/pwFoo/templatefilehelper/overview

 

TemplateFileHelper module features

  • add global controller and template to current page by a Page::render hook
  • Manage global ($layout) and current page ($view) styles and scripts with a $config->scripts / $config->styles context mapping. So $config->styles / $config->scripts works fine too
  • load sub-templates
    • with a controller file 
    • an array of data to fill template variables
    • just an html template
  • Ajax page load in mind 

Usage

Global layout

A global controller / template is added by a Page::render hook.

/site/templates/_layout.php  // controller 
/site/templates/_layout.tpl  // view / html template

 

Example _layout.tpl

<!doctype html>
<html lang="de">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TemplateFileHelper Processwire</title>
    <?=$styles?>
    <?=$scripts?>
  </head>
  <body>
      <div id="nav"><?=$navigation?></div>
      <div id="pageContent"><?=$pageContent?></div>
  </body>
</html>


Example _layout.php

// MarkupSimpleNavigation
$nav = $modules->get('MarkupSimpleNavigation');
$layout->set('navigation', $nav->render($opts));


// Global and current page styles
$styles = '';
foreach ($layout->styles as $style) {
    $styles .= "<link href='{$style}' rel='stylesheet' class='global'>";
}
foreach ($view->styles as $style) {
    $styles .= "<link href='{$style}' rel='stylesheet' class='current'>";
}
$layout->set('styles', $styles);


// Global and current page scripts
$scripts = '';
foreach ($layout->scripts as $script) {
    $scripts .= "<script src='{$script}' type='text/javascript' class='global'></script>";
}
foreach ($view->scripts as $script) {
    $scripts .= "<script src='{$script}' type='text/javascript' class='current'></script>";
}
$layout->set('scripts', $scripts);

 

Current page

The PW template of current page will rendered inside the global view by Page::render hook.

/site/templates/basic-page.php  // controller 
/site/templates/basic-page.tpl  // view / html template

 

Example basic-page.tpl

<div><?=$contentHome?></div>


Example basic-page.php

$view->set('contentHome', 'Simple output...');
echo $view->render();


Output (for example to debug) is possible too.

echo "My PW template file output...";

$view->set('contentHome', 'Simple output...');
echo $view->render();

 

Sub-templates

It's possible to use sub-templates / chunks inside of a PW template / controller.

Sub-template with controller / view files

$part = $view->load('parts/test1', 'parts/test1');  // relative to /site/templates (view = .tpl, controller = .php
$part = $view->load('parts/test1', true);   // same as above. "True" instead of write identical path / file twice

 

$part = $view->load('parts/test1', 'parts/test1_controller');  // view "parts/test1.tpl", controller "parts/test1_controller.php"


Sub-template with array data

$part = $view->load('chunks/test1', array('variable1' => "value1", 'variable2' => 'value2'));

 

Sub-template just a html chunk

$part = $view->load('chunks/test1');   // view file /site/templates/chunks/test1.tpl

 
 

PW template file as view

Because direct output inside a PW template file is possible it also works without a view.

Example: PW template without viewTested with the FrontendUser module quick and dirty...
 

$fu = $modules->get('FrontendUser');

$fu->login();
$button = $fu->form->fhSubmitBtn;


if (!empty($_GET['logout'])) {
    $fu->logout($page->url);
}


$processed = $fu->process($page->url);
if ($processed && !$user->isGuest()) { // $processed == false if login failed (not submitted / login successful == true)
    echo "Hello $user->name!";
    echo "<a href='$page->url?logout=1'>Logout</a>";
} else {
    echo $fu->render();
}

 


Scripts / Styles context

The module itself takes care about the global (inside _layout.php) and "current" (inside PW template file). Just use PW $config to set styles and scripts.

$config->scripts->add('...');
$config->styles->add('...');

 

You can also force the context by use the additional global api variables.

$layout->scripts->add('...');  // global context
$layout->styles->add('...');   // global context

 

$view->scripts->add('...');  // current page context
$view->styles->add('...');   // current page context

 

  • Like 4
Link to comment
Share on other sites

  • 1 month later...

Hi, I haven't tested it with caching or worked with PW caching yet.
I'll do some tests with PW caching in the future...

The module hookAfter Page::render and add the rendered page content to the global / layout TemplateFile object.

// add current page output to global layout
$this->layout->set($this->currentPageContent, $event->return);
$event->return = $this->layout->render();

Anyone tested this approach with caching?

Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...