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 donatas
      Hi,
      I am trying (wondering if even possible) to set a very different file folder path for my PW install. The path I want to set exist in the same server, but in another domain. Can the PW API see that far into file system? It is a shared hosting environment so no permission management options available.
      Also can this be achieved through .htaccess redirection? If you request a file (images mostly, but translation json files are important too) from `domain-A.com/site/assets/files/` to grab them from `domain-B.com/site/assets/files/`? I would prefer to do this through PW API, I can see the function `$config->setPath()` but it didn't work when set in  config.php like `$config->setPath("files", "/domains/domain-B/public_html/site/assets/files/")`.
      Is there some other option to do this?
      My ultimate goal is to have two PW installs on different domains but one is just a "mirror" that is using the same database as the other and should use the same files structure if editors upload any images to the main (domain-B.com) website. I could use domain parking function but it then needs a more expensive SSL certificate for two domains , which I am trying to avoid if possible 🙂 (I'd like to use single domain Let's Encrypt certificates, thus I need to PW installs).
      Would appreciate any insight! Thanks!
    • By franciccio-ITALIANO
      Hi everyone.
      I've created 12 templates that are the same but each with an extra bit of html code. 
      The piece of code is as follows:
      <div> <div class="box-pf"> <i class="fa fa-map-pin fa-2x fa-red faa-pulse animated"></i> <a href=""> <span class="uk-text-middle"><i>Sonchus oleraceus</i> 'Grespino degli Orti'</span></b> </a> </div> </div> On the third line we read "fa-red."
      I created 12 similar templates.
      The first template has only one box with fa-red, the last template has 12 boxes with icons of 12 different colors.
      So. is there any way to have only 1 template and add, if I want and when I want, a small or big, same or different piece of html code?
       
    • By Robin S
      If your module has a lot of config fields you might want to divide them into groups inside a tabbed interface. Here is a demonstration module showing how this can be done.
      https://github.com/Toutouwai/ModuleConfigTabs

      Thanks to @kixe for providing my starting point in this forum topic.
    • By jploch
      Hey folks,
      Iam working on a module that extends InputfieldPageTable. I use PageTableExtended as a starting point and all went well so far, except I can't access my config values outside of the module file. The config values allways return the defaults, and not the values in the pw field settings. 

      Here is the code that sets the defaults inside my inputfield module file:
      public function init() { parent::init(); // defaults $this->set('gridMaxWidth', '1600'); $this->set('gridColumns', '12'); } Here is how I add the config field:
      public function ___getConfigInputfields() { $inputfields = parent::___getConfigInputfields(); $fieldsetGrid = $this->modules->get('InputfieldFieldset'); $fieldsetGrid->label = $this->_('Default Grid Settings'); $f = $this->wire('modules')->get('InputfieldText'); $f->attr('name', 'gridMaxWidth'); $f->attr('value', $this->gridMaxWidth); $f->label = $this->_('Container max width'); $f->description = $this->_('Grid container max width in px'); // page name format description $f->notes = $this->_('default is 1600'); // page name format notes $fieldsetGrid->append($f); $inputfields->append($fieldsetGrid); return $inputfields; } Accessing the values outside my module files like this (this gives me the default: 1600, even if I insert another value in the backend field settings):
      $default = $this->wire('modules')->get('InputfieldPageGrid'); $default->gridMaxWidth; Any Ideas?
    • 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/
×
×
  • Create New...