Skip to content

Blazor RenderFragment to String #22266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sps014 opened this issue May 27, 2020 · 7 comments
Closed

Blazor RenderFragment to String #22266

sps014 opened this issue May 27, 2020 · 7 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@sps014
Copy link

sps014 commented May 27, 2020

I have Blazor Component as

<pre class="language-html">
    <code class="language-html">@ChildContent</code>
</pre>

@code
{
    [Parameter]
    public RenderFragment ChildContent { get; set; }
}

now i am using component as

<CodeBlock>
        <StepContent>
            <StepTitle>Shipping</StepTitle>
            <StepDescription>Choose your shipping options</StepDescription>
        </StepContent>
</CodeBlock>

I want to show RenderFragment as String, not as evaluated Dom Element.

Actual Output

current output

Expected Output

expected

My different Approach

If the component was designed to take string

    <CodeBlock Code="@str"/>
public string str = @"
        <StepContent>
            <StepTitle>Shipping</StepTitle>
            <StepDescription>Choose your shipping options</StepDescription>
        </StepContent>";

Use string as Parameter but with this there come many problems like escaping ", Visually Ugly looking Component Code, etc. Overall Complexity is increased.

I am hoping for a way to convert RenderFragment to string as current implementation gives evaluated Component.

Thank you.

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label May 27, 2020
@javiercn
Copy link
Member

@sps014 thanks for contacting us.

There is no way to do this today within Blazor. We have support for this in the form of prerendering, but not anywhere else. It is unlikely we add something like this, since we haven't receive similar asks for it yet.

@mkArtakMSFT mkArtakMSFT added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label May 27, 2020
@ghost ghost added the Status: Resolved label May 27, 2020
@mrpmorris
Copy link

mrpmorris commented May 27, 2020

In your component have a

<div style="display: none" @ref=MyContentRef`>@ChildContent</div>

In OnAfterRenderAsync you can call some JavaScript to get the innerHtml of MyContentRef and then display that string in your <code>.

@sps014
Copy link
Author

sps014 commented May 28, 2020

In your component have a

<div style="display: none" @ref=MyContentRef`>@ChildContent</div>

In OnAfterRenderAsync you can call some JavaScript to get the innerHtml of MyContentRef and then display that string in your <code>.

That worked ... Thanks..

@sps014
Copy link
Author

sps014 commented May 28, 2020

@javiercn any plan to override ToString Method of RenderFragment in Future?

@javiercn
Copy link
Member

@sps014 no. RenderFragment is also a delegate type, so I don't think that's even possible.

@sps014
Copy link
Author

sps014 commented May 28, 2020

Oops didn't know that.. i am closing this issue for now as i have other workarounds to deal with the current problem.

@sps014 sps014 closed this as completed May 28, 2020
@benm-eras
Copy link

For what it's worth I would like to be able to access the the content of a tag/component as a string rather than a render fragment. In my case I am trying to make a component that simply passes markdown content to HTML. My desired Markdown.razor component:

<div class="some-container">
    @Html
</div>

@code {
    [Parameter]
    public RenderFragment ChildContent { get; set; }

    public string Html => Markdig.Markdown.ToHtml(this.ChildContent.ToString());
}

How I would use it:

<div class="foo">
    <Markdown>
        ## A Header of some description

       Paragraph 1

       Paragraph 2
    </Markdown>
</div>

Passing that as a parameter just feels dirty and I think I would prefer to just write the HTML at that point.

@ghost ghost locked as resolved and limited conversation to collaborators Jul 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

5 participants