Is it possible to create Dynamic Pages

Hey guys. I’m just mucking around making a site for myself and wanted to create dynamic pages but I can’t seem to work out how.

This looks like it should work but it creates $NewPage as a string so it fails.
This is a pastebin of the whole script - https://pastebin.com/P77Xs4hs

This is the section I am having trouble with.

$SeriesCollection = @()
foreach ($Series in ($MyBooks.series_overview_title | Sort-Object -Unique)) {
    $NewPage = New-UDPage -Name "$Series" -Icon "home" -Content {
        New-UDCard }

    New-Variable -Name $Series.Replace(" ", "_") -Value $NewPage -Force
    $SeriesCollection += $Series.Replace(" ", "_")
}

$a = @()
foreach ($Series in $SeriesCollection) {
    $a += ("$" + "$Series")
}
#$allVaribles = $a -join ","


# Create Dashboard and Add Pages. 
$MyBooksDashboard = New-UDDashboard -Theme $Theme -Title "GRDB" -Pages @($a) -EndpointInitialization $EI

#Run Dashboard
Start-UDDashboard -Dashboard $MyBooksDashboard -Port 7777 -AutoReload -Endpoint @($EndpointMyBooks) 

It looks like it should work but the $NewPage is a string so it won’t read it. Has anyone tried something like this that could help me out?

This is the contents of $MyBooks[1] for an example of its contents

