Skip to content

Commit

Permalink
#294: Add the includes function change to Responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Jul 8, 2019
1 parent f58e3cb commit 6c8b8f3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 46 deletions.
2 changes: 1 addition & 1 deletion docs/Tutorials/ViewEngines/Pode.md
Expand Up @@ -94,7 +94,7 @@ This next quick example allows you to include content from another view:
```html
<!-- /views/index.pode -->
<html>
$(include shared/head)
$(Use-PodePartialView -Path 'shared/head')

<body>
<span>$([DateTime]::Now.ToString('yyyy-MM-dd HH:mm:ss');)</span>
Expand Down
5 changes: 2 additions & 3 deletions src/Pode.psd1
Expand Up @@ -40,7 +40,6 @@
'Test-IsWindows',
'Test-IsPSCore',
'Test-Empty',
'Include',
'Lock',
'Listen',
'Access',
Expand All @@ -64,7 +63,6 @@
'Config',
'Csrf',
'Gui',
'Header',

# cookies
'Get-PodeCookie',
Expand Down Expand Up @@ -112,7 +110,8 @@
'Write-PodeTcpClient',
'Read-PodeTcpClient',
'Save-PodeResponseFile',
'Set-PodeViewEngine'
'Set-PodeViewEngine',
'Use-PodePartialView'
)

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
Expand Down
53 changes: 52 additions & 1 deletion src/Public/Responses.ps1
Expand Up @@ -544,7 +544,7 @@ Renders a dynamic, or static, View on the Response.
Renders a dynamic, or static, View on the Response; allowing for dynamic data to be supplied.
.PARAMETER Path
The path to a view, relative to the "/views" directory. (Extension is optional).
The path to a View, relative to the "/views" directory. (Extension is optional).
.PARAMETER Data
Any dynamic data to supply to a dynamic View.
Expand Down Expand Up @@ -970,4 +970,55 @@ function Set-PodeViewEngine
$PodeContext.Server.ViewEngine.Extension = $Extension
$PodeContext.Server.ViewEngine.Script = $ScriptBlock
$PodeContext.Server.ViewEngine.IsDynamic = ($Type -ine 'html')
}

<#
.SYNOPSIS
Includes the contents of a partial View into another dynamic View.
.DESCRIPTION
Includes the contents of a partial View into another dynamic View. The partial View can be static or dynamic.
.PARAMETER Path
The path to a partial View, relative to the "/views" directory. (Extension is optional).
.PARAMETER Data
Any dynamic data to supply to a dynamic partial View.
.EXAMPLE
Use-PodePartialView -Path 'shared/footer'
#>
function Use-PodePartialView
{
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]
$Path,

[Parameter()]
$Data = @{}
)

# default data if null
if ($null -eq $Data) {
$Data = @{}
}

# add view engine extension
$ext = Get-PodeFileExtension -Path $Path
if ([string]::IsNullOrWhiteSpace($ext)) {
$Path += ".$($PodeContext.Server.ViewEngine.Extension)"
}

# only look in the view directory
$Path = (Join-Path $PodeContext.Server.InbuiltDrives['views'] $Path)

# test the file path, and set status accordingly
if (!(Test-PodePath $Path -NoStatus)) {
throw "File not found at path: $($Path)"
}

# run any engine logic
return (Get-PodeFileContentUsingViewEngine -Path $Path -Data $Data)
}
37 changes: 0 additions & 37 deletions src/Public/Utilities.ps1
Expand Up @@ -56,43 +56,6 @@ function Dispose
}
}

function Include
{
param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[Alias('p')]
[string]
$Path,

[Parameter()]
[Alias('d')]
$Data = @{}
)

# default data if null
if ($null -eq $Data) {
$Data = @{}
}

# add view engine extension
$ext = Get-PodeFileExtension -Path $Path
if (Test-Empty $ext) {
$Path += ".$($PodeContext.Server.ViewEngine.Extension)"
}

# only look in the view directory
$Path = (Join-Path $PodeContext.Server.InbuiltDrives['views'] $Path)

# test the file path, and set status accordingly
if (!(Test-PodePath $Path -NoStatus)) {
throw "File not found at path: $($Path)"
}

# run any engine logic
return (Get-PodeFileContentUsingViewEngine -Path $Path -Data $Data)
}

function Lock
{
param (
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Responses.Tests.ps1
Expand Up @@ -358,7 +358,7 @@ Describe 'Write-PodeFileResponse' {
}
}

Describe 'Include' {
Describe 'Use-PodePartialView' {
$PodeContext = @{
'Server' = @{
'InbuiltDrives' = @{ 'views' = '.' }
Expand All @@ -368,18 +368,18 @@ Describe 'Include' {

It 'Throws an error for a path that does not exist' {
Mock Test-PodePath { return $false }
{ Include -Path 'sub-view.pode' } | Should Throw 'File not found'
{ Use-PodePartialView -Path 'sub-view.pode' } | Should Throw 'File not found'
}

Mock Test-PodePath { return $true }
Mock Get-PodeFileContentUsingViewEngine { return 'file contents' }

It 'Returns file contents, and appends view engine' {
Include -Path 'sub-view' | Should Be 'file contents'
Use-PodePartialView -Path 'sub-view' | Should Be 'file contents'
}

It 'Returns file contents' {
Include -Path 'sub-view.pode' | Should Be 'file contents'
Use-PodePartialView -Path 'sub-view.pode' | Should Be 'file contents'
}
}

Expand Down

0 comments on commit 6c8b8f3

Please sign in to comment.