INJA

From AddictHelp
Revision as of 02:52, 15 May 2026 by Brazil (talk | contribs) (Created page with "= Inja Template Engine Integration = This project integrates the [https://github.com/pantor/inja inja] C++ template engine for dynamic text rendering in the MUD. Builders and developers can use inja templates to create flexible, data-driven descriptions and messages. == Features Exposed to Templates == === 1. Variable Substitution === You can use variables from the game context: * <code>player.name</code>, <code>player.level</code>, <code>player.race</code>, etc. *...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Inja Template Engine Integration

This project integrates the inja C++ template engine for dynamic text rendering in the MUD. Builders and developers can use inja templates to create flexible, data-driven descriptions and messages.

Features Exposed to Templates

1. Variable Substitution

You can use variables from the game context:

  • player.name, player.level, player.race, etc.
  • room.name, room.vnum, room.sector, room.light, room.people_count, etc.
  • time.hour, time.day, time.month, time.year, time.period ("morning", "afternoon", "evening", "night")

Example:

Welcome, {{ player.name }}! You are in the {{ room.name }}.

2. Conditionals

Use {% if %}, {% else %}, and {% endif %} for logic:

{% if room.people_count > 0 %}
  The room is occupied.
{% else %}
  The room is empty.
{% endif %}

3. Loops

You can loop over arrays (if provided in context):

{% for item in player.inventory %}
  - {{ item }}
{% endfor %}

4. Random Sentence Selection

Use the custom random_choice callback to randomly select one string from an array:

{{ random_choice(["A cold wind blows.", "You hear distant laughter.", "The torches flicker ominously."]) }}

Each time the template is rendered, one of the options will be chosen at random.

5. Line Breaks

Use the custom br() and br2() callbacks for single and double line breaks:

Line one.{{ br() }}Line two.{{ br2() }}Line three.

6. Text Cleanup and Wrapping

Long lines are automatically wrapped and cleaned up according to game settings.

7. Random Number Generation and Conditional Blocks

You can use the custom number(min, max) callback to generate a random integer between min and max (inclusive). This enables conditional logic in templates, such as randomizing which block of text is shown.

Example:

{% set n = number(0, 100) %}
{% if n < 33 %}
  The shadows seem to move.
{% elseif n < 66 %}
  You hear a distant howl.
{% else %}
  A sudden chill fills the air.
{% endif %}

Each time the template is rendered, a different block may be selected based on the random number.

Available Variables

Player

  • player.name (string)
  • player.level (int)
  • player.race (string)
  • player.str, player.dex, player.wis, player.con, player.int, player.cha (int)
  • player.height, player.weight (int)
  • player.guild (object, e.g. player.guild.Warrior)
    • player.guild is a hash. If you were to display the value you would see all of your guild levels. A level of 0 means you aren't a member of that guild.
    • Check specific guild levels directly: player.guild.MindWarrior.

Example:

{
    "AntiPaladin":0,
    "Cleric":65,
    "Crusader":0,
    "DarkKnight":0,
    "Hero":0,
    "Immortal":30,
    "Invisible":0,
    "Knight":30,
    "MindWarrior":0,
    "NPC":0,
    "Necromancer":0,
    "Ninja":0,
    "Paladin":0,
    "Psionicist":30,
    "Remort":0,
    "Rogue":0,
    "Scrollmaker":0,
    "Sorcerer":0,
    "Thief":1,
    "Warrior":0
}

Room

  • room.name (string)
  • room.vnum (int)
  • room.sector (string)
  • room.light (int)
  • room.people_count (int)
  • room.object_count (int)

Time

  • time.hour, time.day, time.month, time.year (int)
  • time.period (string: "morning", "afternoon", "evening", "night")

Example Template

Welcome to the {{ room.name }}.
{% if room.people_count > 0 %}
  There are others here.
{% else %}
  You are alone.
{% endif %}

{{ random_choice(["A chill runs down your spine.", "You feel watched.", "The silence is deafening."]) }}

Notes

  • All variables are read-only.
  • No variable assignment or random logic is allowed except via the provided callbacks.

For more on inja syntax, see: https://pantor.github.io/inja/