09 Nov 2019
Michael Hnat

How to get a page scope when you don't have one

If you're working with routes in your application and have created a custom page endpoint, you don't have a site tree page scope. So, how can you set page fields for meta information, breadcrumbs, etc.? The answer: Initialize a dummy Preside sitetree page.

Sometimes you're generating an output on a website with some other workflow than the default page rendering. For example, if you're working with custom routes in your application to translate URLs like www.myDomain.com/news/123 to show the content of a record.

So you're translating the URL, run into the handler, set a view and a layout and you get a full rendered page. Fine.

But: What if some functionality requires data of a page object, like the breadcrumbs or the _pageMetaForHtmlHead.cfm or _openGraphMeta.cfm? You don't have a page scope.

The answer is: Initialize a dummy Preside sitetree page.

In your handler, you can do this:

event.initializeDummyPresideSiteTreePage(
     parent_page = IDofParentPage (e.g. your news listing page)
   , title       = newsObj.title
   , slug        = newsObj.slug
   , main_image  = newsObj.image
   , teaser      = newsObj.teaser
   , description = newsObj.description
   .... << any other fieds that would normally come from page object, e.g. metadata fields
            );

The function creates a page-scope in the request for you and you can access the fields of the page as you are used to do it in the page-type views.

This is very helpful, if you want to set for example the meta information for SEO, etc. which is based on the data of a sitetree page.