Skip to content

Set custom properties on custom template before execute #30

@davifiamenghi

Description

@davifiamenghi

I've developed a MvcTemplateBase and MvcTemplateBase and I need to set some properties on those types before execute, like one called ViewData. So I would need to execute the following sequence:

         Type modelType = model.GetType();

        ITemplate template = machine.Context.TemplateFactory.CreateTemplateInstance(viewName, modelType);
        MvcTemplateBase mvcTemplate = (MvcTemplateBase)machine.Context.TemplateFactory.CreateTemplateInstance(viewName);
        ITemplateInternal templateInternal = (ITemplateInternal)template;

        templateInternal.SetModel(model);
        mvcTemplate.ModelTypeForMetadata = new TypeWrapper(metadataModelDefinition, modelType);
        if (viewData == null)
        {
            viewData = new System.Web.Mvc.ViewDataDictionary();
        }

        viewData.Model = model;

        mvcTemplate.SetViewData(viewData);
        mvcTemplate.TempFolder = tempFolder; // custom stuff

        mvcTemplate.Execute();
        templateInternal.Execute();
        machine.ExecuteUrl(viewName, model);

        if (layoutName != null)
            mvcTemplate.Layout = layoutName;

        templateInternal.TryApplyLayout();

One problem I had, is that ITemplateInternal is internal, and I had to make it public to be able to call ITemplateInternal methods.

Let me know if I'm not clear enough or if I can contribute with this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions