Understanding the Context API
PocketPages provides a comprehensive Context API that is magically available in every EJS template. The API methods and properties enable you to manage request data, handle responses, work with assets, and more. This guide will walk you through the key components of the Context API and how to use them effectively.
How the API is Available in EJS
All Context API methods are automatically available in your EJS templates without any prefix. For example, you can use asset()
for asset management or echo()
for output. This makes your templates cleaner and more readable.
The Context API also includes all Global API functions like dbg()
, stringify()
, and url()
. While these Global API functions can be used anywhere via require('pocketpages')
, they're automatically available in templates alongside the Context API methods.
Example: Using the Context API in an EJS File
Let's look at a practical example demonstrating various API methods:
<h1>Product ID: <%= params.productId %></h1>
<p>Requested by: <%= request.header('User-Agent') %></p>
<% if (params.sort) { %>
<p>Sorting by: <%= params.sort %></p>
<% } else { %>
<p>No sorting specified.</p>
<% } %>
<%
// Using Context API methods
const logoUrl = asset('images/logo.png');
meta.title = "Product Details";
// Using Global API functions
dbg('Processing product:', stringify(params));
%>
<img src="<%= logoUrl %>" alt="Logo">
<%
// Set metadata for the page
meta.title = "Product Details";
meta.description = "View details for product " + params.productId;
%>
<%
// Echo some HTML content
echo("<div>Dynamic content here</div>");
%>
<%
// Handle form data
if (formData) {
// Process form submission
}
%>
Explanation:
- Request Data: Access request parameters through
params
and request details viarequest
- Asset Management: Use
asset()
to handle static assets with automatic cache-busting - Response Handling: Use
echo()
to output content andresponse
to manage response headers - Form Data: Access form submissions through
formData
- Metadata: Set page metadata using
meta
- API Access: Make API calls using
api
Available Context Properties
The Context API provides several key properties and methods:
api
- Make API calls to PocketBaseasset()
- Handle static assets with cache bustingdata
- Store and retrieve template dataecho()
- Output content to the responseformData
- Access form submission datameta
- Manage page metadataparams
- Access URL and query parametersredirect()
- Perform redirectsrequest
- Access request detailsresolve()
- Resolve URLs and pathsresponse
- Manage response headers and status
Additionally, all Global API functions are available:
- Logging functions (
dbg
,info
,warn
,error
) - Data handling (
stringify
,url
) - Database helpers (
findRecordByFilter
,findRecordsByFilter
) - Micro-dash utilities (
forEach
,keys
,values
,merge
)
Additional Notes
- Environment Variables: Access environment variables through configuration
- Cache Busting: The
asset
function automatically handles cache busting in development mode - Response Management: Use
response
methods to set headers, status codes, and manage the response
For detailed information about specific API methods, please refer to the individual API documentation pages in the sidebar.