Jump to content

Configuring template path


pcreact
 Share

Recommended Posts

Hello,

I discovered processwire a couple of days ago and I am very impressed so now I am integrating it with the Yii framework (once complete I'll post a tutorial with code), so far it is going well but I do seem to have a problem with setting a new path for a $page->template->filename.

Setting $config->paths->templates in the PW config.php file works fine but I want to be able to set Yii-specific config variables after I have included the PW index.php.

I assumed that by setting $wire->config->paths->templates that this would be reflected in $page->template->filename but that doesn't seem to be the case. Is this a bug or am I confused?

Any advice greatly appreciated.

Link to comment
Share on other sites

Welcome pcreact

I'm not sure why you want to do this, but I did a quick test and I also can't get it to overwrite after a bootstrap. It seems the config after bootstrap is somehow not recognized when rendering a page after that and already set to the system. I tried with a hook or set the template file before rendering but couldn't get it to work. But I am sure there's something I'm missing.

Link to comment
Share on other sites

Thanks for your input, the reason I was trying to do this was to get template->filename in the context of my Yii application to reference the template file in my root/to/yii/templates/ folder.

I use the same method to set config->urls->root to my processwire folder which works fine.

I have realised since that I was maybe on the wrong track, I now just use template->name and handle all of my path generation with Yii.

Link to comment
Share on other sites

You should be able to do this during runtime:

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

or 

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

Note that /path/to/file.php must be a full path and filename relative to the server's file system, but PW won't accept it unless the file ultimately lives somewhere under your web root where PW is installed. Meaning, you can't do this:

$t->filename = '/etc/passwd'; 

:)

Link to comment
Share on other sites

You should be able to do this during runtime:
$page->template->filename = '/path/to/file.php'; 

or 

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

Note that /path/to/file.php must be a full path and filename relative to the server's file system, but PW won't accept it unless the file ultimately lives somewhere under your web root where PW is installed. Meaning, you can't do this:

$t->filename = '/etc/passwd'; 

:)

That's what I've tried in all variants and it doesn't work for some reason. 

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

doesn't get recognized in a bootstrap.

Link to comment
Share on other sites

What is your /path/to/file.php?

Here's the actual code from Template::setFilename that gets executed when you do $somepage->template->filename = '/path/to/file.php';

        protected function setFilename($value) {
                if(empty($value)) return;

                if(strpos($value, '/') === false) {
                        $value = $this->config->paths->templates . $value;

                } else if(strpos($value, $this->config->paths->root) !== 0) {
                        $value = $this->config->paths->templates . basename($value);
                }

                if(is_file($value)) {
                        $this->filename = $value;
                        $this->filenameExists = true;
                }
        }

Look at the "else" condition above, which is what's enforcing your template file to be somewhere below PW's root installation dir. 

Link to comment
Share on other sites

$_SERVER['DOCUMENT_ROOT'] would have to be the same as $config->paths->root in order for that to work. Usually they would be, but sometimes they aren't, whether due to a subdirectory install or aliases/symbolic links. You'd probably want to set it like this instead, which should work (unless I'm forgetting something): 

$home->template->filename = wire('config')->paths->root . 'basic-page.php';

Also, since your basic-page.php is not protected by PW's .htaccess, put this at the top:

if(!defined("PROCESSWIRE")) die('No access'); 
  • Like 1
Link to comment
Share on other sites

Ah now, my local XAMPP has symlink to htdocs. Using wire('config')->paths->root it works.

But doing this doesn't.

wire("config")->paths->templates = wire("config")->paths->root;
$somepage = wire("pages")->get("/about/what/");
echo $somepage->render();
But setting the paths->templates in config.php does work even with $_SEVER['DOCUMENT_ROOT'].
$config->paths->templates = $_SERVER['DOCUMENT_ROOT'] . "/";
 
 
 
Link to comment
Share on other sites

I don't think there's a way to reliably change system paths after bootstrap has occurred. I also don't recommend trying it, just because it seems like there's potential to break things by doing that, and it may work in some cases and not others. Ultimately it wasn't designed for runtime changes to system paths. It probably works from $config.php just because all that happens before bootstrap. It is however designed for runtime changes to template paths via the $template->filename = '...'; syntax. 

Link to comment
Share on other sites

  • 4 months later...

Not sure you'll get a good reply on that just because I'm not aware of anyone else here using the Yii framework. However, if you think of your ProcessWire template files as regular PHP files (which is essentially what they are) you can easily include any other things that you want. I would start there and simply include() your Yii framework file in your template file and see how that works. 

Link to comment
Share on other sites

Greetings,

Interesting to see someone mention Yii here. I have spent a lot of time with Yii, and before that with CodeIgniter and Laravel.

I think it is an interesting topic -- integrating framework elements into ProcessWire. But I think it belongs in a separate discussion from "configuring template path." I started one here: http://processwire.com/talk/topic/3934-integrating-framework-elements-into-processwire/

Thanks,

Matthew

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

:) I just try to include Yii to my blank template named blast.php

<?php
include("./mb/index.php");

my yii are inside /site/templates/mb/

Just Got this error :

Error:     Exception: Unable to resolve the request "blast/index". (in
D:\wamp\www\blaster\site\templates\mb\framework\web\CWebApplication.php
line 286)


#0
D:\wamp\www\blaster\site\templates\mb\framework\web\CWebApplication.php(141):
CWebApplication->runController('blast/index')

#1 D:\wamp\www\blaster\site\templates\mb\framework\base\CApplication.php(180): CWebApplication->processRequest()

#2 D:\wamp\www\blaster\site\templates\mb\index.php(35): CApplication->run()

