Page 1 of 1

Unit statsheet - HTML.Template

Posted: Wed Aug 04, 2021 8:21 pm
by Stratego (dev)
Allunit statsheets are using a templating engine HTML.Template.
Using this The code just give "data", and there is a HTML template file to be filled in with the data.
and this engine generates a HTML file that can be viewed as a webpage.
the in-game help shows this generated HTML.

The HELP

i have found a help for a perl templaing engine having the same synthax(or is it the same?), here it is:
https://metacpan.org/pod/HTML::Template
the real one:
http://html-tmpl-java.sourceforge.net/



LOOPS:

in template: looks like this (see the item "tmpl_loop"):

Code: Select all

  <tmpl_loop up_spells_details>
        <tr onClick="toggleMe('<tmpl_var name="up_spell_id">')">
          <td class="txt_spell_bonus"><img width="32" src="<tmpl_var up_spell_img>"  /> </td>
 <td class="txt_spell_bonus"><tmpl_var up_spell_name></td>
          <td class="txt_spell_bonus"><tmpl_var up_spell_chance></td>
          <td class="txt_spell_bonus"><tmpl_var up_spell_range></td>
          <td class="txt_spell_bonus"><tmpl_var up_spell_timeout></td>
          <td class="txt_spell_bonus"><tmpl_var up_spell_cooldown></td>
        </tr>
        <tr class='collapsible_row'  id="<tmpl_var name="up_spell_id">">
<td colspan="6" width="100%" class="txt_spell_bonus" >
<p>
<tmpl_var name="up_spell_desc">
</p>
<p>
<!-- <tmpl_var name="up_spell_action"> -->
Tranlsated Name of unit effect (ActionUnitID)
</p>
</td>
</tr>
</tmpl_loop>
in code: looks like this little method:
(fills up to template everything needed for the "loop")
i try to make Yellow the interesting parts:
in nutshell:
you create a vector of hashtables,
and in a loop you fill the vector adding one hashtable with key-value pairs for each row,
finally adding the vector to templating engine with name up_spell_details in this example.
not so complex, this little method does everything.
private static void appendSpells(Unit u, String imgLocationPrefix, Template t) {

Code: Select all

    //up_spells_details: abilites detailed list

    //fetching the spells he can cast
    Unit sample = UnitSamples.samples.get(u.type);

    if (sample.weaponEffectOptions != null && sample.weaponEffectOptions.size() > 0) {
        Vector<Hashtable<String, String>> v = new Vector<Hashtable<String, String>>();

        int[] weaponOptions = ZTSPacket.arrayMergeSafe(
                sample.weaponEffectOptions.get(Unit.WEAPON_EFFECT_SLOT_SPELLCAST),
                sample.weaponEffectOptions.get(Unit.WEAPON_EFFECT_SLOT_WP_1)
        );


        for (int wo : weaponOptions) {

            Hashtable<String, String> h = new Hashtable<String, String>();

            EffectDef ef = Const.effectDefs.get(wo);

            h.put("up_spell_img", imgLocationPrefix + ef.getImageNameOfEffect().imageName);
            h.put("up_spell_id", ef.effectNameString);
            h.put("up_spell_name", ef.getName());

            //getting the descriptive text if any
            String descr = "(no description)";
            int descID = Unit.getDescrtiptiveTXTid(ef.effectNameString);
            if (descID != 0) {
                descr = ZTSApplication.getContext().getString(descID);
            }
            h.put("up_spell_desc", descr);


            h.put("up_spell_chance", Math.round(ef.chancePercent * 100) + "%");
            h.put("up_spell_range", String.valueOf(ef.castRangeModifier + u.convertRange));

            if (ef.hasTimeout() && ef.lastsTurnNr > 0)
                h.put("up_spell_timeout", String.valueOf(ef.lastsTurnNr));
            else
                h.put("up_spell_timeout", "Instant");

            if (ef.hasCooldown())
                h.put("up_spell_cooldown", String.valueOf(ef.cooldownTurnNr));
            else
                h.put("up_spell_cooldown", "-");




            v.addElement(h);
        }
        t.setParam("up_spells_details", v);

    }//spells
}

Re: Unit statsheet - HTML.Template

Posted: Thu Aug 05, 2021 7:27 pm
by Endru1241
Isn't this one:
http://html-tmpl-java.sourceforge.net/
the one used?

And few comments to first post:
  • <tmpl_if> tag DOESN'T ALLOW ANY CONDITION - only variable name is put there (e.g. <tmpl_if up_aff_effects>)
    Engine only puts whatever html is between <tmpl_if var_name> and end tag </tmpl_if> or <tmpl_else> in case variable is not equal to null, false, 0, "", empty array and puts whatever's between <tmpl_else> and </tmpl_if> otherwise.
    So it's just unchangeable condition if (var_name and var_name!="0" and var_name!="")
  • tmpl_propsheet.html is not valid html, so any online checkers won't work. Most browsers could display it but of course it would look weird.
    Unfortunately code formatting (like in notepad++) is not reading it well too, so no collapsing levels.
    To make some tests outside game modder would have to replace some (or all) template tags with real data.
  • apart of that limitations - it is normal html and uses css classes listed under <styles> tag (which should be used for changing appearance) and javascript (that's how we work magic like collapsible rows for additional info, selecting unit images for name hint