API Questions on Kentico API.
Version 5.x > API > Changing Templates Dynamically View modes: 
User avatar
Certified Developer 13
Certified Developer 13
sean-inorbital - 7/26/2011 8:14:19 PM
   
Changing Templates Dynamically
hello everyone,

I'm currently working on an ecommerce site. One interesting facet of this website is that it will allow companies to set up their own stores to sell products (tshirts, mugs, etc) printed with their logo, brand, etc.

The customer would visit a store, add products to their cart and continue to the checkout. The appearance of the checkout/shopping cart page will vary depending on the store they just visited. So each store will have a cooresponding template for the checkout/shopping cart.

In other words, I want to change the page template on the fly.

What I've written so far involves changing the PageTemplateId of the shopping cart page. What I have so far works, however, the issue is that the change cannot be seen unless the page is refreshed. I originally placed my code in the ShoppingCart user control, then I tried the PortalTemplate but to no avail. I've placed the code during page init but still nothing.

I took a look at the CMSPageEvents class but soon discovered that this only applies to CMS ui controls which, correct me if I'm wrong, are specifically invovled with the kentico interface.

The problem is that my code is not being executed right away. I'm wondering if anyone could shed some light on how Kentico goes about displaying the pages. I want to ensure that my code is executed first, before the page template is retrieved and displayed in the browser.

Many thanks for your time.






User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 7/28/2011 2:28:42 AM
   
RE:Changing Templates Dynamically
Hello,

I've checked with our developer and he suggests to set the PageInfo in the Global.asax.cs file after the RewriteUrl method is executed:

PageInfo currentPageInfo = CMSContext.CurrentPageInfo;

Best regards,
Boris Pocatko

User avatar
Certified Developer 13
Certified Developer 13
sean - 7/28/2011 10:22:11 AM
   
RE:Changing Templates Dynamically
Thank you very much for getting back to me on this.

I've been playing with a few things.

What I have tried:

PageInfo currentPageInfo = CMSContext.CurrentPageInfo;
currentPageInfo.PageTemplateInfo.PageTemplateId = 3;

Right after the call to the method.

I'm just using whatever template id as i simply just want to see whether it changes right away. The above two lines don't change the template but rather it seems to remove the styling for the page! I get an object reference not found if I place it before the method call.

However, if I use the code that I have, which involves using the TreeProvider and TreeNode classes to obtain the document and then change its template id, and place the code before the method call, it works. Nothing will happen if I place it after the method call.

Thanks very much for the help!





User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 8/1/2011 3:09:50 AM
   
RE:Changing Templates Dynamically
Hello,

I consulted this issue with our Technical Leader and he explained that none of the approaches is correct. A dynamic "current" object shouldn't be modified and in this case it isn't even possible as you can see.
The second approach, which are you using right now works, however, you are changing directly the document. That means that in some special cases it is possible that two users get the same content.

The best approach would be to create a redirection page (with custom redirection logic according to e.g. the user name) and create different documents in the content tree representing different layouts.

We are planning to support a similar feature in future versions, however currently this is the only solution which wouldn't mess up the page life cycle and wouldn't cause issues in special cases.

Best regards,
Boris Pocatko

User avatar
Certified Developer 13
Certified Developer 13
sean - 8/4/2011 9:00:56 AM
   
RE:Changing Templates Dynamically
Thank you for pointing this out.

I just want to see whether I completely understand the approach offered by the Technical Leader.

So they suggest to create documents for each checkout layout and redirect the user to the layout based on the store they visited?

Now my understanding is that the shopping cart control contains the checkout process itself. So naturally, I figure having the shoppingcart control for each of these layouts would make sense. However, I ran into an issue with the design view for each of these layouts. Kentico would not allow for adding webparts or any other components to these pages (no visible zones, no buttons for adding parts); these pages contained simply the shopping cart and nothing else. If I restrict the number of shopping cart controls to one, however, then the design view functions normally.

It seems as if there is a restriction on the number of shopping cart controls that can be used in a given project. Is this true? Have I misunderstood the suggested approach?





User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 8/4/2011 11:24:09 AM
   
RE:Changing Templates Dynamically
Hello,

I am not sure about that. I've successfully added a new page (menu item), used a different template and added another shopping cart web part to it without any problems. Could you please post a screenshot of the error message you are getting?

Best regards,
Boris Pocatko

User avatar
Guest
sean - 8/4/2011 3:36:55 PM
   
RE:Changing Templates Dynamically
It seems like it is due to my code. The user is redirected to the default checkout if they try to access the checkout of a store they did not access previously. If I comment out this redirect, viewing a checkout template in design view works as expected.

I'm using a session variable to keep track of the store previously visited. I'm wondering if edit mode has some sort of affect on session variables? Viewing under live site works fine.

In any case, this makes things a bit tricky if I ever want to modify a checkout template using the cms.

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 8/5/2011 7:38:37 AM
   
RE:Changing Templates Dynamically
Hello,

I am now a bit confused. Could you please explain in detail what is your setup? I suggested the following approach:

Common landing page for all visitors with redirection code to the specific pages with different layouts and templates. That way there will be only one shopping cart on every page. Session variables may be used for the shopping cart. Have you tried to use some other method (e.g. querystring, custom cookie)?

Best regards,
Boris Pocatko

User avatar
Guest
sean - 8/5/2011 8:37:53 AM
   
RE:Changing Templates Dynamically
Apologies.

There are essentially two kinds of stores; general/default store and stores for those wishing to sell their own products.

If a user visits a store, the shopping cart link and the add to cart links will change and will lead the user to the correct cart/checkout layout when clicked. A session variable is used to record the store they visited. But there could be instances where the user tries to go directly to a store checkout (by entering it through the url) and I would like to redirect them to the appropriate page if necessary.

At this point, I placed redirection code into the shopping cart web part. It checks for the presence of the session variable; if it isn't there, redirect to the default cart/checkout otherwise redirect to the appropriate checkout. This works fine on the live site.

But, if I try to view any store cart/checkout document in the cms, it appears as the default layout suggesting that the session variable doesn't exist despite having selected a store from the content tree (where the session variable would be created) prior to the checkout.









User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 8/8/2011 7:50:00 AM
   
RE:Changing Templates Dynamically
Hello,

In that case we will need to see your code you are using. Could you please post the portion where you are setting and retrieving the session variable and please also point us to the location where it is used (which method etc.) because it may be in a branch of the code which is not executed in the CMS Desk.

Best regards,
Boris Pocatko