Jump to content

Configuring template path


pcreact
 Share

Recommended Posts

Interesting reply Matthew,

Regarding - "The only "advantage" of PHP frameworks over ProcessWire is that frameworks have no back end at all." - I'm sure this is an over-simplification. Maybe it's my inexperience of processwire but it does seem very content-centric, where as I see Yii as a framework that could be used to build any tool or application. The only way I'm going to confirm for myself whether or not this is a good idea is by coming up with some concrete examples....to be continued.

Link to comment
Share on other sites

Greetings,

To pcreact: Remember, I believe that ProcessWire is a framework with CMS components built into it. I operate day-to-day with that as my working vision of ProcessWire.

My comment about "no back-end" is a way of saying that the other frameworks mentioned have just that one bit more of openness and "no assumptions" attitude we all prize. But for me it just means I use a few more lines in my code to tie into the ProcessWire page/field system. Then I am home free to develop exactly like those other frameworks -- but with one very powerful advantage: the querying ease and depth of ProcessWire.

Once you are working entirely in the ProcessWire API, I don't currently know of any applications that could not be built with ProcessWire.

Thanks,

Matthew

  • Like 2
Link to comment
Share on other sites

Just to clarify, I don't deny it's technically possible to use processwire to build the things I would build with Yii, my main concern is about whether or not PW would be the right tool for the job.

Good point. You'll find this stressed by many lead developers here including Ryan. Never shy away from using what you believe is the right tool for you and/or for a particular project :D. Looking forward to reading about your experience with PW once you've put it through its paces :)

  • Like 2
Link to comment
Share on other sites

Greetings,

Good point Kongondo about choosing the right tool for a particular project!

I think it's important to point out that using a framework in conjunction with ProcessWire does not mean the framework is better or can do more.  I like the idea of having one other tool that offers new ideas.

With that said, I want to emphasize that I have drastically reduced my reliance on other frameworks over the past few months (since I started using ProcessWire).

I mentioned it briefly above, but I have to say that I like PHPixie as a partner to ProcessWire.  There is something about PHPixie that seems to meld nicely with ProcessWire thinking.  The system itself, as well as the friendly involvement of the developer, all reminds me of ProcessWire.  I'd be curious to hear what others here think of it: PHPixie Web Site.

Thanks,

Matthew

Link to comment
Share on other sites

  • 2 months later...

Hello,

Since I last posted I've used processwire to create a simple content managed website and as an API data source for a Content-Publishing mobile application. I still intend on using it as a data source from within the Yii framework for the following reasons:

Views

I like that processwire gives you a raw template file and allows you to do what you like to output your HTML, but I found myself just trying to re-create the Themes/Layout/Partials structure provided by Yii.

Folder/Class organisation

With processwire I found myself just adding files to the modules folder, which is fine for a smaller project, but for a large project I would need to implement some sort of system that separated out the different types of modules and organise them accordingly, whereas with Yii I have the out of the box separation of Actions, Behaviors, Components, Extensions, Modules and Widgets.

URL Management

Processwire URL's were perfect for the small site I built, but I felt I had more out of the box control over URL's using Yii, with it's CBaseUrlRule class.

Automated Testing

Yii provides support for automated testing: http://www.yiiframework.com/doc/guide/1.1/en/test.overview

In Summary - I really like processwire and I will definitely continue to use it stand alone, but I also still see how I could benefit from bootstrapping it into Yii, so I will be going ahead with that also.

  • Like 2
Link to comment
Share on other sites

  • 4 years later...
On 1/31/2013 at 3:40 PM, ryan said:

You should be able to do this during runtime:


$page->template->filename = '/path/to/file.php'; 

 

 

I write a new module with it's own template file which I try to store inside of the module directory. Is it possible to persistent set a template path / file to module directory?

$this->config->urls->siteModules . $this . '/templates/mytemplate.php'

The module install() created an template with an hidden page and the page / template should use the template file located in the module directory. Would be nice if I haven't to store that "system template" to the pw templates folder...

Link to comment
Share on other sites

2 hours ago, pwFoo said:

The module install() created an template with an hidden page and the page / template should use the template file located in the module directory. Would be nice if I haven't to store that "system template" to the pw templates folder... 

wireRenderFile() ?

Link to comment
Share on other sites

Thanks @Macrura,

I used it that way with custom templateFile, but will it work for "normal" page load? It's a (system) page with root as parent. Hidden page, but called from the client. I think it would be better to separate the special system template created by the module from normal (user) templates.

No need to change or accidently deleted by user / developer. Just use as given by the module.

Link to comment
Share on other sites

I don't think I fully understand the setup; does your module create a template that it uses - you could just create the template file in your /templates/ directory and then wireRenderFile() the file in the modules from that; but i'm really not clear on the setup so hard to answer coherently.

Link to comment
Share on other sites

The modules creates a template and page as child of "/". It's a hidden page and should only used by the module. So it would be nice to have the module internal template file inside of the module directory instead of the site/templates directory mixed with the "normal" site templates.

The page is called from client side javascript code and need to be in the pw root directory. So it needs a template file otherwise it results in a 404 error message from PW.

Link to comment
Share on other sites

you should be able to set the file that the template uses at runtime, so wherever you render the page:

$page->template->filename = '/path/to/file.php'; 

or 

$t = $templates->get('some-template');
$t->filename = '/path/to/file.php';

quoted from here:

 

  • Like 1
Link to comment
Share on other sites

1 hour ago, Macrura said:

you should be able to set the file that the template uses at runtime

@pwFoo, note that you will also need to disable FileCompiler for the template.

2018-06-18_144910.png.3d8760608db3be68ce0fb54e72cfa2eb.png

