public override object ConvertTo(Type type, CultureInfo culture)
        {
            if (String.IsNullOrEmpty(_parameters))
            {
                return(null);
            }

            var glassHtml     = new GlassHtml(SitecoreContext.GetFromHttpContext());
            var genericMethod = method.MakeGenericMethod(type);

            return(genericMethod.Invoke(glassHtml, new object[] { _parameters }) ?? base.ConvertTo(type, culture));
        }
示例#2
0
        /// <summary>
        /// Gets the object.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="db">The db.</param>
        /// <returns></returns>
        /// <exception cref="Glass.Mapper.MapperException">Failed to find context {0}.Formatted(ContextName)</exception>
        public object GetObject(string model, Database db, Rendering renderingItem)
        {
            if (model.IsNullOrEmpty())
            {
                return(null);
            }

            //must be a path to a Model item
            if (model.StartsWith("/sitecore"))
            {
                var target = db.GetItem(model);
                if (target == null)
                {
                    return(null);
                }

                string newModel = target[ModelTypeField];
                return(GetObject(newModel, db, renderingItem));
            }
            //if guid must be that to Model item
            Guid targetId;

            if (Guid.TryParse(model, out targetId))
            {
                var target = db.GetItem(new ID(targetId));
                if (target == null)
                {
                    return(null);
                }

                string newModel = target[ModelTypeField];
                return(GetObject(newModel, db, renderingItem));
            }


            var type = Type.GetType(model, false);

            if (type == null || renderingModelType.IsAssignableFrom(type))
            {
                return(null);
            }

            ISitecoreContext scContext = SitecoreContext.GetFromHttpContext(ContextName);


            //this is really aggressive
            if (!scContext.GlassContext.TypeConfigurations.ContainsKey(type))
            {
                //if the config is null then it is probably an ondemand mapping so we have to load the ondemand part

                IConfigurationLoader loader =
                    new OnDemandLoader <SitecoreTypeConfiguration>(type);
                scContext.GlassContext.Load(loader);
            }

            if (renderingItem.DataSource.IsNotNullOrEmpty())
            {
                var item = scContext.Database.GetItem(renderingItem.DataSource);
                return(scContext.CreateType(type, item, false, false, null));
            }

            if (renderingItem.RenderingItem.DataSource.HasValue())
            {
                var item = scContext.Database.GetItem(renderingItem.RenderingItem.DataSource);
                return(scContext.CreateType(type, item, false, false, null));
            }

            /**
             * Issues #82:
             * Check Item before defaulting to the current item.
             */
            if (renderingItem.Item != null)
            {
                return(scContext.CreateType(type, renderingItem.Item, false, false, null));
            }

            return(scContext.GetCurrentItem(type));
        }
示例#3
0
        public static GlassHtmlMvc <T> Glass <T>(this HtmlHelper <T> htmlHelper)
        {
            var html = new GlassHtml(SitecoreContext.GetFromHttpContext());

            return(new GlassHtmlMvc <T>(html, htmlHelper.ViewContext.Writer, htmlHelper.ViewData.Model));
        }
示例#4
0
        public override void Process(GetModelArgs args)
        {
            if (!IsValidForProcessing(args))
            {
                return;
            }

            string path = GetViewPath(args);

            if (string.IsNullOrWhiteSpace(path))
            {
                return;
            }

            string cacheKey  = modelCacheManager.GetKey(path);
            Type   modelType = modelCacheManager.Get(cacheKey);

            if (modelType == typeof(NullModel))
            {
                // The model has been attempted before and is not useful
                return;
            }

            // The model type hasn't been found before or has been cleared.
            if (modelType == null)
            {
                modelType = GetModel(args, path);

                modelCacheManager.Add(cacheKey, modelType);

                if (modelType == typeof(NullModel))
                {
                    // This is not the type we are looking for
                    return;
                }
            }

            ISitecoreContext scContext = SitecoreContext.GetFromHttpContext(ContextName);

            Rendering renderingItem = args.Rendering;

            object model = null;

            if (renderingItem.DataSource.HasValue())
            {
                var item = scContext.Database.GetItem(renderingItem.DataSource);
                model = scContext.CreateType(modelType, item, false, false, null);
            }
            else if (renderingItem.RenderingItem.DataSource.HasValue())
            {
                var item = scContext.Database.GetItem(renderingItem.RenderingItem.DataSource);
                model = scContext.CreateType(modelType, item, false, false, null);
            }
            else if (renderingItem.Item != null)
            {
                /**
                 * Issues #82:
                 * Check Item before defaulting to the current item.
                 */
                model = scContext.CreateType(modelType, renderingItem.Item, false, false, null);
            }
            else
            {
                model = scContext.GetCurrentItem(modelType);
            }

            args.Result = model;
        }