Jump to content

Change Template of current page?


thomas
 Share

Recommended Posts

Hi,

I'd like to have an "edit" option for pages in the frontend. I thought I could append an urlSegment to the page url and (instead of coding show and edit functions into one template) simply switch templates:

if($input->urlSegments[1] == 'edit') $page->template=$templates->get('page_edit');

This code doesn't work ... Is this even possible or do I need to redirect or put all code in one template?

Thanks!

thomas

Link to comment
Share on other sites

Sorry Thomas could you explain a little more what you're hoping to do?

If someone goes to my-page/edit you want that page to have its template changed?

You could try:

<?php

if ($input->urlSegment1 == "edit") {

$page->template = "new_template";
$page->save();
echo "Page now has the new template!";

} ?>

But I'm not sure how you want to set which template it changes to or even if this is your aim?

ALSO: Have you turned on urlSegments on the template?

Link to comment
Share on other sites

The answer is on the foot.inc of the default theme:

// If the page is editable, then output a link that takes us straight to the page edit screen:
if($page->editable()) {
   echo "<a class='nav' id='editpage' href='{$config->urls->admin}page/edit/?id={$page->id}'>Edit</a>"; 
}

This is the small EDIT button that appears on the upper-left corner of every page. To do exactly what you want, you can change your code based on it:

if($input->urlSegments[1] == 'edit') $session->redirect($config->urls->admin . "page/edit/?id=" . $page->id);
Link to comment
Share on other sites

Thanks for your answers,

I achieved want I wanted with hooking before render, now I only need to figure out if this is actually a good idea.

Instead of using a lot of if() or different include() I thought I could use a different template for the current page, depending on a condition. So I built a "profile" template and a "profile_edit". If someone accesses his profile ('/profile/my_fancy_user_name') the link to edit his own profile is ('/profile/my_fancy_user_name/edit') ... it works, but I already encountered the first drawback: the two templates need to have the same fields. So I might just go for the "include()"

Sorry, I should stop typing while thinking ...

t

Link to comment
Share on other sites

Do you want to build a form to edit data on a page or is it on another page? Is it possibly a PW user?

So it's not the "page" you're looking at, like in the admin backend edit screen? In this case you could load the edit screen of PW with /processwire/page/edit/?id=$page->id and add a &modal=1. If you load that edit link using fancybox iframe feature you get something like adminbar module.

So if you enter /processwire/page/edit/?id=1&modal=1 you'll get the edit form without the layout around. Of course this is only for trusted users, as you could remove the param and edit other things like publish, hidden etc. But it's a nice alternative to edit pages data without leaving the frontend page.

But if you are wanting a community user to edit his "profile data", which isn't necessary on the page he's viewing or only part and you will code a form to edit the data he can, you simply make a file with the view profile and for edit profile. In your page template for the profile you then simply include one or the other depending on the url segment. Or make the two views they're own physical page.

Kinda obvious it would look like this with segments and include.

if(count($input->urlSegments) == 0){
 include("view_profile.php");
} else if($input->urlSegment1 == 'edit'){
 include("edit_profile.php");
}

In a edit template to edit profile user data, I for example do something like this:


if($user->isLoggedin() && $user->hasRole('beratung-user')) {
   // logged in

   $form = $modules->get("InputfieldForm");
   $form->action = "./";
   $form->method = "post";
   $form->attr("id+name",'profil-form');

   $field = $modules->get("InputfieldText");
   $field->label = "Last Name";
   $field->attr('id+name','last_name');
   $field->required = 1;
   $field->value = $user->last_name;
   $form->append($field);

   $field = $modules->get("InputfieldText");
   $field->label = "First Name";
   $field->attr('id+name','first_name');
   $field->required = 1;
   $field->value = $user->first_name;
   $form->append($field);

   $field = $modules->get("InputfieldEmail");
   $field->label = "E-Mail";
   $field->attr('id+name','email');
   $field->required = 1;
   $field->value = $user->email;
   $form->append($field);

   $field = $modules->get("InputfieldText");
   $field->label = "Username";
   $field->attr('id+name','name');
   $field->required = 1;
   $field->value = $user->name;
   $form->append($field);

   $field = $modules->get("InputfieldPassword");
   $field->label = "Password";
   $field->attr("id+name","pass");

   $form->append($field);

   $submit = $modules->get("InputfieldSubmit");
   $submit->attr("value","Save");
   $submit->attr("id+name","submit");

   $form->append($submit);

   // process form
   if($input->post->submit) {

       $form->processInput($input->post);

       if(!$form->getErrors()){

           $user->of(false); // outputformatting off
           $user->name = $sanitizer->username($input->post->name);
           $user->first_name = $sanitizer->text($input->post->first_name);
           $user->last_name = $sanitizer->text($input->post->last_name);
           if($input->post->pass; != ''){
               $user->pass = $input->post->pass;;
           }
           $user->email = $sanitizer->email($input->post->email);

           $user->save();
           $user->of(true); // outputformatting on

           $session->message("Profile saved");
           $session->redirect("./"); // redirect to get rid of double post when refreshing
       } else {
           // error message are already added by form->processInput() call
       }
   }

   $out .= $form->render();

} else {
   $out .= "<p>You don't have permission to view this page.</p>";
}
?>


<section class="clearfix">

   <h1>Edit your Profile</h1>

   <?php if(count($notices)) include($config->paths->templates . "inc/notices.inc"); ?>

   <?php echo $out; ?>

</section>
  • Like 3
Link to comment
Share on other sites

Cheers for the answer, Soma!

I'm talking about a user profile on the frontend, no PW user, so yours is pretty much the way I did it know. The whole question was more of a thought experiment if I could use PW to handle the include() decision for me.

Link to comment
Share on other sites

PW can't handle include decision for you. PW doesn't make any decision on templates and logik in front-end, you always have to provide an if-then and best way here is use simple includes for the view.

You can specify a template file a page uses when rendering. But not for the page you're on, because it already has a template file your code is in and end in a recursion giving you an memory limit error.

But you can do it for a page you're you are recieving from a find/get query and give it a template file before render it.

For example:

$gallery = $pages->get($id);

if($input->urlSegment1 == 'view') {
 $gallery->template->set("filename","view_full_gallery.php");
 $gallery->render();
} else {

 $gallery->template->set("filename","view_teaser_gallery.php");
 $gallery->render();
}

So this will render the gallery page with different templates depending on a condition.

As you already seem to tried you could change the template file (not the PW template) of a page via a before page render hook.

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...