Skip to content

Commit 721ced6

Browse files
committed
new handlebars helper for custom components: app_config
1 parent 8aa3f29 commit 721ced6

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- allow using `FALSE as tile_source` to completely remove the base map. This makes the map component useful to display even non-geographical geometric data.
99
- Fix a bug that occured when no `database_url` was provided in the configuration file. SQLPage would generate an incorrect default SQLite database URL.
1010
- Add a new `background_color` attribute to the [card](https://sql.ophir.dev/documentation.sql?component=card#component) component to set the background color of the card.
11+
- new handlebars helper for [custom components](https://sql.ophir.dev/custom_components.sql): `{{app_config 'property'}}` to access the configuration object from the handlebars template.
1112

1213
## 0.23.0 (2024-06-09)
1314

examples/official-site/custom_components.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ and SQLPage adds a few more:
129129
- if the argument is a string containing a valid json list, returns the parsed list,
130130
- otherwise returns a list containing only the argument
131131
- `array_contains`: returns true if a list contains a value
132+
- `static_path`: returns the path to one of the static files bundled with SQLPage. Accepts arguments like `sqlpage.js`, `sqlpage.css`, `apexcharts.js`, etc.
133+
- `app_config`: returns the value of a configuration parameter from sqlpage''s configuration file, such as `max_uploaded_file_size`, `site_prefix`, etc.
132134
- `icon_img`: generate an svg icon from a *tabler* icon name
133135
- `markdown`: renders markdown text
134136
- `each_row`: iterates over the rows of a query result

src/app_config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::Context;
22
use config::Config;
33
use percent_encoding::AsciiSet;
44
use serde::de::Error;
5-
use serde::{Deserialize, Deserializer};
5+
use serde::{Deserialize, Deserializer, Serialize};
66
use std::net::{SocketAddr, ToSocketAddrs};
77
use std::path::{Path, PathBuf};
88

@@ -280,7 +280,7 @@ fn default_https_acme_directory_url() -> String {
280280
"https://acme-v02.api.letsencrypt.org/directory".to_string()
281281
}
282282

283-
#[derive(Debug, Deserialize, PartialEq, Clone, Copy, Eq, Default)]
283+
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Copy, Eq, Default)]
284284
#[serde(rename_all = "lowercase")]
285285
pub enum DevOrProd {
286286
#[default]

src/template_helpers.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use handlebars::{
88
};
99
use serde_json::Value as JsonValue;
1010

11+
/// Simple static json helper
12+
type H0 = fn() -> JsonValue;
1113
/// Simple json to json helper
1214
type H = fn(&JsonValue) -> JsonValue;
1315
/// Simple json to json helper with error handling
@@ -41,6 +43,7 @@ pub fn register_all_helpers(h: &mut Handlebars<'_>, config: &AppConfig) {
4143

4244
// static_path helper: generate a path to a static file. Replaces sqpage.js by sqlpage.<hash>.js
4345
register_helper(h, "static_path", StaticPathHelper(site_prefix.clone()));
46+
register_helper(h, "app_config", AppConfigHelper(config.clone()));
4447

4548
// icon helper: generate an image with the specified icon
4649
h.register_helper("icon_img", Box::new(IconImgHelper(site_prefix)));
@@ -154,6 +157,27 @@ impl CanHelp for StaticPathHelper {
154157
}
155158
}
156159

160+
/// Generate the full path to a builtin sqlpage asset. Struct Param is the site prefix
161+
struct AppConfigHelper(AppConfig);
162+
163+
impl CanHelp for AppConfigHelper {
164+
fn call(&self, args: &[PathAndJson]) -> Result<JsonValue, String> {
165+
let static_file = match args {
166+
[v] => v.value(),
167+
_ => return Err("expected one argument".to_string()),
168+
};
169+
let name = static_file
170+
.as_str()
171+
.ok_or_else(|| format!("app_config: not a string: {static_file}"))?;
172+
match name {
173+
"max_uploaded_file_size" => Ok(JsonValue::Number(self.0.max_uploaded_file_size.into())),
174+
"environment" => serde_json::to_value(self.0.environment).map_err(|e| e.to_string()),
175+
"site_prefix" => Ok(self.0.site_prefix.clone().into()),
176+
other => Err(format!("unknown app config property: {other:?}")),
177+
}
178+
}
179+
}
180+
157181
/// Generate an image with the specified icon. Struct Param is the site prefix
158182
struct IconImgHelper(String);
159183
impl HelperDef for IconImgHelper {
@@ -333,6 +357,15 @@ trait CanHelp: Send + Sync + 'static {
333357
fn call(&self, v: &[PathAndJson]) -> Result<JsonValue, String>;
334358
}
335359

360+
impl CanHelp for H0 {
361+
fn call(&self, args: &[PathAndJson]) -> Result<JsonValue, String> {
362+
match args {
363+
[] => Ok(self()),
364+
_ => Err("expected no arguments".to_string()),
365+
}
366+
}
367+
}
368+
336369
impl CanHelp for H {
337370
fn call(&self, args: &[PathAndJson]) -> Result<JsonValue, String> {
338371
match args {

0 commit comments

Comments
 (0)