Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Tags rendering in PageTemplate

Vote:
 

Hello,i am developing a multi site and want to use the same HomePage page type for each of the sites with different page templates. Is there any way of creating this or do i need to duplicate the page types?. The page types we use are the same with the same properties so i dont really want to duplicate them if i have the option of using the same for 2 templates.

thanks, Andrei

#122579
Jun 05, 2015 13:21
Vote:
 

Hi,

You could try to use TemplateResolver:

http://world.episerver.com/documentation/Class-library/?documentId=cms/7/159e1a69-8fa7-aaac-d12c-f72b5170a30a

#122580
Jun 05, 2015 13:27
Vote:
 

Hello, thank you for your suggestion, could u provide an example with it? i never worked with it and have to clue where everything links together.

#122587
Jun 05, 2015 15:29
Vote:
 

Hi,

First create InitializationModule and subscribe to TemplateResolved event. Inside TemplateResolved method you should determinate the current domain. Based on that information you could select proper template.

using System;
using System.Linq;
using System.Web;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Web;
using EpiServerThumbnail.Models.Pages;

namespace EpiServerThumbnail.Business
{
    [InitializableModule]
    public class MobileRedirectSample : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            context.Locate.TemplateResolver().TemplateResolved
                += new EventHandler<TemplateResolverEventArgs>(this.HomePageTemplateResolved);
        }

        public void Uninitialize(global::EPiServer.Framework.Initialization.InitializationEngine context)
        {
            context.Locate.TemplateResolver().TemplateResolved
                -= new EventHandler<TemplateResolverEventArgs>(this.HomePageTemplateResolved);
        }

        void HomePageTemplateResolved(object sender, TemplateResolverEventArgs eventArgs)
        {
            if (eventArgs.ItemToRender != null && eventArgs.ItemToRender is ProductPage)
            {
                var templateName = IsSite1(eventArgs) ? "Site1" : "Site2";

                var render =
                    eventArgs.SupportedTemplates.SingleOrDefault(
                        r =>
                            r.Name == templateName &&
                            r.TemplateTypeCategory == eventArgs.SelectedTemplate.TemplateTypeCategory);

                if (render != null)
                {
                    eventArgs.SelectedTemplate = render;
                }
            }
        }

        private bool IsSite1(TemplateResolverEventArgs eventArgs)
        {
            // determinate is this is site1 or site2
            return eventArgs.WebContext.Request.ServerVariables["SERVER_NAME"] == "Site1.com";
        }

        public void Preload(string[] parameters)
        {
        }
    }
}

Then you need two renderers one for Site1 and second for Site2.

For ASP Webforms:

 [TemplateDescriptor(Tags = new[] { "Site1" })]
    public partial class HomePage1Template : TemplatePageBase<ProductPage>
{
}

 [TemplateDescriptor(Tags = new[] { "Site2" })]
    public partial class HomePage2Template : TemplatePageBase<ProductPage>
{
}

For ASP MVC:

[TemplateDescriptor(
        Inherited = true,
        TemplateTypeCategory = TemplateTypeCategories.MvcController, 
        Tags = new[] { "Site1"}, Default = true,
        AvailableWithoutTag = true)]
public class Site1HomePageController : PageController<PageData>
{
    public ActionResult Index(PageData currentPage)
    {
}
}

[TemplateDescriptor(
        Inherited = true,
        TemplateTypeCategory = TemplateTypeCategories.MvcController, 
        Tags = new[] { "Site2"}, Default = true,
        AvailableWithoutTag = true)]
public class Site2HomePageController : PageController<PageData>
{
    public ActionResult Index(PageData currentPage)
    {
}
}

You could also try to use Display Channels (instead of TemplateResolver).

public class MobileDisplayChannel : DisplayChannel
{
    public override bool IsActive(HttpContextBase context)
    {
        return context.Request.ServerVariables["SERVER_NAME"] == "Site1.com";
    }
 
    public override string ChannelName
    {
        get { return RenderingTags.Mobile; }
    }
}

The code above is just a draft. I didn't test it.

#122592
Jun 05, 2015 17:56
Vote:
 

Hello, thanks for taking the time, really apreciate it, will test it out

#122594
Jun 05, 2015 20:11
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.