Skip to main content

StringReplace

Token replacement engine for HTML, JavaScript, and CSS content, supporting variable interpolation, file imports, and iteration.

Usage​

import { StringReplace } from "@io-oidis-localhost/Io/Oidis/Localhost/Utils/StringReplace.js";
import { StringReplaceType } from "@io-oidis-localhost/Io/Oidis/Localhost/Enums/StringReplaceType.js";

// Static API (uses singleton internally)
const output : string = StringReplace.Content(template, StringReplaceType.VARIABLES);

// Instance API
const replacer : StringReplace = StringReplace.getInstance();
const result : string = replacer.ContentFrom("./source/html/index.html", StringReplaceType.IMPORTS_FINISH);

StringReplaceType Enum​

ValueConstantDescription
"variables"StringReplaceType.VARIABLESResolves <? @var path.to.prop ?> and <!-- @var path.to.prop --> tokens from the environment object
"importsPrepare"StringReplaceType.IMPORTS_PREPAREResolves import tags to absolute staged paths without inlining file content
"importsFinish"StringReplaceType.IMPORTS_FINISHResolves import tags and inlines the referenced file content, then applies FOR_EACH and VARIABLES
"forEach"StringReplaceType.FOR_EACHExpands <? @forEach template in collection ?> iteration blocks

Token Syntax​

VARIABLES — supported in .html and .js files:

<title><?  @var project.name ?></title>
<!-- @var build.version -->

IMPORTS_PREPARE / IMPORTS_FINISH — inline another file's content:

<!-- @import source/html/partials/header.html -->
<? @import resource/javascript/app.js ?>
/* @import resource/javascript/bundle.js */

FOR_EACH — repeat a template for each item in a collection:

<?  @forEach templates.row in project.items ?>
<!-- @forEach templates.listItem in build.entries -->

Inside the repeated template, this.value references a scalar item and this.propName references a property of an object item.

API Reference​

getInstance​

Returns the shared singleton instance.

public static getInstance() : StringReplace

Content​

Applies the specified replacement strategy to an in-memory string.

public Content($input : string, $type : StringReplaceType, $varFormatter? : ($value : string) => string) : string
static Content($input : string, $type : StringReplaceType, $varFormatter? : ($value : string) => string) : string
  • $varFormatter — optional transform applied to every resolved variable value before substitution.

ContentFrom​

Reads a file from disk and applies the replacement strategy. Path resolution follows the same rules as File.

public ContentFrom($path : string, $type : StringReplaceType, $varFormatter? : ($value : string) => string) : string
static ContentFrom($path : string, $type : StringReplaceType, $varFormatter? : ($value : string) => string) : string

File​

Reads a file, applies replacements, and writes the result. When $dest is omitted, the source file is overwritten in-place.

public File($path : string, $type : StringReplaceType, $dest? : string) : boolean

setEnvironment​

Replaces the entire environment object used for VARIABLES and FOR_EACH lookups.

public setEnvironment($value : any) : void

Environment Object​

By default the environment is built from EnvironmentArgs and contains:

{
"build": { /* build configuration */ },
"project": { /* target configuration */ },
"serverConfig": { /* full project config */ },
"templates": { /* templates map (empty in base class) */ }
}

Variable paths in tokens use dot notation with optional array index: project.name, build.entries[0].value.


See also: Utilities Overview | StringUtils | Builder Overview