Beispiel #1
0
 /// <summary>
 /// Renders the HTML for this component. This will execute the component server-side and
 /// return the rendered HTML.
 /// </summary>
 /// <param name="renderContainerOnly">Only renders component container. Used for client-side only rendering.</param>
 /// <param name="renderServerOnly">Only renders the common HTML mark up and not any React specific data attributes. Used for server-side only rendering.</param>
 /// <returns>HTML</returns>
 public virtual string RenderHtml(bool renderContainerOnly = false, bool renderServerOnly = false)
 {
     EnsureComponentExists();
     try
     {
         var html = string.Empty;
         if (!renderContainerOnly)
         {
             var reactRenderCommand = renderServerOnly
                                         ? string.Format("ReactDOMServer.renderToStaticMarkup({0})", GetComponentInitialiser())
                                         : string.Format("ReactDOMServer.renderToString({0})", GetComponentInitialiser());
             html = _environment.Execute <string>(reactRenderCommand);
         }
         return(string.Format(
                    "<{2} id=\"{0}\">{1}</{2}>",
                    ContainerId,
                    html,
                    ContainerTag
                    ));
     }
     catch (JsRuntimeException ex)
     {
         throw new ReactServerRenderingException(string.Format(
                                                     "Error while rendering \"{0}\" to \"{2}\": {1}",
                                                     ComponentName,
                                                     ex.Message,
                                                     ContainerId
                                                     ));
     }
 }
Beispiel #2
0
 /// <summary>
 /// Renders the HTML for this component. This will execute the component server-side and
 /// return the rendered HTML.
 /// </summary>
 /// <returns>HTML</returns>
 public virtual string RenderHtml()
 {
     EnsureComponentExists();
     try
     {
         var html = _environment.Execute <string>(
             string.Format("React.renderToString({0})", GetComponentInitialiser())
             );
         return(string.Format(
                    "<{2} id=\"{0}\">{1}</{2}>",
                    ContainerId,
                    html,
                    ContainerTag
                    ));
     }
     catch (JsRuntimeException ex)
     {
         throw new ReactServerRenderingException(string.Format(
                                                     "Error while rendering \"{0}\" to \"{2}\": {1}",
                                                     ComponentName,
                                                     ex.Message,
                                                     ContainerId
                                                     ));
     }
 }
Beispiel #3
0
 /// <summary>
 /// Renders the HTML for this component. This will execute the component server-side and
 /// return the rendered HTML.
 /// </summary>
 /// <returns>HTML</returns>
 public string RenderHtml()
 {
     EnsureComponentExists();
     try
     {
         var html = _environment.Execute <string>(
             string.Format("React.renderComponentToString({0})", GetComponentInitialiser())
             );
         // TODO: Allow changing of the wrapper tag element from a DIV to something else
         return(string.Format(
                    "<div id=\"{0}\">{1}</div>",
                    _containerId,
                    html
                    ));
     }
     catch (JsRuntimeException ex)
     {
         throw new ReactServerRenderingException(string.Format(
                                                     "Error while rendering \"{0}\" to \"{2}\": {1}",
                                                     _componentName,
                                                     ex.Message,
                                                     _containerId
                                                     ));
     }
 }
        /// <summary>
        /// Renders the HTML for this component. This will execute the component server-side and
        /// return the rendered HTML.
        /// </summary>
        /// <param name="renderContainerOnly">Only renders component container. Used for client-side only rendering.</param>
        /// <param name="renderServerOnly">Only renders the common HTML mark up and not any React specific data attributes. Used for server-side only rendering.</param>
        /// <param name="exceptionHandler">A custom exception handler that will be called if a component throws during a render. Args: (Exception ex, string componentName, string containerId)</param>
        /// <returns>HTML</returns>
        public virtual string RenderHtml(bool renderContainerOnly = false, bool renderServerOnly = false, Action <Exception, string, string> exceptionHandler = null)
        {
            if (!_configuration.UseServerSideRendering)
            {
                renderContainerOnly = true;
            }

            if (!renderContainerOnly)
            {
                EnsureComponentExists();
            }

            var html = string.Empty;

            if (!renderContainerOnly)
            {
                try
                {
                    var reactRenderCommand = renderServerOnly
                                                ? string.Format("ReactDOMServer.renderToStaticMarkup({0})", GetComponentInitialiser())
                                                : string.Format("ReactDOMServer.renderToString({0})", GetComponentInitialiser());
                    html = _environment.Execute <string>(reactRenderCommand);

                    if (renderServerOnly)
                    {
                        return(html);
                    }
                }
                catch (JsRuntimeException ex)
                {
                    if (exceptionHandler == null)
                    {
                        exceptionHandler = _configuration.ExceptionHandler;
                    }

                    exceptionHandler(ex, ComponentName, ContainerId);
                }
            }

            string attributes = string.Format("id=\"{0}\"", ContainerId);

            if (!string.IsNullOrEmpty(ContainerClass))
            {
                attributes += string.Format(" class=\"{0}\"", ContainerClass);
            }

            return(string.Format(
                       "<{2} {0}>{1}</{2}>",
                       attributes,
                       html,
                       ContainerTag
                       ));
        }
Beispiel #5
0
        /// <summary>
        /// Renders the HTML for this component. This will execute the component server-side and
        /// return the rendered HTML.
        /// </summary>
        /// <param name="renderContainerOnly">Only renders component container. Used for client-side only rendering.</param>
        /// <param name="renderServerOnly">Only renders the common HTML mark up and not any React specific data attributes. Used for server-side only rendering.</param>
        /// <returns>HTML</returns>
        public virtual string RenderHtml(bool renderContainerOnly = false, bool renderServerOnly = false)
        {
            if (!_configuration.UseServerSideRendering)
            {
                renderContainerOnly = true;
            }

            if (!renderContainerOnly)
            {
                EnsureComponentExists();
            }

            try
            {
                var html = string.Empty;
                if (!renderContainerOnly)
                {
                    var reactRenderCommand = renderServerOnly
                                                ? string.Format("ReactDOMServer.renderToStaticMarkup({0})", GetComponentInitialiser())
                                                : string.Format("ReactDOMServer.renderToString({0})", GetComponentInitialiser());
                    html = _environment.Execute <string>(reactRenderCommand);
                }

                string attributes = string.Format("id=\"{0}\"", ContainerId);
                if (!string.IsNullOrEmpty(ContainerClass))
                {
                    attributes += string.Format(" class=\"{0}\"", ContainerClass);
                }

                return(string.Format(
                           "<{2} {0}>{1}</{2}>",
                           attributes,
                           html,
                           ContainerTag
                           ));
            }
            catch (JsRuntimeException ex)
            {
                throw new ReactServerRenderingException(string.Format(
                                                            "Error while rendering \"{0}\" to \"{2}\": {1}",
                                                            ComponentName,
                                                            ex.Message,
                                                            ContainerId
                                                            ));
            }
        }
Beispiel #6
0
        /// <summary>
        /// Renders the HTML for this component. This will execute the component server-side and
        /// return the rendered HTML.
        /// </summary>
        /// <returns>HTML</returns>
        public string RenderHtml()
        {
            EnsureComponentExists();
            var html = _environment.Execute <string>(
                string.Format("React.renderComponentToString({0})", GetComponentInitialiser())
                );

            // TODO: Allow changing of the wrapper tag element from a DIV to something else
            return(string.Format(
                       "<div id=\"{0}\">{1}</div>",
                       _containerId,
                       html
                       ));
        }
Beispiel #7
0
        /// <summary>
        /// Renders the HTML for this component. This will execute the component server-side and
        /// return the rendered HTML.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.IO.TextWriter" /> to which the content is written</param>
        /// <param name="renderContainerOnly">Only renders component container. Used for client-side only rendering.</param>
        /// <param name="renderServerOnly">Only renders the common HTML mark up and not any React specific data attributes. Used for server-side only rendering.</param>
        /// <param name="exceptionHandler">A custom exception handler that will be called if a component throws during a render. Args: (Exception ex, string componentName, string containerId)</param>
        /// <param name="renderFunctions">Functions to call during component render</param>
        /// <returns>HTML</returns>
        public virtual void RenderHtml(TextWriter writer, bool renderContainerOnly = false, bool renderServerOnly = false, Action <Exception, string, string> exceptionHandler = null, IRenderFunctions renderFunctions = null)
        {
            if (!_configuration.UseServerSideRendering)
            {
                renderContainerOnly = true;
            }

            if (!renderContainerOnly)
            {
                EnsureComponentExists();
            }

            var html = string.Empty;

            if (!renderContainerOnly)
            {
                var stringWriter = _sharedStringWriter;
                if (stringWriter != null)
                {
                    stringWriter.GetStringBuilder().Clear();
                }
                else
                {
                    _sharedStringWriter = stringWriter = new StringWriter(new StringBuilder(_serializedProps.Length + 128));
                }

                try
                {
                    stringWriter.Write(renderServerOnly ? "ReactDOMServer.renderToStaticMarkup(" : "ReactDOMServer.renderToString(");
                    if (renderFunctions != null)
                    {
                        stringWriter.Write(renderFunctions.WrapComponent(GetStringFromWriter(componentInitWriter => WriteComponentInitialiser(componentInitWriter))));
                    }
                    else
                    {
                        WriteComponentInitialiser(stringWriter);
                    }
                    stringWriter.Write(')');

                    if (renderFunctions != null)
                    {
                        renderFunctions.PreRender(x => _environment.Execute <string>(x));
                        html = _environment.Execute <string>(renderFunctions.TransformRenderedHtml(stringWriter.ToString()));
                        renderFunctions.PostRender(x => _environment.Execute <string>(x));
                    }
                    else
                    {
                        html = _environment.Execute <string>(stringWriter.ToString());
                    }

                    if (renderServerOnly)
                    {
                        writer.Write(html);
                        return;
                    }
                }
                catch (JsException ex)
                {
                    if (exceptionHandler == null)
                    {
                        exceptionHandler = _configuration.ExceptionHandler;
                    }

                    exceptionHandler(ex, ComponentName, ContainerId);
                }
            }

            writer.Write('<');
            writer.Write(ContainerTag);
            writer.Write(" id=\"");
            writer.Write(ContainerId);
            writer.Write('"');
            if (!string.IsNullOrEmpty(ContainerClass))
            {
                writer.Write(" class=\"");
                writer.Write(ContainerClass);
                writer.Write('"');
            }

            writer.Write('>');
            writer.Write(html);
            writer.Write("</");
            writer.Write(ContainerTag);
            writer.Write('>');
        }