Jump to content
EyeDentify

Change Template file via PW API (SOLVED)

Recommended Posts

Hello Fellow PW Fans and Gurus.

I have run into a problem where i have created a template without a template file associated with it.
Now i would like to HAVE a template file associated with it, not an alternate but as the main template file.

So i thought, that's easy, I upload the template file and change it in template settings but I only get the Alternate template file setting?

Ok so maybe i can change it via the API , so i wrote some code for this.

<?PHP
function changeTemplate($pages = null, $templates = null) {
    /* get the pages to change template on */
    $logItems = $pages->findMany('template=tmp_log_item');
	
  	/* get the template object for our desired template */
  	$Template = $templates->get('tmp_new_log_item');
  
    foreach($logItems AS $key => $logData) {
        
        /* use template object $Template to set template */
      	$logData->template = $Template;
        
        /* save page item */
        $logData->save();
    }
}

/* do the magic change */
changeTemplate();
?>

Needless to say, PW was not happy about this, It throw up an error message:

Error: Exception: Invalid value sent to Page::setTemplate (in /home/virtual/mydomain/public_html/wire/core/Page.php line 1782)


Now from what i understand i gave $logData->template the wrong type of value.
So what should the value be to correct this?

When I read the docs at: https://processwire.com/api/ref/page/

the value can be "string" or "Template".

So i gave it a string and it went haywire.

How should I approach this cause I realy dont want to manually change template file on över 50 plus pages :)

Maybe I should use the Template class to create the value that the API wanted, but I am unsure how to use it.

Thank you for any info you can give me on this.

If you want more info to help me, ask and I will try my best to give it to you.

Updated and Solved
I solved the problem and have updated the code to reflect this.

Essentialy i used the get() method of $templates to get the template object for my template i wanted to use using the template name.
I gave the Object to the $logData->template property and all went well.

We learn something new every day. :)

One Note:

The template file you want to use must have been uploaded and added as a template in the Template section in the Admin for this code to work as expected.

Edited by EyeDentify
Updated the post to correct som variable/property namnings and make it more readable.

Share this post


Link to post
Share on other sites

I realise this was a one-off but just wanted to point out that calling the get() inside the foreach loop is not very efficient. It is better to do that (once) outside the foreach since it is the same template you are retrieving each time. 

Edited by kongondo
  • Like 1

Share this post


Link to post
Share on other sites
2 minutes ago, kongondo said:

I realise this was a one-off but just wanted to point out that calling the get() inside the foreach loop is not very efficient. It is better to do that (once) outside the foreach since it is the same template you are retrieving each time. 

You are absolutly right. I was thinking of this but i forgot at the last minute. 😊

Share this post


Link to post
Share on other sites
1 hour ago, EyeDentify said:

I have run into a problem where i have created a template without a template file associated with it.
Now i would like to HAVE a template file associated with it, not an alternate but as the main template file.

Maybe I'm misunderstanding the situation here, but it isn't necessary to do anything in the PW admin or via the API to associate a template file with a template - you just add a file named the same as your template to /site/templates/. So if you created a template named "my_template" then you add a file "my_template.php".

You don't have to create the template and the template file at the same time. You could create a template last week and add the file today and it is automatically associated with the template if the name is the same.

  • Like 1

Share this post


Link to post
Share on other sites
5 minutes ago, Robin S said:

Maybe I'm misunderstanding the situation here, but it isn't necessary to do anything in the PW admin or via the API to associate a template file with a template - you just add a file named the same as your template to /site/templates/. So if you created a template named "my_template" then you add a file "my_template.php".

You don't have to create the template and the template file at the same time. You could create a template last week and add the file today and it is automatically associated with the template if the name is the same.

You are talking about the Alternate template file functionality. Then Yes, you can have a file uploaded and it would get used by the template that has a alternate template assigned with the same name as the one you uploaded.

But in my case i hade Created a Template without a file in Admin, but later i wanted it to have a file for views, but not an alternate file.
So my code fixes this by using a Template that i added with a associated file to go with it.

Share this post


Link to post
Share on other sites
10 hours ago, EyeDentify said:

You are talking about the Alternate template file functionality.

No, I'm not referring to the Alternate template file feature. I'm just saying that if you create a template without a file, and later decide you want to add a file for it, all you need to do is put that file (named the same as the template it relates to) into /site/templates/. Maybe you never tried it that way before but give it a go and you'll see what I mean.

  • Like 2

