Skip to content

Commit

Permalink
feat(export): Add template_params parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Jun 27, 2024
1 parent 0e1f457 commit ab688ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
17 changes: 14 additions & 3 deletions R/app_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ write_app_json <- function(
app_info,
destdir,
html_source_dir,
template_params = list(),
verbose = is_interactive()
) {
verbose_print <- if (isTRUE(verbose)) message else list
Expand All @@ -175,13 +176,23 @@ write_app_json <- function(
# the template parameters.
template_html_files <- fs::dir_ls(html_source_dir, recurse = TRUE, glob = "**.html")

template_params <- rlang::dots_list(
# Forced parameters
REL_PATH = subdir_inverse,
APP_ENGINE = "r",
# User parameters
!!!template_params,
# Default parameters
title = "Shiny App",
.homonyms = "first"
)

for (template_file in template_html_files) {
file_content <- brio::read_file(template_file)

file_content_interp <- glue::glue(
file_content_interp <- glue::glue_data(
template_params,
file_content,
REL_PATH = subdir_inverse,
APP_ENGINE = "r",
.open = "{{",
.close = "}}"
)
Expand Down
19 changes: 17 additions & 2 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
#' @param template_dir Path to a custom template directory to use when exporting
#' the shinylive app. The template can be copied from the shinylive assets
#' using: `fs::path(shinylive:::assets_dir(), "export_template")`.
#' @param template_params A list of parameters to pass to the template. The
#' supported parameters depends on the template being used. Custom templates
#' may support additional parameters (see `template_dir` for instructions on
#' creating a custom template or to find the current shinylive assets'
#' templates).
#'
#' With shinylive assets > 0.4.1, the default export template supports the
#' following parameters:
#'
#' 1. `title`: The title of the app. Defaults to `"Shiny app"`.
#' 2. `include_in_head`, `include_before_body`, `include_after_body`: Raw
#' HTML to be included in the `<head>`, just after the opening `<body>`,
#' or just before the closing `</body>` tag, respectively.
#' @param ... Ignored
#' @export
#' @return Nothing. The app is exported to `destdir`. Instructions for serving
Expand All @@ -45,7 +58,8 @@ export <- function(
wasm_packages = TRUE,
package_cache = TRUE,
assets_version = NULL,
template_dir = NULL
template_dir = NULL,
template_params = list()
) {
verbose_print <- if (verbose) message else list
if (is.null(assets_version)) {
Expand Down Expand Up @@ -178,7 +192,8 @@ export <- function(
app_info,
destdir,
html_source_dir = template_dir %||% fs::path(assets_path, "export_template"),
verbose = verbose
verbose = verbose,
template_params = template_params
)

verbose_print(
Expand Down
20 changes: 18 additions & 2 deletions man/export.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ab688ef

Please sign in to comment.