Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_dynamic_template.h
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Declares template rendering functions for the in-process dynamic runtime.
4 *
5 * These functions are exposed from native_json_runtime_ops.c so that
6 * the dynamic runtime handler (native_dynamic_runtime.c) can render
7 * page templates in-process without shelling out.
8 *
9 * @see native_dynamic_runtime.h for the higher-level entry point.
10 * @see native_json_runtime_ops.c for the implementations.
11 */
12
13#ifndef PHAROS_NATIVE_DYNAMIC_TEMPLATE_H
14#define PHAROS_NATIVE_DYNAMIC_TEMPLATE_H
15
16#include "yyjson.h"
17
18/** @name Template rendering helpers
19 * @{
20 */
21
22/**
23 * @brief Renders one HTML template text string with JSON parameter substitution.
24 *
25 * Plain @c {key} placeholders render into HTML text context and are escaped by
26 * default. Explicit prefixes are available for other trusted contexts:
27 * - @c {html:key} inserts a trusted raw HTML fragment
28 * - @c {path:key} inserts a validated path/reference for URL attributes
29 * - @c {url:key} inserts a validated URL/reference for URL attributes
30 *
31 * @param template_text Raw template string with placeholder expressions.
32 * @param params_root JSON object providing values for each key.
33 * @return Newly allocated rendered string, or NULL on failure.
34 * Caller must free.
35 */
36char *render_template_text_dup_runtime(const char *template_text, yyjson_val *params_root);
37
38/**
39 * @brief Renders one path-oriented template text string with JSON substitution.
40 *
41 * This variant is for internal route or action path assembly rather than HTML
42 * body rendering. Plain placeholders are validated as path references by
43 * default while still accepting the explicit @c {path:key} and @c {url:key}
44 * forms.
45 *
46 * @param template_text Raw path template string with placeholder expressions.
47 * @param params_root JSON object providing values for each key.
48 * @return Newly allocated rendered string, or NULL on failure.
49 * Caller must free.
50 */
51char *render_path_template_text_dup_runtime(const char *template_text, yyjson_val *params_root);
52
53/**
54 * @brief Reads a template file and renders it with JSON parameter substitution.
55 *
56 * @param template_file File-system path to the template.
57 * @param params_root JSON object providing values for each placeholder.
58 * @return Newly allocated rendered string, or NULL on failure.
59 * Caller must free.
60 */
61char *render_file_template_dup_runtime(const char *template_file, yyjson_val *params_root);
62
63/**
64 * @brief Parses JSON text into a yyjson_val root.
65 *
66 * @param text NUL-terminated JSON text.
67 * @param doc_out Output pointer to the parsed document (must be freed
68 * by caller with native_json_doc_free()).
69 * @return Root value, or NULL on parse failure.
70 */
71yyjson_val *load_root_from_text_runtime(const char *text, yyjson_doc **doc_out);
72
73/**
74 * @brief Rewrites relative URLs in HTML to use a base path.
75 *
76 * @param html Original HTML string.
77 * @param base_path URL base path to prepend, or NULL for no change.
78 * @return Newly allocated scoped HTML string, or NULL on failure.
79 * Caller must free.
80 */
81char *apply_base_path_html_dup_runtime(const char *html, const char *base_path);
82
83/**
84 * @brief Resolves one UCAL declared page id to its app-relative page-spec path.
85 *
86 * @param page_id Declared UCAL page id (e.g. "login", "register").
87 * @return Matching relative path string, or NULL when unknown.
88 * @note The returned pointer is borrowed from an internal table and must
89 * NOT be freed by the caller.
90 */
91char *declared_page_relative_path_dup_runtime(const char *app_root, const char *page_id);
92
93/**
94 * @brief Joins a base path and a relative path into one string.
95 *
96 * @param base_path Base directory path.
97 * @param relative_path Relative file path.
98 * @return Newly allocated joined path, or NULL on failure.
99 * Caller must free.
100 */
101char *join_base_relative_path_dup_runtime(const char *base_path, const char *relative_path);
102
103/**
104 * @brief Builds the nav context and renders a full layout page.
105 *
106 * Constructs the navigation HTML (language selector, login/logout links,
107 * profile links, etc.) and renders the layout template with the given
108 * body HTML embedded. Applies base-path scoping to the result.
109 *
110 * @param app_root App root directory (for finding templates/layout/).
111 * @param base_path URL base path for scoping, or NULL.
112 * @param layout_id Layout identifier (e.g. "account_shell").
113 * @param title Page title.
114 * @param body Already-rendered body HTML.
115 * @param current_lang Current language code (e.g. "en"), or NULL.
116 * @param current_request_target Current request path for nav highlighting.
117 * @param has_user Non-zero when a user is authenticated.
118 * @param managed_business_count Number of businesses the user manages.
119 * @return Newly allocated full page HTML, or NULL on failure.
120 * Caller must free.
121 */
123 const char *app_root,
124 const char *base_path,
125 const char *layout_id,
126 const char *title,
127 const char *body,
128 const char *current_lang,
129 const char *current_request_target,
130 int has_user,
131 int managed_business_count
132);
133
134/**
135 * @brief Renders an error alert <div> HTML, or an empty string.
136 * @param message Error message text, or NULL/empty for no alert.
137 * @return Newly allocated HTML string. Caller must free.
138 */
139char *error_alert_html_dup_runtime(const char *message);
140
141/**
142 * @brief Escapes one plain-text string for safe HTML insertion.
143 * @param text Raw text input.
144 * @return Newly allocated escaped HTML string. Caller must free.
145 */
146char *html_escaped_dup_runtime(const char *text);
147
148/**
149 * @brief Renders a hidden next-path <input> HTML, or an empty string.
150 * @param next_path Next URL path, or NULL/empty for no input.
151 * @return Newly allocated HTML string. Caller must free.
152 */
153char *hidden_next_input_html_dup_runtime(const char *next_path);
154
155/**
156 * @brief Builds rendered profile page params JSON from runtime state.
157 *
158 * Uses the native state model plus declarative profile fragments to produce
159 * the parameter object expected by `templates/profile/profile.body.html`.
160 *
161 * @param state_path On-disk runtime state JSON path.
162 * @param user_id Signed-in user id.
163 * @param app_root UCAL app root used to locate fragment templates.
164 * @param payment_preference_options_html Pre-rendered payment preference options.
165 * @return Newly allocated compact JSON string, or NULL on failure.
166 * Caller must free.
167 */
169 const char *state_path,
170 long user_id,
171 const char *app_root,
172 const char *payment_preference_options_html
173);
174
175/**
176 * @brief Builds rendered service-book-form page params JSON from runtime state.
177 *
178 * Uses the native state model plus declarative booking fragments to produce
179 * the parameter object expected by `templates/businesses/service_book_form.body.html`.
180 *
181 * @param state_path On-disk runtime state JSON path.
182 * @param business_slug Business slug from the matched route.
183 * @param service_id Service id from the matched route.
184 * @param app_root UCAL app root used to locate fragment templates.
185 * @param error_message Optional error text to render into the form.
186 * @return Newly allocated compact JSON string, or NULL on failure.
187 * Caller must free.
188 */
190 const char *state_path,
191 const char *business_slug,
192 long service_id,
193 const char *app_root,
194 const char *error_message
195);
196
197/**
198 * @brief Builds rendered business-edit page params JSON from runtime state.
199 *
200 * Uses the native state model to produce the parameter object expected by
201 * `templates/businesses/business_edit.body.html`.
202 *
203 * @param state_path On-disk runtime state JSON path.
204 * @param business_slug Business slug from the matched route.
205 * @return Newly allocated compact JSON string, or NULL on failure.
206 * Caller must free.
207 */
209 const char *state_path,
210 const char *business_slug
211);
212
213/**
214 * @brief Builds rendered appointments page params JSON from runtime state.
215 *
216 * @param state_path On-disk runtime state JSON path.
217 * @param user_id Signed-in user id.
218 * @param app_root App root used to locate declarative fragments when needed.
219 * @return Newly allocated compact JSON string, or NULL on failure.
220 * Caller must free.
221 */
222/**
223 * @brief Builds rendered explore page params JSON from runtime state.
224 *
225 * @param state_path On-disk runtime state JSON path.
226 * @param app_root App root used to locate declarative fragments when needed.
227 * @return Newly allocated compact JSON string, or NULL on failure.
228 * Caller must free.
229 */
231 const char *state_path,
232 const char *app_root
233);
234
235/**
236 * @brief Builds rendered appointments page params JSON from runtime state.
237 *
238 * @param state_path On-disk runtime state JSON path.
239 * @param user_id Signed-in user id.
240 * @param app_root App root used to locate declarative fragments when needed.
241 * @return Newly allocated compact JSON string, or NULL on failure.
242 * Caller must free.
243 */
245 const char *state_path,
246 long user_id,
247 const char *app_root
248);
249
250/**
251 * @brief Builds rendered business dashboard page params JSON from runtime state.
252 *
253 * @param state_path On-disk runtime state JSON path.
254 * @param user_id Signed-in user id.
255 * @param app_root App root used to locate declarative fragments when needed.
256 * @return Newly allocated compact JSON string, or NULL on failure.
257 * Caller must free.
258 */
260 const char *state_path,
261 long user_id,
262 const char *app_root
263);
264
265/**
266 * @brief Builds rendered business admin page params JSON from runtime state.
267 *
268 * @param state_path On-disk runtime state JSON path.
269 * @param business_slug Business slug from the matched route.
270 * @param app_root App root used to locate declarative fragments when needed.
271 * @return Newly allocated compact JSON string, or NULL on failure.
272 * Caller must free.
273 */
275 const char *state_path,
276 const char *business_slug,
277 const char *app_root
278);
279
280/** @} */
281
282#endif
char * business_dashboard_render_page_params_json_dup_runtime(const char *state_path, long user_id, const char *app_root)
Builds rendered business dashboard page params JSON from runtime state.
char * profile_render_page_params_json_dup_runtime(const char *state_path, long user_id, const char *app_root, const char *payment_preference_options_html)
Builds rendered profile page params JSON from runtime state.
char * render_file_template_dup_runtime(const char *template_file, yyjson_val *params_root)
Reads a template file and renders it with JSON parameter substitution.
char * error_alert_html_dup_runtime(const char *message)
Renders an error alert.
char * join_base_relative_path_dup_runtime(const char *base_path, const char *relative_path)
Joins a base path and a relative path into one string.
char * business_detail_admin_render_page_params_json_dup_runtime(const char *state_path, const char *business_slug, const char *app_root)
Builds rendered business admin page params JSON from runtime state.
char * appointments_render_page_params_json_dup_runtime(const char *state_path, long user_id, const char *app_root)
Builds rendered appointments page params JSON from runtime state.
char * render_path_template_text_dup_runtime(const char *template_text, yyjson_val *params_root)
Renders one path-oriented template text string with JSON substitution.
char * render_template_text_dup_runtime(const char *template_text, yyjson_val *params_root)
Renders one HTML template text string with JSON parameter substitution.
char * explore_render_page_params_json_dup_runtime(const char *state_path, const char *app_root)
Builds rendered appointments page params JSON from runtime state.
char * html_escaped_dup_runtime(const char *text)
Escapes one plain-text string for safe HTML insertion.
char * declared_page_relative_path_dup_runtime(const char *app_root, const char *page_id)
Resolves one UCAL declared page id to its app-relative page-spec path.
char * business_edit_page_params_json_dup_runtime(const char *state_path, const char *business_slug)
Builds rendered business-edit page params JSON from runtime state.
char * service_book_form_page_params_json_dup_runtime(const char *state_path, const char *business_slug, long service_id, const char *app_root, const char *error_message)
Builds rendered service-book-form page params JSON from runtime state.
char * hidden_next_input_html_dup_runtime(const char *next_path)
Renders a hidden next-path <input> HTML, or an empty string.
char * apply_base_path_html_dup_runtime(const char *html, const char *base_path)
Rewrites relative URLs in HTML to use a base path.
char * render_page_html_dup_runtime(const char *app_root, const char *base_path, const char *layout_id, const char *title, const char *body, const char *current_lang, const char *current_request_target, int has_user, int managed_business_count)
Builds the nav context and renders a full layout page.
yyjson_val * load_root_from_text_runtime(const char *text, yyjson_doc **doc_out)
Parses JSON text into a yyjson_val root.