wanze/TemplateEngineFactory

Blank page if template-name.tpl is not existing

tiefenb opened this issue · 7 comments

I'm setting my template file based on a condition and use $view->setFilename.

E.g.
my-tempalte/json.tpl
my-template/html.tpl

if($format == 'json') {
$view->setFilename('my-tempalte/json.tpl');
} else {
$view->setFilename('my-tempalte/html.tpl');
}

Because of this reason I don't need an .tpl file for each Processwire template.
E.g. If I have in Processwire a tempalte called "home", I have to add an emtpy views/home.tpl file to make smarty work for me. If not I would get an empty white page - also if I do $view->setFilename('my-tempalte/json.tpl');

I don't want to blow up my views folder with so much empty files.
Is there a way to avoid this when using $view->setFilename?

I want to get rid of this files at the bottom of the screenshot:
Screenshot

wanze commented

Hi @tiefenb
Where do you set your custom filename, in some controller or prepended template file? You could try the following:

  • Enter a global template file in the smarty module config. This way, the template behind the $view variable always corresponds to this smarty template.
  • Create the global template file

If it works, you should be able to remove your bt-* placeholder templates.

Thx, fixed with this workaround!

Hi @wanze,

I have now a new problem with this setup and https://github.com/dadish/ProcessGraphQL

For the graphql endpoint, there is a new template file which contains:

<?php namespace ProcessWire; echo $modules->get('ProcessGraphQL')->executeGraphQL();

I set the global template file to your 404.tpl and if no view->setTemplate happens, this template file would be displayed. We do not need andy Smarty Template in the the GraphQL Endpoint www.example-pw.com/graphql.

How could we use the global template file and also use the graphql module?

My first guess would be, to create an graphql.tpl and execute wire('modules')->get('ProcessGraphQL')->executeGraphQL() inside the template. But this sounds more like an workaround.

Would be great if their is an option where you can opt-out of the template engine factory rendering and use the regular processwire/php output.

wanze commented

@tiefenb Were you able to solve this problem?

The global template file is the problem here. Usually, if the module cannot find a a template engine file (e.g. Smarty) for the template of the current page, the render hook is not attached. So it would just process the regular ProcessWire template. Because you are using the global template option, the module always falls back to this template. I'm thinking of introducing a hook which allows to overwrite the template to load. Maybe you could solve it by writing a small autoload module having a ready method:

public function ready() {
  if ($this->wire('page')->template->name == 'admin') {
    return;
  }
  if ($this->wire('page')->template->name === 'graph-ql') {
    // Tell the factory to not process this template
    $this->wire('view', new TemplateEngineNull()); 
 }
}

I would consider this as a workaround too, because the above ready method must be executed after TemplateEngineFactory::ready and I don't think we can influence the order. Let me think of a hook for this problem.

Yes I found an other workaround, but not know whats exactly it was, because we don't use the graphql module yet.