Jump to content

Configuring template path


pcreact
 Share

Recommended Posts

@pwFoo, here is demonstration module you can use as a starting point. Just install the module and then view "My Page".

MyModule.module

<?php namespace ProcessWire;

class MyModule extends WireData implements Module {

    /**
     * Module information
     */
    public static function getModuleInfo() {
        return array(
            'title' => "My Module",
            'version' => 1,
            'autoload' => true,
        );
    }

    /**
     * Init
     */
    public function init() {
        $t = $this->templates->get('my-template');
        if(!$t) return;
        $t->filename = $this->config->paths->MyModule . 'my-template.php';
    }

    /**
     * Install
     */
    public function ___install() {
        $this->createTemplate('my-template', 'My Template');
        $this->createPage('My Page', 'my-template');
    }

    /**
     * Create template
     */
    protected function createTemplate($template_name, $template_label) {
        if($this->templates->get($template_name)) {
            $this->warning("Template '$template_name' already exists.");
            return;
        }
        $fg = new Fieldgroup();
        $fg->name = $template_name;
        $fg->add($this->fields->get('title'));
        $fg->save();
        $t = new Template();
        $t->name = $template_name;
        $t->label = $template_label;
        $t->fieldgroup = $fg;
        $t->compile = 0;
        $t->noPrependTemplateFile = true;
        $t->noAppendTemplateFile = true;
        $t->save();
        $this->message("Created template '$template_name'.");
    }

    /**
     * Create page
     */
    protected function createPage($page_title, $template_name) {
        $page_name = $this->sanitizer->pageName($page_title, true);
        if($this->pages->get("parent=/, name=$page_name")->id) {
            $this->warning("Page '$page_name' already exists.");
            return;
        }
        $p = new Page();
        $p->template = $template_name;
        $p->parent = '/';
        $p->name = $page_name;
        $p->title = $page_title;
        $p->save();
        $this->message("Created page '$page_name'.");
    }

}

 

my-template.php

<?php namespace ProcessWire;

echo "This is the template file for my-template";

 

MyModule.zip

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Hi @Robin S

thank you very much! It was a little bit strange to debug because I needed some module un-/install and module list refreshs until it works, but the problem should be solved by 

  1. change "$this->page->template" (was http404 inside of autoload modules init()) to "$this->templates->get('MyTemplate')"
  2. Add template settings from your example module!  Looks like that was important.     
    $t->compile = 0;	// even though module and template not compiled by FileCompiler?!
    $t->noPrependTemplateFile = true;	// important?? I don't use appended / prepended files
    $t->noAppendTemplateFile = true;	// important?? I don't use appended / prepended files

    If compile = 0 was needed it's strange, because template / module not compiled. I checked it in /site/assets/cache/FileCompiler and logs...

 

 

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