Patchouli

Patchouli

168M Downloads

Feature request: string interpolation in custom template

3TUSK opened this issue ยท 0 comments

commented

Synopsis

The text component used in custom template needs the functionality of string interpolation (things like Hello world from ${name}).

Description

String interpolation or string template is a functionality featured in several programming languages which allows one to incorporate variables or expressions into a string. That said, with string interpolation, if you have the following pseudocode:

var person = "anonymous user"
var greeting = "Hello, ${person}"

, the value of greeting would be evaluate to Hello, anonymous user.

In the context of a Patchouli book, it may look like this inside a text component used in a template, notice the variable custom_text is enclosed in a pair of #:

[
  {
    "type": "text",
    "x": 0,
    "y": 0,
    "text": "This is example text. The following is from another variable. #custom_text#"
  }
]

, and if I have a page that uses such template:

{
  "type": "demo_template",
  "custom_text": "Hi I am from another variable"
}

The string to be displayed will be This is example text. The following is from another variable. Hi I am from another variable instead.

Rationale

Although it may be possible to write a custom template in code to achieve similar effect, such a functionality may be proven useful for a modpack creator who does not have skills in making a new mod. One potential use case would be creating a page that introduces ore distribution in a modpack:

{
  "components": [
    {
      "type": "image",
      "image": "...",
      "u": 5,
      "v": 5,
      "width": 100,
      "height": 100
    },
    {
      "type": "text",
      "text": "#ore_name# generates between y=#min_y# and y=#max_y#."
    }
  ]
}

Instead of duplicating the same sentence in all entries, a modpack creator may wish to create such a template and just simply write:

{
  "type": "ore_info",
  "ore_name": "Thonkinite",
  "min_y": 1,
  "max_y": 63
}

, and the manual will automatically populate the string to Thonkinite generates between y=1 and y=63. and display that.