#3 D:\wamp\www\blaster\site\templates\blast.php(2): include('D:\wamp\www\bla...')

#4 D:\wamp\www\blaster\wire\core\TemplateFile.php(125): require('D:\wamp\www\bla...')

#5 [internal function]: TemplateFile->___render()

#6 D:\wamp\www\blaster\wire\core\Wire.php(271): call_user_func_array(Array, Array)

#7 D:\wamp\www\blaster\wire\core\Wire.php(229): Wire->runHooks('render', Array)

#8 D:\wamp\www\blaster\wire\modules\PageRender.module(250): Wire->__call('render', Array)

#9 D:\wamp\www\blaster\wire\modules\PageRender.module(250): TemplateFile->render()

#10 [intern
 

Haven't found the solution yet... maybe some people who already trying can shed the light to me...

{I am newbies in both yii and processwire :) }

Link to comment
Share on other sites

Greetings,

This general idea of linking a framework with ProcessWire is interesting.

Before I started using ProcessWire, I used CodeIgniter mostly, and was starting to get into Laravel. More recently, I have gained greatest respect for Yii.

All of my development work is in ProcessWire these days. But I'm curious -- what are your goals are for linking ProcessWire with Yii? What can you accomplish by combining the powers of each one?

There are already a couple of discussions on this topic, but again, I wonder if maybe we need one where we just look at PW/framework partnering?

Thans,

Matthew

Link to comment
Share on other sites

Error:     Exception: Unable to resolve the request "blast/index". (in

D:\wamp\www\blaster\site\templates\mb\framework\web\CWebApplication.php

line 286)

The exception is coming from Yii rather than ProcessWire, so it's probably a question we can't answer here (unless someone here is an experienced Yii user). But it looks to me like Yii has a controller that is monitoring the request URI "/blast/" and trying to run a Blast::index() method or something like that. Basically, it looks like Yii's version of a 404. You probably need to somehow turn of its URL-to-function mapping, or tell it not to handle the request. 

Link to comment
Share on other sites

  • 1 month later...

Just thought I should add an update as I promised tutorials!

Unfortunately I got massively side-tracked in the middle of my integration and am only now getting back to working with processwire. I am still going to do the integration, hopefully before christmas.

I haven't used either Yii or PW for a while, but my motivation was that I am more familiar with Yii and have used it to build complex systems, it provides many helpers and if memory serves me right I remember being able to do much more out-of-the-box with the "view" portion of Yii than I could with PW, but I couldn't find any good pre-built CMS components.

That's where I got the idea of using PW purely to build up my content trees and create/access the content via the PW API. Url management, routing, user managerment, templating, widgets, business logic etc would all still be powered by yii.

Link to comment
Share on other sites

Greetings,

This is a terrific discussion, which opens up a lot of ideas in my mind.

Over the past couple of years, I have spent a lot of time using various PHP frameworks (especially like Yii). I would agree with Soma that -- for the most part -- ProcessWire allows you to accomplish the same goals as these frameworks.

There is a lot to say here, but I'll try to summarize:

ProcessWire's Huge Advantage: Querying and Syntax

I find that the syntax of ProcessWire is far more concise and "expressive" (borrowing a favorite Laravel term) than these PHP frameworks. The ability to design very deep queries really quickly and easily is the greatest advantage of ProcessWire.

"Framework" vs "CMS"

I believe ProcessWire should be described as a framework with crucial pre-built CMS components. Currently, ProcessWire is described as a CMS with a framework behind it. The problem is that when it is described as a CMS, we naturally see comparisons with WordPress, Joomla, and Drupal, and anyone who spends time with ProcessWire immediately realizes it is not the same type of thing as these CMSs. If it were presented as a framework, it would attract a whole different set of people with (I believe) more accurate assumptions.

Future Development and Framework Choices

Nothing replaces ProcessWire, but I gain something using other frameworks that I often can come back and use in ProcessWire. My very rapid framework rundown... CodeIgniter is essentially dead. Laravel, which seemed great at first, has become overly convoluted. Yii has the best balance as a framework, but when I compared the time and effort to get an application up and running in ProcessWire vs Yii, ProcessWire wins. Lately, I have been experimenting with PHPixie (yes, I am always trying new things), and it is really nice. In fact, I see a lot of similarities between PHPixie and ProcessWire.

What Do Frameworks Have Over ProcessWire?

The only "advantage" of PHP frameworks over ProcessWire is that frameworks have no back end at all. ProcessWire's back end is extremely minimal, and really does serve the most necessary elements. But when building custom UIs, I often want to just go directly to the database with no assumed page concept at all. With that said, I have developed a set of methods that nicely takes care of this in ProcessWire. And ProcessWire has such an elegant query and syntax system, it saves massive time overall as compared with one of these frameworks. So there is a bit more time getting your custom UI CRUD going, but after that you save huge time using ProcessWire's data-handling system.

Sorry if this got a bit far from the direct subject, but it seems that people reading this thread might be asking some of the same questions I have asked about PHP frameworks and ProcessWire.

Thanks,

Matthew

  • Like 3
Link to comment
Share on other sites

I thought when only using in-memory PageArrays the queries are not executed in mysql. I know my question probably sound quite stupid, and in most cases it wouldn't make any sense.

I just want to create a bunch of pages on each request and query them. I only want to bootstrap pw, I have no need for a backend. Something like this:

$pa = new PageArray();
foreach ($mydata as $data) {
  $p = new Page();
  $p->title = $data['xy'];
  $pa->add($p);
}
// and later I want to query my pages like this:
$pa->find('title=abc');
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...