PolarisPowerShellPSHTML

Organize your Polaris Code using PSHTML module

I am back after a couple of “Amstel” to demo structuring the Polaris code using PSHTML module. IMHO PSHTML is made for Polaris! It’s such great module which couples with web apps built using Polaris. Navigate to the hyper link if you are new to Polaris and PSHTML.

Illustration of my project structure.

Our Objective

Now, the code is not really a big challenge because it’s PowerShell. We are familiar to use and explore it as per our requirement. With no theory let me jump over to PowerShell scripts!

using module "C:\Program Files\WindowsPowerShell\Modules\Polaris\0.1.0\Polaris.psd1"
Import-Module PSHTML -Verbose

#REGION STATIC ROUTES
New-PolarisStaticRoute -RoutePath "/assets" -FolderPath "./assets"
New-PolarisStaticRoute -RoutePath "/routes" -FolderPath "./routes"
#ENDREGION

#REGION GET ROUTES
.\routes\home.ps1
.\routes\about.ps1
.\routes\contact.ps1
#ENDREGION

Start-Polaris -Port 8080

Now, we made code refactoring in a simple way – Yes, we have one main page named (main.ps1) and 3 routes (about.ps1 , home.ps1  and contact.ps1). I really loved it! On other hand it took some time for me to understand the static route! I logged a bug in Polaris repo and received help from @tiberriver256 

Here comes the PSHTML Code

about.ps1

New-PolarisGetRoute -Path "/about" -Scriptblock {
    $Response.SetContentType('text/html')
    $Html = html {
        head {
            Title "iPortal About Card"
            Link -href "/assets/styles/about.css" -rel "stylesheet"
        }
        body {
            hr 
            h1 "About Me"
            hr
            p -Content {
                "Text"
            }
        }
    }
    $Response.Send($Html)
}

contact.ps1

New-PolarisGetRoute -Path "/contact" -Scriptblock {
    $Response.SetContentType('text/html')
    $Html = html {
        head {
            Title "iPortal Contact Card"
            Link -href "/assets/styles/about.css" -rel "stylesheet"
        }
        body {
            hr 
            h1 "Contact Form"
            hr
            p -Content {
                "Text"
            }
        }
    }
    $Response.Send($Html)
}

home.ps1

New-PolarisGetRoute -Path "/home" -Scriptblock {
    $Response.SetContentType('text/html')
    $Html = Get-Content .\views\home.html -Raw
    $Response.Send($Html)
}

Enjoy PowerShell!

2 thoughts on “Organize your Polaris Code using PSHTML module

  1. Very cool blog chain you’ve got going here. We’ve thought about adding built-in support for a couple of PowerShell view engines like PSHTML and EPS.

    One suggestion, unless you want to share the source code for your routes you may want to remove the static route for your routes folder since you probably want the server to execute those scripts when /home or /about is requested from the browser. The static route would serve the ps1 file without being executed to anyone requesting it via /routes/home.ps1 which may not be that bad in this demo 🙂 but could be a risk if you thought the code was hidden.

  2. It will awesome to get PSHTML and EPS as view engines! Yes, only for demo I am exposing the code in routes! In parallel I will think alternate as per your suggestion. Thanks for your valuable feedback Micah!

Leave a Reply

Your email address will not be published. Required fields are marked *