2

I am having a hard time figuring out how to do this...

I am essentially saving a huge blog post in a property called "Body" in a class called "Post". In body I will have various things like

<p> Hello world </p>
<p> Some random paragraph </p>
<codeblock> Here is an example of a basic HTML page
<html>
<body>
<h1> Hello Guys ! </h1>
</body>
</html>
</codeblock>

Then I want to have a code block and thus I want the HTML/CSS/Javascript/etc to just be parsed to the page as HTML encoded/decoded so I literally want the tags and angle brackets to show up on the page instead of being parsed as whatever they are.

I also have a HTML tag called which is ended by . It's nothing special it just indents and adds some specific CSS with it. I want the markup before the and after the tag to render the HTML tags as necessary.

Currently I am literally outputting the contents of the Body property using

@Html.Raw(post.Body)

Nothing special when I save it to the DB:

@Html.TextAreaFor(model => model.Body)
Spets
  • 2,391
  • 4
  • 24
  • 26
  • If I understand this correctly, you want to show the raw version (which will let the browser parse the markup as html) with @Html.Raw(post.Body) and the version where its not parsed @Html.Display(post.Body) or @Html.DisplayFor(model => model.Body) where the tags are escaped. – janhartmann Feb 16 '15 at 06:55
  • Correct @janhartmann – Spets Feb 16 '15 at 07:03
  • I want to post both the raw: Html.Raw(post.Body) but somehow within that block I want to also parse portions just as @Html.Display.... – Spets Feb 16 '15 at 07:04
  • I think I might just try Google's Prettify css/js files and see if that works better. Thoughts? – Spets Feb 16 '15 at 07:05
  • 1
    Can you use
     in your markup?
    – janhartmann Feb 16 '15 at 07:09
  • Ah, I think I get what you mean. You need to escape the HTML when saving the content. And keep it unescaped and place it inside
     blocks when display it on the page with .Raw();
    – janhartmann Feb 16 '15 at 07:16
  • Yeah I can use
     in the comments.  I just did that using the prettify library on Google.  Thanks man, this got me going !
    – Spets Feb 16 '15 at 07:18
  • yeah @janhartmann . I tested it out and it seems to work with non-markup languages just fine(C#, SQL, etc), but it completely fails with HTML or anything that has tags. I need to find a way to just display it by having the code convert. I'm thinking of maybe doing the conversion myself on the backend. – Spets Feb 16 '15 at 07:51
  • After more research I figured it out. You can use the and tag! – Spets Feb 16 '15 at 07:54
  • I just found out that is depreciated. I'm now lost again :) I guess i could always post it in a parser? – Spets Feb 16 '15 at 08:01
  • Did you ever manage to find a solution to this? If so, can you post your source or git repo? – Joseph Casey May 06 '15 at 21:38
  • @JosephCasey sure see the answer below. – Spets May 08 '15 at 21:15

1 Answers1

3

So for those that are still having this issue, this is how I resolved it.

1) I included prettyprint. See link below

https://google-code-prettify.googlecode.com/svn/trunk/README.html

2) When editing a post I just add the following code //Bunch of code

Example: var http = require("http");

var server = http.createServer(function(req, res){
console.log(req.url);
resp.write("<html><body>" + req.url + "</html></body>");
resp.end();
});

server.listen(3000);

</pre>

In my Razor View I have the following code:

<div class="blog margin-bottom-40" onload="prettyPrint()">
//Bunch of other code up here for my view
    <div class="blogpost">
        @Html.Raw(post.Body)
    </div>
</div>

My blog is www.techiejs.com

Feel free to have a look and if you need another file from my solution let me know. Currently my git repository is private.

Spets
  • 2,391
  • 4
  • 24
  • 26