Jump to content

Recommended Posts

Posted

How do I change $page->template->filename in a module? I'm using a the page render hook before and no combination of attempting this has worked. 

To further confuse me... I can't even set the variable in a template directly or using set().

Posted

Copied from the helloworld module and modified. 

public function changeFilename($event) {
  $page = $event->object;
 
  // don't add this to the admin pages
  if($page->template == 'admin') return;

  $page->template->filename = wire('config')->paths->themes.wire('config')->theme->name.$page->template.'.php';
  $event->return = $page->template->filename;

 //wire('template')->filename = wire('config')->paths->themes.wire('config')->theme->name.$page->template.'.php';
  //wire('page')->template->filename = wire('config')->paths->themes.wire('config')->theme->name.$page->template.'.php';
}

in the template i tried

$page->template->set('filename','/httpdocs/tmp/site/templates/dev/basic-page.php');
$page->template->filename = '/httpdocs/tmp/site/templates/dev/basic-page.php';

The $config variables are working fine, but I also tried hard coding the path. No luck, tried both production and newest dev release. I must be missing something.  

Posted

Setting template filename only works if it starts with full root path. Make sure your paths are correct.

The root path you have you can see with:

$this->config->paths->root

So $this->config->paths->templates . "view/mytpl.php"; works fine in a hook

I don't think you can set the template filename in the template for the page as it's already rendering it.



Also $event->return = $page->template->filename; isn't needed if it's in a before render hook.

  • Like 1
Posted

No such luck Soma. I tried all of the following in the module. The paths are correct.

$this->template->filename = $this->config->paths->templates . "view/mytpl.php";
$page->template->filename = $this->config->paths->templates . "view/mytpl.php";
wire('page')->template->filename = $this->config->paths->templates . "view/mytpl.php";
wire('template')->filename = $this->config->paths->templates . "view/mytpl.php";
Posted
Can't say except that the path has to be correct and file exist or it will ignore it.

Ah, I didn't realize the file had to exist. Problem solved. Thank you so much!

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...