flancy
flancy copied to clipboard
Passing declared variables into New-Flancy -WebSchema
This may not be possible but it would be nice if variables could be passed in the -WebSchema parameter of New-Flancy:
$thing = "SomeText"
New-Flancy -WebSchema {
Get "/" {
$thing
}
}
The only reason I did this was because things like $PSScriptRoot don't seem to exist in the scope of those cmd blocks.
I think the only way to achieve this would be to build a dynamic script block. For example:
$thing
$scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock(@"
$thing
"@)
New-Flancy -WebSchema {
Get "/" $scriptblock
}
This brought up a really old memory: https://powertoe.wordpress.com/2010/02/10/dynamic-code-in-powershell/
It might be interesting to use a scriptblock property with some sort of variable list property that we interpret and inject vars from the current session, but then again, doing it the PS way with NewScriptBlock() is probably the better way to do this.
I tried this:
$text = "Hello world"
$sb = $ExecutionContext.InvokeCommand.NewScriptBlock(@"
$text
"@)
New-Flancy -url http://localhost:8002 -webschema @(
Get "/" {
$sb
}
)
and when I go to the url, the page is blank.
tired it like this as well:
$text = "Hello world"
$sb = $ExecutionContext.InvokeCommand.NewScriptBlock(@"
$text
"@)
New-Flancy -url http://localhost:8002 -webschema @(
Get "/" $sb
)
still blank.
That's probably because Hello world isn't a function or anything. If you want to return it, you'd need to do this
$sb = $ExecutionContext.InvokeCommand.NewScriptBlock(@"
"$text"
"@)
You can always test the code locally too: & $sb
See if that works