id                                 : 42619410
title_without_series               : 
title                              : The Darkest Revenge (The Elder Stones Saga #1)
small_image_url                    : https://s.gr-assets.com/assets/nophoto/book/50x75-a91bf249278a81aabab721ef782c4a74.png
large_image_url                    : 
link                               : https://www.goodreads.com/book/show/42619410-the-darkest-revenge
description                        : <b>Some are born to power. Others must find it within themselves.</b> <br /><br />Decades ago, a powerful enemy called the Forgers attacked their homeland, nearly destroying everything. Few remember the time before the attack, but 
                                     the Forgers have not forgotten. They have spent decades preparing, gaining power, and now their plan has nearly reached fruition. <br /><br />On the outskirts of the city, living within the stronghold of ancient power, Haern longs 
                                     for a life beyond what he knows?and away from the long shadow cast by his father. When an attack on the city renews a decades old battle, Haern volunteers to leave the city and find the one person that can lead their people to 
                                     victory. His inexperience might kill him, but his instincts might be the reason they succeed. <br /><br />For Daniel, a member of the ruling family, life in the palace is easy and his path is clear. Eventually, he will rise up to sit 
                                     on the council and lead the people of the city. An injury to someone he cares for changes everything for him, and he must go to the one person he despises most for help. If he fails, he?ll learn what it means to lose everything he 
                                     cares about. <br /><br />When their fates intertwine, they must put aside past differences. Stopping the Forgers will require strength and power that both must find within them?or more than their people will suffer.
work_id                            : 66338165
author_image_url                   : image_url
author_small_image_url             : small_image_url
author_id                          : 7231848
series_overview_XML                : #document
series_overview_title              : The Elder Stones Saga
series_overview_primary_work_count : 1
series_overview_work_id            : 248588
owned_books_in_series              : 0

I found out there is something built in for dynamic pages but I can’t work out how to use it.
This is the example from the site:
$Dynamic = New-UDPage -Url “/dynamic/:name” -Endpoint {
param($name)
$ScriptColors = @{
BackgroundColor = “#5A5A5A
FontColor = “#FFFFFF
}
New-UDCard @ScriptColors -Title “Welcome to a dynamic page, $name!” -Text “This is a dynamic page. The content of the page is determined by the URL.”
}

It sets param($name) but i’m not sure how I can pass that an array of strings.
I would expect something like this

New-UDPage -Name $StrArray -Endpoint{
param($name)
}

This way i would be creating a new page for each item in the array…

Anyone help with what i’m missing?

You found the right example.
But you have to change your concept I think.
On the HOME you can list all your books with a link.
If you click the link, you can forward to One UD-dynamic page + a book variable.
Then you do the rest of your code with the details for the provided book-variable.

So, if i’m following. It doesn’t actually create a bunch of pages that would show in the menu for example, but will create the page when the link is followed.
That does make more sense, i was thinking it might end up taking ages to run, but if its doing it on the fly that would solve that issue.
I was sure there was something wrong with my logic, but unfortunately I couldn’t think of another way to do it.

Thanks for the tip. I’ll rework it and see what happens.

1 Like

Hi Gary, glad that this could a help you a bit.
If you want it in the menu, you can also add the links in the menu with New-UDSideNavItem.
But if you have a lot of books to display a searchable grid can have some advantages.
I am curious what you will achieve.

Best wishes Marco

I don’t need it in a menu, was just using that as a example of what i thought it was doing…

I was looking for an idea an landed on my ebooks because I have all these series that I haven’t finished. So, I thought a good little dashboard for me would be to have a page with all the series and show on the card ‘Books owned’ \ ‘Total books’ in series (pulled from goodreads which already has this info, so its a useless dashboard). Then when I click on the card I was thinking of going to a new page that contains a card for each book in the series with unowned in a lighter colour to show they are not owned.

It’s really just about learning more about the app, I want to build up a bunch of these dashboard\portals for work so that I can get them to buy it. Right now my company is deciding on the systems that are hosting the data so I can’t build anything until I know if they are going to stick with the current applications or not.
I have built a simple dashboard that I use, it has some things like LAPs lookup, tickets open, zoom rooms etc, so I have no problem with using APIs etc, just getting them into a portal to make it useful.

My issue with PUD seems to just be how to pass things into a foreach loop. I keep wanting to put a variable in the parameters of the function but that always fails or crashes the dashboard.

for example.
Foreach ($a in $b){
New-udcard -title $a -content {
}
}

Hi Gary, thanks for sharing your background. I am in a similar situation. Looking around with a lot of ideas, but at first want to know everything about it, before I go in a project for business. To create a private project is a very good idea. It helps to learn an understand, but keeps the pressure away that it brings when you say “I will build a ud for a important business case”

For your problem. When I see your code snippets. I think you do not need help with the basics.
Maybe your foreach does not work because the $b array was created outside of an Endpoint and you try to enumerate it inside of an Endpoint?

The endpoints are seperate runspaces, so you have to pass variables with -Argumentlist
Or by utilizing the scopes $cache: or $session: that Adam created.

Best wishes, Marco

So it was just something really simple.
I was storing my info like this $Cache:Var but calling it like this $Var
Just had to call it with the $Cached: part and it works as expected.

Its still not behaving like I would expect.

Take this for example

$Pages = New-UDPage -Name "Home" -Icon "home" -Content { 
    New-UDLayout -Columns 6 -Content { 
        foreach ($Cache:Book in $Cache:MyBooks) {
            New-UDCard -Title ($Cache:Book.title) -Content {
                New-UDImage -Url ($Cache:Book.large_image_url)
                New-UDButton -Text "Series Overview" -OnClick {
                    Show-UDModal -Content {
                        New-UDParagraph -Text ($Cache:Book.description)
                    }
                }
                New-UDParagraph -Text ($Cache:Book.description)
            }  -Size 'small' -BackgroundColor "#404552"
        }
    }  
} -AutoRefresh -RefreshInterval 120

This line here (in New-UDCard) works as expected and displays the right description.
New-UDParagraph -Text ($Cache:Book.description)

This one on the other hand only displays the last description that was gathered at the end of the foreach loop.

Show-UDModal -Content {
            New-UDParagraph -Text ($Cache:Book.description)
       }

So it looks like the code in the show-udmodel is only generated when the button is clicked. But I want the contents to be in there already.