Jump to content

applab

Members
  • Posts

    32
  • Joined

  • Last visited

Posts posted by applab

  1. On 3/10/2023 at 6:44 PM, bernhard said:

    I haven't read everything carefully and I have never used the feature myself, but TracyDebugger has a feature to render different template files with a customizable suffix. Here is how adrian implemented it: https://github.com/adrianbj/TracyDebugger/blob/f2fbcd88fcecad45b2ce7b428cfe80c1527c1122/TracyDebugger.module.php#L1481-L1494

    Hi @bernhard, sorry for the delayed response but I'm flitting between 2 projects and have just come back to this.

    From looking at that Tracy source code and reading the Tracy docs it looks like the method used relies on the alt-template being in the main /site/templates/ folder and having a recognisable suffix.  My goal is:
    1) to keep dev templates outside of the main templates folder (eg. have /site/templates/ and /site/dev-templates/)
    2) for /site/dev-templates/ not to require a full set of templates, just one or two that are being worked on, and to 'fallback' to the main templates folder if there is no tpl for the current request in dev-templates.

    My problem is that include files and  assets are no longer in the same relative location if the alt-template is used  when the dev-tpl is not in the same folder as the main tpl.

    TBH, it's not super-high priority but I think it would be useful both when collaborating or when modifying a live site so I will come back to it at some point.

  2. Hi @monollonom

    Doing it this way still encounters the problem I had with my original method, ie. it finds the alternate template but require/include files aren't found as relative paths are now different.  I'm not sure how I'm going to approach that, I will give it some thought over the weekend.

    BTW, because I have an extra level of folders I had to modify your code slightly to append a / after the username.

    $userTpl = str_replace("templates/", "templates-dev/" . $event->user->name . '/', $tpl->filename);

    Thanks for giving it some thought :-)

  3. 13 hours ago, monollonom said:

    I actually share a similar setup but for another use (templates-dev is for local development / preview) and in my case I use this in my config.php:

    $config->urls->templates = $config->urls->site . "templates-dev/";
    $config->paths->templates = $config->paths->site . "templates-dev/";
    $config->urls->fieldTemplates = $config->urls->site . "templates-dev/fields/";
    $config->paths->fieldTemplates = $config->paths->site . "templates-dev/fields/";

    Similar to what @androbey suggested. However I don’t switch based on the user but it's easy to do so in your site/init.php

    if($user->name === "username") {
    	// ...
    } elseif($user->name === "admin") {
    	// ...
    }

    Thanks @monollonom, though as per @androbey's suggestion the conditional is not necessary if the username matches the folder name and can be used directly to construct a path.

    On a side note, I wasn't aware that fieldTemplates was a thing until I saw your post so I've learned something very useful :-)

    • Like 1
  4. 13 hours ago, androbey said:

    @applab

    I actually encountered a similar problem some time ago.
    But there is actually a setting option so that the paths can also remain separated. Hopefully this works for you, too.

    $config->setLocation('templates', $config->path('site') . 'templates-dev/' . $user->name . '/');

     

    This is exactly how I usually approach it.  In this case though I was hoping to achieve a setup where each dev's folder does not need to contain a full set of template files, just the one or two they are working on, and for any template not found in the dev's folder it will  fallback to the main template directory, ie. the same way Wordpress child themes work.

  5. Hi,

    I'm trying to change a template file at runtime based on the logged in users name.

    in addition to the /site/templates/ folder I have a /site/templates-dev/ folder, and under that is a folder for each developer.

    so, if I'm logged in as `timmy` and a file exists at `/site/templates-dev/timmy/article.php`  then that file should be used, otherwise use `/site/templates/article.php`.

    in /site/ready.php I have

    $fp = $config->path('site') . 'templates-dev/' . $user->name . '/' . $page->template . '.php';
    if(file_exists($fp)) {
        //echo "found tpl!";
        $page->template->filename($fp);
    }

    If I uncomment the `echo` statement then I can see that it's finding the alternate template but if I view the page it's just blank (status 200, 'this request has no response data available').

    Is anyone able to point me in the right direction please?

    Thanks,
    Martin.

     

     

     

     

  6. Hi @Zeka, 

    Yes, I am using $modules->get("MyModule") in my front-end template.  I was just surprised to see MyModule get installed as a result of that code rather than throwing an error, which is what I was expecting. 

    I've achieved my original goal of preventing the install in that situation but I've realised that I still need to use isInstalled() in my front-end templates to avoid runtime errors so, thanks.

  7. Hi All,

    I'm making my first serious foray into module development and I've discovered some behaviour which I found surprising...

    If I uninstall my module from the backend but do not delete the file, then I refresh a front-end page that is using that module, I would expect to see some kind of error but what happens is the module gets automatically re-installed.

    I can mitigate this behaviour by wrapping my front-end code in:

    if(modules()->isInstalled('MyModule')) { ... }

    but that doesn't seem like the best approach if the module is used in multiple places.

    Is there something I can add to my modules install() method to prevent it getting auto-installed in this situation?

    Thanks.

  8. 3 hours ago, BillH said:

    Or I could improve the method!

    My method is this...

    The user will register using their email address and never actually know the username that's used to log them in behind the scenes.  On registration the username will be derived from the email address and if there's a clash I'll simply add an incremental numeric suffix.  When the user comes to login, the username is looked up from the email address (which is, of course, unique) and it doesn't matter if a suffix has been added or not, as long as it's the right username for the email address.

  9. I had a feeling that the failure of

    $session->login($email, $pswd);

    might be related to email sanitization, but since my solution worked I gave up pondering.

    Before trying it I didn't expect it to work as I thought that without a logged-in admin session I wouldn't have permission to query admin pages.

    But your solution of using a sanitized email as the username and then sanitizing $input->post->email before calling $session->login() also makes a lot of sense.  I'll be doing the registration form next so I might adopt that approach.

  10. I have configured ProcessLogin to use email instead of username (and  enforced unique values for the email field).  I can login to my admin just fine using an email address but I'm trying to create a front-end login form and it's failing.  I've copied the example from https://processwire.com/api/ref/session/login/ and replaced the two params with hard-coded values known to work for backend login but I just get 'Sorry Bob'.

    Anyone have pointers on how to get this working from the front end?

     

  11. Hi Sierra,

    It looks like you haven't disabled the 'automatic prepend/append' option for your template.

    Navigate to Setup->Templates->YourTemplate

    Go to the 'Files' tab and tick the two 'Disable automatic...' boxes.

    Does that fix it?

     

    • Like 3
  12. I'm still at the shallow end of the learning curve with Docker so I can't answer from experience but, AIUI...

    Yes, build a Docker image locally for your app/site and it should run seamlessly on any Docker-supporting host, which includes things like AWS.

    Regarding performance, according to this IBM research paper
    "The general result is that Docker is nearly identical to Native performance and faster than KVM in every category"
    http://domino.research.ibm.com/library/cyberdig.nsf/papers/0929052195DD819C85257D2300681E7B/$File/rc25482.pdf

     

    Just found this, it looks like others have made more progress

     

  13. thanks Teppo, worked great after 1 amendment:

    on first attempt I got an error:

    "Error: Exception: Can't save field from page 1028: /blog/posts/test-post/: Call $page->setOutputFormatting(false) before getting/setting values that will be modified and saved."

    so, I did as the error suggested end ended up with:

    $comment = new Comment;
    $comment->text = "my text";
    $comment->cite = "my name";
    // etc.
    
    $p = $pages->get('/about/');
    $p->setOutputFormatting(false);    // added
    $p->comments->add($comment);
    $p->save('comments');
    
    

    checking the PW cheatsheet for $page->setOutputFormatting() makes it clear why that's needed.

    • Like 5
  14. I have a template which includes a "comments" field using the core Comments field type (FieldtypeComments)

    Is there a way to post a comment to a page which uses this field, programmatically, from a different page?, ie. not the current $page.

    something along the lines of:

    $my_comment = array(
        'cite' => $comment_cite,
        'text' => $comment_text,
        'created' => $comment_date
    );
    
    $p = $pages->get('my-selector');
    $p->comments->add($my_comment);    // no such method!
    

    tia.

  15. Hi All,

    I've just noticed that the latest stable version has moved on to 2.4, when did that happen?, did I miss an announcement??

    I'm too busy to download and take a look right now, is there a changelog anywhere that will inform us what to expect in the new version?, or maybe a forum post that I should check out?

    cheers,

    Martin.

×
×
  • Create New...