/// <summary>
        /// Render HTML for a link
        /// </summary>
        /// <param name="link">The link to render</param>
        /// <param name="model">The model containing the link</param>
        /// <param name="field">An expression that points to the link</param>
        /// <param name="attributes">A collection of attributes to added to the link</param>
        /// <param name="isEditable">Indicate if the link should be editable in the page editor</param>
        /// <param name="contents">Content to go in the link</param>
        /// <returns>An "a" HTML element</returns>
        public virtual string RenderLink <T>(T model, Expression <Func <T, object> > field, NameValueCollection attributes = null, bool isEditable = false, string contents = null)
        {
            var sb     = new StringBuilder();
            var writer = new StringWriter(sb);

            RenderingResult result = null;

            if (IsInEditingMode && isEditable)
            {
                result = MakeEditable(
                    field,
                    null,
                    model,
                    contents == null ? string.Empty: "haschildren=true",
                    _context, SitecoreContext.Database, writer);
            }
            else
            {
                result = BeginRenderLink(
                    field.Compile().Invoke(model) as Fields.Link, attributes, contents, writer
                    );
            }

            result.Dispose();
            writer.Flush();
            writer.Close();
            return(sb.ToString());
        }
示例#2
0
        /// <summary>
        /// Render HTML for a link
        /// </summary>
        /// <param name="link">The link to render</param>
        /// <param name="model">The model containing the link</param>
        /// <param name="field">An expression that points to the link</param>
        /// <param name="attributes">A collection of parameters to added to the link</param>
        /// <param name="isEditable">Indicate if the link should be editable in the page editor</param>
        /// <param name="contents">Content to go in the link</param>
        /// <returns>An "a" HTML element</returns>
        public virtual string RenderLink <T>(T model, Expression <Func <T, object> > field, object attributes = null, bool isEditable = false, string contents = null)
        {
            NameValueCollection attrs = null;

            if (attributes is NameValueCollection)
            {
                attrs = attributes as NameValueCollection;
            }
            else
            {
                attrs = Utilities.GetPropertiesCollection(attributes, true);
            }

            var sb        = new StringBuilder();
            var writer    = new StringWriter(sb);
            var linkField = field.Compile().Invoke(model) as Fields.Link;

            RenderingResult result = null;

            if (IsInEditingMode && isEditable)
            {
                if (!string.IsNullOrEmpty(contents))
                {
                    attrs["haschildren"] = "true";
                }
                if (contents.IsNotNullOrEmpty())
                {
                    attrs.Add("haschildren", "true");
                }

                if (linkField != null)
                {
                    AttributeCheck(attrs, "class", linkField.Class);
                    AttributeCheck(attrs, "title", linkField.Title);
                }

                result = MakeEditable(
                    field,
                    null,
                    model,
                    Utilities.ConstructQueryString(attrs),
                    _context, SitecoreContext.Database, writer);

                if (contents.IsNotNullOrEmpty())
                {
                    sb.Append(contents);
                }
            }
            else
            {
                result = BeginRenderLink(
                    GetCompiled(field).Invoke(model) as Fields.Link, attrs, contents, writer
                    );
            }

            result.Dispose();
            writer.Flush();
            writer.Close();
            return(sb.ToString());
        }