That's because FileCompiler forces the $config->paths->templates path at the start of the filename you set for the template, resulting in an invalid path if you used a full path outside of the templates folder for the filename. See herehere and here. You could open a GitHub issue for this - Ryan may or may not agree that it constitutes a bug.

  • Like 1
Link to comment
Share on other sites

@pwFoo, if your page/template/file exists only to give some AJAX response then another approach you could consider is to hook ProcessPageView::pageNotFound. Then you don't need the page/template/file.

In /site/init.php:

$wire->addHookBefore('ProcessPageView::pageNotFound', function(HookEvent $event) {
    $url = $event->arguments(1);
    if($url === '/your-special-url/') {
        $event->replace = true;
        $event->return = 'your response';
    }
});

 

  • Like 3
Link to comment
Share on other sites

@Robin S

Thanks, could Work for me as autoload module If isn't possible to set page template outside of /site/templates.

Existing page would be safer because name / path is blocked.

Or is it possible to build a process module with page parent home page ("/") instead of the backend?

https://processwire.com/api/ref/process/install-page/

 

At the moment I have to copy the template to /site/templates to install the modul ?

Link to comment
Share on other sites

Created a process module with page in frontend / child to "/", but it doesn't work as expected. Process is set, but it returns a "404 error". I think instead using the process module it searching for a template file in site/templates ?

Link to comment
Share on other sites

Verified by a test...

Process is set to the page, but there is a template too. Process is ignored and template used if exists. Without template file I get a 404 error.

So process module in frontend or page without template isn't possible. Also a custom template path outside of site/templates isn't possible.

 

So I have to copy a template file from module folder to site/templates during module install that shouldn't used / modified by user / developer.

Link to comment
Share on other sites

Hi @Robin S,

I know there are workarounds with additional code in init.php or a hook. But I try to save and use the template inside of the modules directory to avoid workarounds outside of the module directory (autoload module with hook, additional code in init.php, copy modules template to site/templates).

A nice solution would be to set the needed template file in modules install() method and save the custom template path to the page / template. ?

Link to comment
Share on other sites

9 minutes ago, pwFoo said:

But I try to save and use the template inside of the modules directory to avoid workarounds outside of the module directory

Nothing is required outside of your module - make your module autoload and use the init() method in your module.

 

10 minutes ago, pwFoo said:

A nice solution would be to set the needed template file in modules install() method and save the custom template path to the page / template.

The filename is not a property that is saved to the database - it is generated at runtime from the template name, so if you want to set a custom filename then this must be done at runtime. The comment in the Template class is clear about this:

@property string $filename Template filename, including path (this is auto-generated from the name, though you may modify it at runtime if it suits your need). #pw-group-files

 

  • Like 2
Link to comment
Share on other sites

Hi @Robin S,

autoload module and init() method doesn't work. I disabled FileCompiler, but still not works.
Because it's redirected to http404 before the existing template is set inside of the module.

    public function init() {
		$this->log->message("ServiceWorkerJS module autoload");

		$template = $this->page->template;
		$this->log->message("TEMPLATE CURRENT: {$template}");

		$file = $this->config->paths->siteModules . $this . '/templates/ServiceWorker.php';
		$this->log->message("NEW RUNTIME FILE: {$file}");

		$template->filename = $file;
		$this->log->message("CHECK TEMPLATE RUNTIME FILE: " . $template->filename);

		//$this->log->message("CHECK PAGE URL: " . $this->page->url()); // DISABLED BECAUSE IT FAILS WITH AUTOLOAD!
    }

 

The log shows the template change runtime works fine, but it's to late to get the correct and expected page. URL is changed to http404.

2018-06-30 09:23:45	admin	http://localhost/pw/serviceworker.js	ServiceWorkerJS module autoload
2018-06-30 09:23:45	admin	http://localhost/pw/serviceworker.js	TEMPLATE CURRENT:
2018-06-30 09:23:45	admin	http://localhost/pw/serviceworker.js	NEW RUNTIME FILE: C:/xampp/htdocs/pw/site/modules/ServiceWorkerJS/templates/ServiceWorker.php
2018-06-30 09:23:45	admin	http://localhost/pw/serviceworker.js	CHECK TEMPLATE RUNTIME FILE: C:/xampp/htdocs/pw/site/modules/ServiceWorkerJS/templates/ServiceWorker.php

2018-06-30 09:23:47	admin	http://localhost/pw/serviceworker.js	ServiceWorkerJS module autoload
2018-06-30 09:23:47	admin	http://localhost/pw/serviceworker.js	TEMPLATE CURRENT:
2018-06-30 09:23:47	admin	http://localhost/pw/serviceworker.js	NEW RUNTIME FILE: C:/xampp/htdocs/pw/site/modules/ServiceWorkerJS/templates/ServiceWorker.php
2018-06-30 09:23:47	admin	http://localhost/pw/serviceworker.js	CHECK TEMPLATE RUNTIME FILE: C:/xampp/htdocs/pw/site/modules/ServiceWorkerJS/templates/ServiceWorker.php

Autoload module is loaded twice during one page (re-)load, but page is redirected before the autoload module changes the template. FileCompiler is off (namespace in files, site/assets/cache/FileCompiler/site/modules/* is empty).

 

Correct page should be instead of "serviceworker.js" instead of "http404". So how to prevent redirect to http404 with my autoload module? Where change the template to be early enough to prevent the redirect to http404?

 

Example module need to deliver a serviceworker file to the browser from the root directory.

 

Autoloaded with condition of template works fine. So initial the template is set, module is autoloaded, but page is redirected to http404.

'autoload'  => 'template=ServiceWorker',

 

There was a module->get() inside of basic-page. Conditional autoload based on template doesn't work. So I changed it to 

'autoload'  => true,

 

Edited by pwFoo
Same information wasn't correct, updated it...
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...