/// <summary>
        /// Renders HTML for an image
        /// </summary>
        /// <param name="image">The image to render</param>
        /// <param name="attributes">Additional attributes to add. Do not include alt or src</param>
        /// <returns>An img HTML element</returns>
        public virtual string RenderImage(Fields.Image image, NameValueCollection attributes)
        {
            if (image == null)
            {
                return("");
            }

            if (attributes == null)
            {
                attributes = new NameValueCollection();
            }

            string format = "<img src='{0}' {1}/>";

            //should there be some warning about these removals?
            AttributeCheck(attributes, "class", image.Class);
            AttributeCheck(attributes, "alt", image.Alt);
            if (image.Height > 0)
            {
                AttributeCheck(attributes, "height", image.Height.ToString());
            }
            if (image.Width > 0)
            {
                AttributeCheck(attributes, "width", image.Width.ToString());
            }

            return(format.Formatted(image.Src, Utilities.ConvertAttributes(attributes)));
        }
Exemple #2
0
        public virtual string RenderImage(Fields.Image image, NameValueCollection attributes)
        {
            /*
             * ME - This method is used to render images rather than going back to the fieldrender
             * because it stops another call having to be passed to Sitecore.
             */

            if (image == null || image.Src.IsNullOrWhiteSpace())
            {
                return("");
            }

            if (attributes == null)
            {
                attributes = new NameValueCollection();
            }


            var builder = new UrlBuilder(image.Src);

            //append to url values
            if (attributes[ImageWidth].IsNotNullOrEmpty())
            {
                attributes.Add(ImageParameters.WIDTH, attributes[ImageWidth]);
            }
            else
            {
                attributes.Add(ImageParameters.WIDTH, image.Width.ToString());
            }

            if (attributes[ImageHeight].IsNotNullOrEmpty())
            {
                attributes.Add(ImageParameters.HEIGHT, attributes[ImageHeight]);
            }
            else
            {
                attributes.Add(ImageParameters.HEIGHT, image.Height.ToString());
            }

            foreach (var key in attributes.AllKeys)
            {
                if (key == "alt" || key == "class" || key == "style")
                {
                    continue;
                }

                builder[key] = attributes[key];
            }

            //should there be some warning about these removals?
            AttributeCheck(attributes, "class", image.Class);
            AttributeCheck(attributes, "alt", image.Alt);


            return(ImageTagFormat.Formatted(builder.ToString(), Utilities.ConvertAttributes(attributes)));
        }
        public virtual string RenderImage(Fields.Image image, NameValueCollection attributes)
        {
            var urlParams  = new NameValueCollection();
            var htmlParams = new NameValueCollection();

            /*
             * ME - This method is used to render images rather than going back to the fieldrender
             * because it stops another call having to be passed to Sitecore.
             */

            if (image == null || image.Src.IsNullOrWhiteSpace())
            {
                return("");
            }

            if (attributes == null)
            {
                attributes = new NameValueCollection();
            }

            Action <string> remove = key => attributes.Remove(key);
            Action <string> url    = key =>
            {
                urlParams.Add(key, attributes[key]);
                remove(key);
            };
            Action <string> html = key =>
            {
                htmlParams.Add(key, attributes[key]);
                remove(key);
            };
            Action <string> both = key =>
            {
                htmlParams.Add(key, attributes[key]);
                urlParams.Add(key, attributes[key]);
                remove(key);
            };

            foreach (var key in attributes.AllKeys)
            {
                switch (key)
                {
                case ImageParameters.BORDER:
                case ImageParameters.ALT:
                case ImageParameters.HSPACE:
                case ImageParameters.VSPACE:
                case ImageParameters.CLASS:
                    html(key);
                    break;

                case ImageParameters.OUTPUT_METHOD:
                case ImageParameters.ALLOW_STRETCH:
                case ImageParameters.IGNORE_ASPECT_RATIO:
                case ImageParameters.SCALE:
                case ImageParameters.MAX_WIDTH:
                case ImageParameters.MAX_HEIGHT:
                case ImageParameters.THUMBNAIL:
                case ImageParameters.BACKGROUND_COLOR:
                case ImageParameters.DATABASE:
                case ImageParameters.LANGUAGE:
                case ImageParameters.VERSION:
                case ImageParameters.DISABLE_MEDIA_CACHE:
                    url(key);
                    break;

                case ImageParameters.WIDTH:
                case ImageParameters.HEIGHT:
                    both(key);
                    break;

                default:
                    both(key);
                    break;
                }
            }

            var builder = new UrlBuilder(image.Src);

            foreach (var key in urlParams.AllKeys)
            {
                builder[key] = urlParams[key];
            }

            //should there be some warning about these removals?
            AttributeCheck(htmlParams, ImageParameters.CLASS, image.Class);
            AttributeCheck(htmlParams, ImageParameters.ALT, image.Alt);
            AttributeCheck(htmlParams, ImageParameters.BORDER, image.Border);
            if (image.HSpace > 0)
            {
                AttributeCheck(htmlParams, ImageParameters.HSPACE, image.HSpace.ToString(CultureInfo.InvariantCulture));
            }
            if (image.VSpace > 0)
            {
                AttributeCheck(htmlParams, ImageParameters.VSPACE, image.VSpace.ToString(CultureInfo.InvariantCulture));
            }

            if (htmlParams.AllKeys.Any(x => x == ImageParameters.HEIGHT))
            {
                htmlParams["height"] = htmlParams[ImageParameters.HEIGHT];
                htmlParams.Remove(ImageParameters.HEIGHT);
            }

            if (htmlParams.AllKeys.Any(x => x == ImageParameters.WIDTH))
            {
                htmlParams["width"] = htmlParams[ImageParameters.WIDTH];
                htmlParams.Remove(ImageParameters.WIDTH);
            }

            return(ImageTagFormat.Formatted(builder.ToString(), Utilities.ConvertAttributes(htmlParams)));
        }
 public virtual string RenderImage(Fields.Image image)
 {
     return(RenderImage(image, null));
 }