{"size": 1589, "ext": "scrbl", "max_stars_repo_path": "polyglot-doc/polyglot/scribblings/how-tos/incorporate-side-effects.scrbl", "max_stars_repo_name": "pmatos/polyglot", "max_stars_repo_stars_event_min_datetime": "2019-08-25T10:41:17.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-03T19:33:04.000Z", "max_issues_repo_path": "polyglot-doc/polyglot/scribblings/how-tos/incorporate-side-effects.scrbl", "max_issues_repo_name": "pmatos/polyglot", "max_issues_repo_issues_event_min_datetime": "2019-08-25T10:44:27.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-03T12:22:16.000Z", "max_forks_repo_path": "polyglot-doc/polyglot/scribblings/how-tos/incorporate-side-effects.scrbl", "max_forks_repo_name": "pmatos/polyglot", "max_forks_repo_forks_event_min_datetime": "2019-12-19T06:18:49.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-13T03:20:07.000Z"}
{"max_stars_count": 89, "max_issues_count": 29, "max_forks_count": 5, "avg_line_length": 31.78, "max_line_length": 92, "alphanum_fraction": 0.686595343}
#lang scribble/manual

@require[@for-label[racket polyglot] "../macros.rkt"]

@title[#:tag "incorporate-se"]{How-To: Include Module Instantiation Side-Effects As Content}
In some cases you may need to capture output before incorporating it into the
page.

@section{Make the Functional Workflow Capture Content Like The Imperative Workflow}

In @(functional-workflow), you can write an app element that, like in
@(imperative-workflow), @racket[read]s tagged X-expressions
written by another module as a side-effect of instantiation:

@racketmod[#:file "<script type=\"application/racket\">...</script>"
racket/base

(require polyglot racket/port)
(provide replace-page)

(define (replace-page page-tx)
  (tx-replace-me page-tx
                 (λ (x)
                   (define-values (i o) (make-pipe))
                   (parameterize ([current-output-port o])
                     (dynamic-require "loud-module.rkt" #f)
                     (close-output-port o))
                   (port->list read i))))]

@section{Make the Imperative Workflow Reformat Invalid Content}

In @(imperative-workflow), app elements cannot @racket[display]
or @racket[print] content if it would result in reading a value
that is not a Tagged X-Expression.

You can use the same trick above to capture and reformat content:

@racketmod[#:file "<script type=\"application/racket\">...</script>"
racket/base

(require racket/port)

(define-values (i o) (make-pipe))
(parameterize ([current-output-port o])
  (dynamic-require "displays-string.rkt" #f)
  (close-output-port o))

(write `(p ,(port->string i)))
]
