In the last part of Part – 9, we will update the View Page to render the dynamically created page through the Manage Page component in the Admin section. Our top menu is also managed in the Admin section, then Manage Page section stores the corresponding menu item when we create the new page. When the user will click on any menu item, the menu ID will pass to View Page that will load the page HTML from the database and render it on the screen.
Introduction
This is the last part of Part 9 where finally we will render the dynamically created page. The menu and page are connected to each other through the menu ID. When a user clicks on any menu item, the menu ID is being passed to View Page. We will remove all the static HTML and related typescript in ViewPage
component, write new code to take the menu ID and load the corresponding page’s HTML from the database. We will also introduce a new Pipe to properly render the HTML.
Let’s Start
It is strongly recommended to read all previous parts before moving to this one, I can’t promise you would understand all the steps I am going to list down without knowledge of previous parts.
- As always, make sure the last part is working fine, you are successfully able to Add, Update and Delete the page and its content.
- First, let’s create a custom Pipe to bypass any kind of HTML special characters e.g. any type of scripting (Javascript, C#, Java, etc.) to properly view the code on the screen, this is an optional step if you know that it is not required for your application, it’s better NOT to use it cause it makes your application vulnerable to Cross Site Scripting (XSS) where user can inject malicious Javascript in your code and screw your application. I am only using it here to show you how you can actually use the
DomSanitizer
class. Read more aboutDomSanitizer
from here. - Right-click on src -> app -> shared folder, select the option, New File. Enter the name keepHtml.pipe.ts. Paste the following code in it:
- The class metadata defines that it is a Pipe with the name
keepHtml
. We are implementing aPipeTransform
interface that has only one methodtransform
taking the input value that in our case would be the page HTML. ThebypassSecurityTrustHtml()
method will let all HTML render as it is without sanitizing the potential dangerous text e.g. <script>some crapy script</script> that otherwise would be viewed as <script>some crapy script</script> where < and > are converted into < and > to avoid executing the unwanted script. - Next, add the reference of
keepHtml
Pipe inAppModule
, edit the src -> app -> app.module.ts and replace its content with the following:
- Now, edit the src -> app -> client -> view-page -> view-page.component.css and replace its content with our famous CSS:
mat-card
{
margin: 0px;
padding: 0px;
}
mat-card-noshadow{
background: #ECECF4;
box-shadow: none !important;
}
mat-card-content{
margin: 0px;
padding: 0px;
}
.header
{
background: #45484d;
border-bottom: 5px solid #393B3E;
height: 50px;
padding-left: 5px;
}
.subheader
{
height: 40px;
background: #5B5C60;
}
.subheader_title{
vertical-align:baseline;
padding-top: 5px;
padding-bottom: 0px;
padding-left: 5px;
font-size: 13pt;
font-family: Arial, Helvetica, sans-serif;
color: #C8CCD2;
}
.header_title{
vertical-align:baseline;
padding-top: 10px;
padding-bottom: 0px;
padding-left: 5px;
font-size: 16pt;
font-family: Arial, Helvetica, sans-serif;
color: #A1A9AF;
}
.card_cntnt
{
padding: 15px;
padding-bottom: 15px;
}
.content_header
{
font-size:16pt;
color: coral;
}
.content_subheader
{
color: #908482;
}
- Next, src -> app -> client -> view-page -> view-page.component.html and replace its content with following:
<mat-card>
<mat-card-header class="header">
<mat-card-title class="header_title">{{page.Title}}</mat-card-title>
</mat-card-header>
<mat-card-content class="card_cntnt">
<div [innerHTML]="page.Content | keepHtml"></div>
</mat-card-content>
</mat-card>
- You can see, the page title is coming from
page
object that is loading from the database, in<div [innerHTML]="page.Content | keepHtml"></div>
, we are assigning thepage.Content
and using our newly created PipekeepHtml
to bypass the special script related characters to view everything to a user without HTML sanitization. - Edit the src -> app -> client -> view-page -> view-page.component.ts and replace its code with the following:
- That’s pretty simple code:
- On a top, we specify the API endpoint (/api/page/id) to load the page by menu ID.
- In a
constructor
, we are creating theDataService
andActivatedRoute
services object. The route object is used to get the[routerLink]
parameter value passed to theViewPage
component. (check the app.component.html for reference). - The
params['id']
is a menu ID that is passed toloadContentbyId()
function to load the page from the database. - The
loadContentbyId()
function is taking menu ID as an input parameter, concatenating it with API URL and calling the data service GET method to load the page record from the database. The result is stored in apage
object that we are using in view-page.component.html to show the page data on a screen.
- That’s all for Part 9. Now, create different menu items, pages and check if you are successfully able to see them on screen. I am also attaching the small video for your view where first I will
- Go to the admin section.
- Go to Menus Management.
- Create a new menu.
- Go to Page Management.
- Create a simple page and save it.
- After refreshing the website, both menu and page would be visible on a screen.