Share this post


Link to post
Share on other sites
On 2017-06-06 at 0:29 AM, Robin S said:

No, I'm not referring to the Alternate template file feature. I'm just saying that if you create a template without a file, and later decide you want to add a file for it, all you need to do is put that file (named the same as the template it relates to) into /site/templates/. Maybe you never tried it that way before but give it a go and you'll see what I mean.

Oh. Ok i see what you mean. I have tried what you are talking about, but i need to associate the file with a Template, and to remove the file from list of files without a Template that gets shown when you click "new template" in admin.

then i have to change every Template manually in the tree for every page.

There is no option in admin when you edit a Template to change the Orignal associated file (if any) or associate a new one if no one was chosen when creating the Template without a file.

Therefore i needed the code.

I don´t see the Automatic association you are talking about.

Maybe i misunderstood you.

On 2017-06-06 at 0:29 AM, Robin S said:

No, I'm not referring to the Alternate template file feature. I'm just saying that if you create a template without a file, and later decide you want to add a file for it, all you need to do is put that file (named the same as the template it relates to) into /site/templates/. Maybe you never tried it that way before but give it a go and you'll see what I mean.

Anyway, i have resolved the issue. i Thank you all for your input.

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 marcus
      wireshell 1.0.0 is out    
      See Bea's post
       


      -------- Original post -----------
        Now this one could be a rather long post about only an experimental niche tool, but maybe a helpful one for some, so stay with me   Intention Do you guys know "Artisan" (Laravel) or "Drush" (Drupal)? If not: These are command line companions for said systems, and very useful for running certain (e.g. maintenance, installation) task quickly - without having to use the Admin Interface, first and foremost when dealing with local ProcessWire installations. And since it has a powerful API and an easy way of being bootstrapped into CLIs like this, I think such a tool has a certain potential in the PW universe.    It's totally not the first approach of this kind. But: this one should be easily extendable - and is based on PHP (specifically: the Console component of the Symfony Framework). Every command is tidily wrapped in its own class, dependencies are clearly visible, and so on.   ( Here was the outdated documentation. Please visit wireshell.pw for the current one )
    • By horst
      Change Default Language to be None-English | Walk Trough
      When you start a new (single) language site and the default language shouldn't be English, you can change it this way:
      Go to the modules core section:

       
      Select the Language ones by the filter function:

       
      We have four language related modules here, but for a single language site in none english,
      we only need the base module, named "Languages Support". So go on and install it.

       
      After that, you can leave it, ... 

       
      ... and switch to the newly created Language section under SETUP:

       
      Select the default language

       
      Enter your new language name or its Shortcut and save the page.
      I will use DE for a single language site in german here as example:

       
      Now I go to the ProcessWire online modules directory, down to the subsection for language packs and select
      and download my desired (german) one: 

       

       

       

       
      After downloading a lang pack as ZIP, I go back into my SETUP > LANGUAGES > default language page in admin,
      select the downloaded lang pack ZIP and install it:

       

       

       
       
      After the ZIP is uploaded, the files are extracted and installed, most of my screen is already in
      the new default language. To get all fully switched, we save and leave that page, ...

       
      ... and completely logout from the admin.

       
      Now, of course, we directly login back, ...

      ... and see, that now also the cached parts of the admin have switched to the new default language. 🙂

       
      That was it for a single language site in none english.
       
      If you want to have a multi language site, just add more languages to the SETUP > LANGUAGES section.
      When using a multi language site, I think you also want to use multi language input fields, and maybe different page names for your language page pendents. If so, you need to go into MODULES > CORE > filter LANGUAGE and install what you need or want to use of it, (if not already done).
      Thanks for reading and happy coding, 🙂
       
    • By quickjeff
      Hi Guys, 
      I have been debugging a site for the last 2 hours and cannot solve the issue. 
      I have a site running on 3.0.148. 
      I installed the Kongondo Blog module and was updating the templates to include the website style. 
      Once everything was set and done, I checked the page tree to see an error appear. 
      Template must be assigned a name before 'filename' can be accessed
      The same error appears in templates. 
      Debugging Steps
      I checked the templates in the server to ensure I didnt accidentally delete the namespace.  Deleted cache in browser and server under assets Still no go. 
      Any help is appreciated. 
      Thanks! 
    • By Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
×
×
  • Create New...