flancy icon indicating copy to clipboard operation
flancy copied to clipboard

Passing declared variables into New-Flancy -WebSchema

Open dotps1 opened this issue 8 years ago • 5 comments

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.

dotps1 avatar Aug 04 '16 14:08 dotps1

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/

toenuff avatar Aug 09 '16 00:08 toenuff

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.

toenuff avatar Aug 09 '16 00:08 toenuff

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.

dotps1 avatar Aug 09 '16 17:08 dotps1

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.

dotps1 avatar Aug 09 '16 17:08 dotps1

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

toenuff avatar Aug 10 '16 17:08 toenuff