Jump to content

mustache.php / Processwire integration - prependtemplatefile


nico65
 Share

Recommended Posts

Hi,
I’m trying to implement mustache.php into processwire.
 
What I did so far and what worked with one problem is:
put the Mustache” Folder in site/template
 
make a _init_mustache.php
<?php

// include Mustache
require __DIR__ . '/Mustache/Autoloader.php';
Mustache_Autoloader::register();

//start the mustache engine
$mustache = new Mustache_Engine(array(
    'template_class_prefix' => '__MyTemplates_',
    'cache' => dirname(__FILE__).'/tmp/cache/mustache',
    'cache_file_mode' => 0666, // Please, configure your umask instead of doing this :)
    'cache_lambda_templates' => true,
    'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/Pattern'),
    'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/Pattern/Partials'),
    'helpers' => array('i18n' => function($text) {
        // do something translatey here...
    }),
    'escape' => function($value) {
        return htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
    },
    'logger' => new Mustache_Logger_StreamLogger('php://stderr'),
    'strict_callables' => false,
));
 
 
call that _init_mustache.php in the _init.php
include_once("./_init_mustache.php");
 
Now I have a site/template/portfolio.php where I call the template
 
$tpl = $mustache->loadTemplate('02-objects-050-image-hovertext'); // loads __DIR__.'/views/foo.mustache';
$content .= $tpl->render(array(
    'alltagstitle' => $alltagtitle,
    'order' => '01',
    'Title' => $title,
    'SRC' => $resized_thumb->url,
));

 

 
this works so far in mysite.com/portfolio
 
but I also call it from here fields/gcp/portfolio.php
$temp = wireRenderFile($config->paths->templates.'/portfolio.php');
$search = explode(",", $srch);
$replace = explode(",", $rplc);

$newtemp = str_replace($search, $replace, $temp);

echo $newtemp;

 

to include the portfolio part on other pages.
(I use the module fieldtype select file”)
 
My problem is now, that on the page where I use that last method I get the error:
Error: Call to a member function loadTemplate() on null (line 71 of /var/…./site/templates/portfolio.php) 
 
And the Notice: 
Notice: Undefined variable: mustache in /var/www/clients/client29/web166/web/eyeonu-v6/site/templates/portfolio.php on line 71 Fatal error: Call to a member function loadTemplate() on null in /var/www/clients/client29/web166/web/eyeonu-v6/site/templates/portfolio.php on line 71
 
line 71 is: $tpl = $mustache->loadTemplate('02-objects-050-image-hovertext'); // loads __DIR__.'/views/foo.mustache';
 
It looks that I somehow messed it up with the prependtemplatefiles, can anyone give me a tip?
Link to comment
Share on other sites

Not sure how to help you, but you might be better off creating a TemplateEngineMustache module that extends the TemplateEngineFactory module:

There is already sub modules created that you could look at for other template engines like

I personally use TemplateEngineFactory with TemplateEngineTwig.

Hope that helps

  • Like 3
Link to comment
Share on other sites

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...