Stephanevg / PSHTML

Cross platform Powershell module to generate HTML markup language
https://pshtml.readthedocs.io/en/latest/
Other
168 stars 43 forks source link

Difficulty using includes. Found another way? #298

Closed coateds closed 2 years ago

coateds commented 2 years ago

Hello, I have gotten very interested in the combination of PSHTML and Polaris. I want to use it for "micro-monitoring" web services on an almost ad-hoc basis. Good job with this module!

I have had some difficulty getting includes to work. I have always been able to in the end, but it is not always clear what I have done to make them work. This was particularly bad when I tried to use an include within a Polaris web site. It did not work for several days and I was just about to write it up as a bug here when it just started to work. I do not know why. I know it has something to do with importing the module as the documentation states. However, that solution has not been reliable. I found an alternative that seems to work rather well. Just call a function.

It will be easier to show you the code I came up with than explain it:


Import-Module pshtml -Force

Function Get-Include ($str) { h3 "This is my $str" }

New-PolarisGetRoute -Path "/includes" -Force -Scriptblock { $Response.SetContentType('text/html')

$HTML = html {
    head {
        Title 'Includes Demo'
    }

    Body {
        Get-Include 'Body'
    }

    Footer {
        Get-Include 'Footer'
    }
}

$Response.Send($HTML)

}


Seems pretty simple and it works every time.

I am raising this as an issue because I am interested to know what direction you think you will take with includes and what problems you may see with my solution. I see a lot of uses for this module and Polaris, I definitely want to follow along with your progress and might even try to contribute someday.

Great Job with this and thanks, -Dave

Stephanevg commented 2 years ago

Hi @coateds ,

Includes are documented here -> https://pshtml.readthedocs.io/en/latest/Includes/Overview/ It states there, that you need to have a folder called 'Includes' in the root of your folder. If you add / remove files from the 'includes' folder, in order to make them 'visibile' again in your intellisense, you have to re-import the module using the -force parameter.