Template String Renderer
Building a message like "Trade #123 on EURUSD: profit 128.46, size 0.50 lots" by concatenation means interleaving literal text and Print()-style arguments in one long expression - reorder two fields, forget a space, or change a number's precision, and the mistake is easy to make and easy to miss in review. A template keeps the literal text and the variable positions visually separate: "Trade #{ticket:d} on {symbol}: profit {profit:.2f}, size {lots:.1f} lots". MQL4 has no variant type and no reflection, so the rendering context is two parallel stores - one for string variables, one for numeric ones - rather than a single dictionary of "any" value. SetString() and SetNumber() register values under a name; Render() walks the template once and resolves each {name} or {name:spec} against whichever store has that key. Format specifiers are a small, deliberate subset: ".Nf" for N fixed decimal places (".2f" -> 2 decimals), "d" for a rounded integer, and no specifier at all for a default 4-decimal rendering. This covers the overwhelming majority of trading-report formatting needs without attempting to reimplement printf's full specifier grammar. An unresolved placeholder - a name that was never registered - is left in the output exactly as written, "{like_this}", rather than silently becoming an empty string. A blank gap in a rendered alert is easy to miss; the literal, unresolved placeholder text is not, and immediately shows which variable was forgotten. Literal brace characters are supported via "{{" and "}}" escapes for templates that need to print braces themselves. Notes and limitations - Format specifiers only apply to numeric variables; a string variable used with a specifier ({symbol:.2f}) simply ignores the specifier and renders the string as-is, since ".2f" has no meaning for text. - An unresolved placeholder is kept verbatim in the output rather than raising an error or logging a warning - check the rendered text yourself if silent typos in placeholder names are a concern for a given template. - An unrecognised format specifier falls back to the default 4-decimal rendering rather than failing the whole render. - Nested or nested-looking nesting ("{{outer{inner}outer}}") is not a supported feature - "{{" and "}}" are literal-brace escapes only, not a nesting mechanism, and are resolved independently of any placeholder matching that happens to be adjacent to them.
Script · MT5 · v1.0 · Free
About Template String Renderer
Template String Renderer is a script for MetaTrader 5. MQL source code included.
Files in the download: TemplateStringRenderer.mq5.mqh, TemplateStringRenderer.txt, TemplateStringRendererDemo.ex5, TemplateStringRendererDemo.mq5.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Alerts · EURUSD · MT5 Script · MQL5 Source Code