Skip to content Skip to sidebar Skip to footer

How To Render Shape To String?

i am using Orchard 1.6 and want to render view to string with theme custom layout and so on. In fact there is orchard cms rendering shape as email template but i need some more det

Solution 1:

It does not work with me either, I am using Orchard 1.6. There used to be a vote to make it work but I am not sure it is taken in Orchard 1.7.

Here is my way to do it in my project (I render shapes to string for ajax DataTables), provided that you make the call from a controller.

Inside your service (or controller), get your hand to 2 services: IThemeAwareViewEngine and IDisplayHelperFactory:

privatereadonly IThemeAwareViewEngine _themeAwareViewEngine;
    privatereadonly IDisplayHelperFactory _displayHelperFactory;
    publicAjaxDataTables(
        IThemeAwareViewEngine themeAwareViewEngine,
        IDisplayHelperFactory displayHelperFactory
        )
    {
        _themeAwareViewEngine = themeAwareViewEngine;
        _displayHelperFactory = displayHelperFactory;
    }

Here is the way to get the magic display, provided that you have a reference to a real controller:

dynamicGetDisplayHelper(Controller controller)
    {
        // We can specify any view name, just to get a View only, the shape template finding will be taken care by DisplayHelperFactory.// Here the "Brandking" view is always existed, we can also use something like "Layout" ...var viewResult = _themeAwareViewEngine.FindPartialView(controller.ControllerContext, "Branding", false, false);
        var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, new StringWriter());
        return _displayHelperFactory.CreateHelper(viewContext, new ViewDataContainer());
    }
    privateclassViewDataContainer : IViewDataContainer
    {
        public ViewDataDictionary ViewData { get; set; }
    }

And render shape to string:

var display = GetDisplayHelper(controller);
var str = Convert.ToString(display(shape.Content));

Again, this work only if you make call from a controller action.

HTH, Huy

Post a Comment for "How To Render Shape To String?"