Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

can we dynamically set the pageTemplate for same page type and same language.

Vote:
 

Hi,

I have one pagetype.For that I have set three templates.In Admin mode,When I create a page it takes the default template.I need to choose an alternate from the three.How can i choose it dynamically?

#141847
Nov 23, 2015 8:08
Vote:
 

Hi,

You can hook up to the TemplateResolved event of the TemplateResolver. One way to do it is in an initialization module:

[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
[InitializableModule]
public class TemplateInitialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        context.Locate.TemplateResolver().TemplateResolved += OnTemplateResolved;
    }

    public void Uninitialize(InitializationEngine context)
    {
        ServiceLocator.Current.GetInstance<TemplateResolver>().TemplateResolved -= OnTemplateResolved;
    }

    public void Preload(string[] parameters)
    {
    }

    static void OnTemplateResolved(object sender, TemplateResolverEventArgs args)
    {
        var myContent = args.ItemToRender as YourPageType;

        if (myContent != null) 
        {
            IEnumerable<TemplateModel> supportedTemplates = args.SupportedTemplates; // Should contain your three templates

            // Set template based on condition
            if (...)
            {
                args.SelectedTemplate = supportedTemplates.FirstOrDefault(...);
            }
        }
    }
}
#141848
Nov 23, 2015 8:53
Vote:
 

Hi  Mattias,

Thanks a lot.You have solve my requiement.

#141858
Nov 23, 2015 10:42
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.