public string RitornaParsingTemplate(dynamic contentModel, int templateId, Dictionary <string, object> viewBag = null) { try { if (contentModel != null && contentModel.Id != null && contentModel.Id > 0) { _tagForCache.Add(contentModel.Id); } } catch { } if (templateId > 0) { _tagForCache.Add(templateId); } ParseTemplateContext templatectx = new ParseTemplateContext(); var template = GetTemplate(templateId); var baseUri = new Uri(_services.WorkContext.CurrentSite.BaseUrl); var basePath = GetBasePath(baseUri); string host = ""; string mediaUrl = ""; var tenantPrefix = GetTenantUrlPrexix(_shellSettings); if (_services.WorkContext.HttpContext != null) { var urlHelper = new UrlHelper(_services.WorkContext.HttpContext.Request.RequestContext); // Creo un model che ha Content (il contentModel), Urls con alcuni oggetti utili per il template // Nel template pertanto Model, diventa Model.Content host = string.Format("{0}://{1}{2}", _services.WorkContext.HttpContext.Request.Url.Scheme, _services.WorkContext.HttpContext.Request.Url.Host, _services.WorkContext.HttpContext.Request.Url.Port == 80 ? string.Empty : ":" + _services.WorkContext.HttpContext.Request.Url.Port); mediaUrl = urlHelper.MediaExtensionsImageUrl(); } else { host = string.Format("{0}://{1}{2}", baseUri.Scheme, baseUri.Host, baseUri.Port == 80 ? string.Empty : ":" + baseUri.Port); mediaUrl = string.Format("{0}{1}{2}", basePath, tenantPrefix, @"Laser.Orchard.StartupConfig/MediaTransform/Image"); } // compute base url for medias in the media folder: var mockMediaPath = _mediaLibraryService .GetMediaPublicUrl("a", "a"); // path for mocked media var baseMediaPath = Path.Combine(host, mockMediaPath .Substring(0, mockMediaPath.Length - 3)); string baseUrl = string.Format("{0}{1}{2}", host, basePath, tenantPrefix); var dynamicModel = new { WorkContext = _services.WorkContext, Content = contentModel, Urls = new { BaseUrl = baseUrl, MediaUrl = mediaUrl, Domain = host, PublicMediaPath = baseMediaPath, }.ToExpando() }; templatectx.Model = dynamicModel; // var razorviewBag = viewBag; //RazorEngine.Templating.DynamicViewBag vb = new DynamicViewBag(); //try { // foreach (string key in ((Dictionary<string, object>)viewBag).Keys) { // vb.AddValue(key, ((IDictionary<string, object>)viewBag)[key]); // } //} catch { } //templatectx.ViewBag = vb; templatectx.ViewBag = viewBag; return(ParseTemplate(template, templatectx)); }
public string ParseTemplate(TemplatePart template, ParseTemplateContext context) { var parser = SelectParser(template); return(parser.ParseTemplate(template, context)); }