Jump to content
pcreact

Configuring template path

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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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'; 

:)

Share this post


Link to post
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.

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

So you mean this should work?

$home = wire("pages")->get("/about/what/");
$home->template->filename = $_SERVER['DOCUMENT_ROOT'] . "/basic-page.php";
echo $home->render();

Share this post


Link to post
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

Share this post


Link to post
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'] . "/";
 
 
 

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

Hello,

How to integrate processWire into Yii framework. Pls, post the tutorials about it. Thanks. 

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

:) 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 :) }

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I am just want to implement yii users module to work together with PW. Just simple as that. It's like a shortcut for me. Don't know if it is a right decision or not.

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Sound strange as I don't see what you can do with yii what you can't with PW. It a bit like using yii with laravel.

  • Like 3

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Matthew, as usual, you provide a deep and eloquent response. Very helpful, thanks! 

Share this post


Link to post
Share on other sites

Is it possible to use processwire without a database? I would like to use the great selector api without having to save all my pages to a db.

Share this post


Link to post
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');

Share this post


Link to post
Share on other sites

Aah, I see. don't know about the pros and cons of doing it that way...but am sure someone else will comment. And no, your question did not sound stupid :D

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      A new module that hasn't had a lot of testing yet. Please do your own testing before deploying on any production website.
      Custom Paths
      Allows any page to have a custom path/URL.
      Note: Custom Paths is incompatible with the core LanguageSupportPageNames module. I have no experience working with LanguageSupportPageNames or multi-language sites in general so I'm not in a position to work out if a fix is possible. If anyone with multi-language experience can contribute a fix it would be much appreciated!
      Screenshot

      Usage
      The module creates a field named custom_path on install. Add the custom_path field to the template of any page you want to set a custom path for. Whatever path is entered into this field determines the path and URL of the page ($page->path and $page->url). Page numbers and URL segments are supported if these are enabled for the template, and previous custom paths are managed by PagePathHistory if that module is installed.
      The custom_path field appears on the Settings tab in Page Edit by default but there is an option in the module configuration to disable this if you want to position the field among the other template fields.
      If the custom_path field is populated for a page it should be a path that is relative to the site root and that starts with a forward slash. The module prevents the same custom path being set for more than one page.
      The custom_path value takes precedence over any ProcessWire path. You can even override the Home page by setting a custom path of "/" for a page.
      It is highly recommended to set access controls on the custom_path field so that only privileged roles can edit it: superuser-only is recommended.
      It is up to the user to set and maintain suitable custom paths for any pages where the module is in use. Make sure your custom paths are compatible with ProcessWire's $config and .htaccess settings, and if you are basing the custom path on the names of parent pages you will probably want to have a strategy for updating custom paths if parent pages are renamed or moved.
      Example hooks to Pages::saveReady
      You might want to use a Pages::saveReady hook to automatically set the custom path for some pages. Below are a couple of examples.
      1. In this example the start of the custom path is fixed but the end of the path will update dynamically according to the name of the page:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'my_template') { $page->custom_path = "/some-custom/path-segments/$page->name/"; } }); 2. The Custom Paths module adds a new Page::realPath method/property that can be used to get the "real" ProcessWire path to a page that might have a custom path set. In this example the custom path for news items is derived from the real ProcessWire path but a parent named "news-items" is removed:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'news_item') { $page->custom_path = str_replace('/news-items/', '/', $page->realPath); } }); Caveats
      The custom paths will be used automatically for links created in CKEditor fields, but if you have the "link abstraction" option enabled for CKEditor fields (Details > Markup/HTML (Content Type) > HTML Options) then you will see notices from MarkupQA warning you that it is unable to resolve the links.
      Installation
      Install the Custom Paths module.
      Uninstallation
      The custom_path field is not automatically deleted when the module is uninstalled. You can delete it manually if the field is no longer needed.
       
      https://github.com/Toutouwai/CustomPaths
      https://modules.processwire.com/modules/custom-paths/
    • By Brian Peat
      Hello! I've got a single client on Processwire and it's completely foreign to me. I've been able to find most things, but they've asked for a custom designed page with a full width header image/section, and then a body block and a right side bar below the header. They're using a landing page template, so I duplicated that, figured out how to add it in the admin, and assigned it to the page. Nothing. It doesn't change a thing.
      I feel like I'm missing something obvious. I've made sure all the fields are the same, I tried to set up the parent/child stuff though this page doesn't have a parent that I can tell (though it's under Landing Pages). If I can get the template to actually kick in, I think I can use a bit of code to check for the hero section and load it at the top. I also made a copy of the widget template and set that to load instead of the original. But again, since my main page template isn't kicking in, neither is the new widget template.
      I'd just love some tips on what to check or change to get a landing page to actually render what's in the assigned page template.
      Hopefully I've used the correct terminology here to make sense.
      Thanks!
    • By rookie
      hi there,
      Is there a module or other solution to select a template with a preview image? Sometimes it is not that easy to describe a template.
      e.g. Template with 3 columns (image, title, content, button) or template for a slider with max. 4 pictures.
      Even if it is nice to have a description field for a template, a picture says more than thousand words could describe.
      And no, I don't think I could develop something like that. I don't even know where to start.🙄
    • By muzzer
      I have just upgraded a site from 2.7 to latest v3.
      I have run into a weird issue where a page template setting for slashUrls changes from No to Yes immediately after an ajax call, causing the second ajax call on the page to use the wrong setting.
      There is no reference to the $template->slashUrls() in any of my code.
      This happens every time I load the page, it's 100% consistent.
      I have this running on a second machine under v2.7 and no such weirdness.
      I'm lost as to why this is happening, what would cause a PW setting change for slashUrls? Anyone have any ideas?
       
       
       
    • By Pip
      Hi everyone, 
      Is there a way for us to replicate the "http://mydomain.com/processwire/page/edit/?id=xxx" and change the template to match my site template? 
      I'm terribly happy with the admin / backend page edit. It covers literaly everything I want to empower my non super admin user in updating pages such as validation, repeater management, file upload. 
      I wish not to allow the users to see the backend for both security and aesthetic reasons. 
      Thanks and hope to hear from you soon. 
       
×
×
  • Create New...