To render SMS templates safely, use your template engine's strict mode so a missing variable raises an error, render the text, check that no braces are left over, and count segments before you send. A broken email is embarrassing. A broken text that says "Hi {{first_name}}" goes to a phone with a bill attached.

We make Ready, and these checks sit in front of any send, so they apply to any provider. Our post on template variables and segment drift explains why a long name can double your segment count, and this one is about the code.

Make missing variables an error

Every engine defaults to rendering a missing variable as an empty string. For SMS that's the worst choice, because "Hi , your code is" still sends.

from jinja2 import Environment, StrictUndefined

env = Environment(undefined=StrictUndefined, autoescape=False)
tpl = env.from_string("Hi {{ first_name }}, your table is ready at {{ time }}.")
text = tpl.render(first_name="Dana", time="7:30 PM")

StrictUndefined raises when first_name is missing. Turn autoescape off, since HTML escaping turns an apostrophe into ' and that lands in the text.

import Handlebars from 'handlebars';

const tpl = Handlebars.compile('Hi {{first_name}}, your table is ready at {{time}}.', { strict: true, noEscape: true });
const text = tpl({ first_name: 'Dana', time: '7:30 PM' });

strict: true throws on a missing field, and noEscape: true stops HTML escaping.

require 'liquid'

tpl = Liquid::Template.parse('Hi {{ first_name }}, your table is ready at {{ time }}.', error_mode: :strict)
text = tpl.render({ 'first_name' => 'Dana', 'time' => '7:30 PM' }, strict_variables: true)
raise 'template errors' if tpl.errors.any?

Liquid keeps going after an error unless you check errors, so read it.

Check the rendered text

Even in strict mode, a value can be an empty string, or contain braces from user input. Run a short check after rendering:

CheckWhy
No {{, }}, {% or # placeholders leftA leftover tag means a render path was skipped
No empty slots, such as "," or "Hi ,"A blank value passed strict mode
Length within your segment budgetA 30-character name can push a text into a second segment
No unsupported characters you didn't expectCurly quotes and emoji change the encoding

Ready swaps curly quotes, long dashes and ellipses for plain ones before sending, so those never change the count. An emoji or other non-GSM character still drops the limit from 160 to 70 characters per segment, and each segment is billed. The GSM-7 encoding guide and the segment counter show how to count them yourself.

Escape user-supplied values

A contact's first name is user input. If it contains a link or a stray {{, you don't want it to alter the message. Treat values as data: don't render a template inside a template, don't allow a template field from a customer to use filters that call code, and cap each variable's length (a name over 20 characters is probably a mistake, so fall back to "there").

Let customers write their own templates

On a multi-tenant platform, your customers will want to edit text. Give them a small variable set, validate templates when they save, not when they send, and reject any variable outside the list. Use a logic-less engine such as Handlebars, or Liquid's safe mode. Never evaluate a customer's template with an engine that can run code. The post on pricing SMS in your SaaS plans covers how preview and segment counts show up in the UI.

FAQ

Which engine is best for SMS?

Any of them work. Pick the one your stack already uses, turn on strict mode and add the post-render check.

Should I store the template or the rendered text?

Both. Store the template version and the rendered text with each message, so you can answer what a customer was sent without re-rendering with changed data.

How do I preview a template?

Render it with sample data, run the same checks, and show the segment count and the first-message footer, since Ready adds "Reply STOP to opt out" and the business name to the first text to each contact unless the account turns it off.

The short version

Strict mode, no HTML escaping, a post-render check and a segment count. The messages API docs cover what you send afterward.

Cost and registration

A segment on the Standard rate is $0.0075 plus a $0.0045 carrier fee, passed through at cost, excluding optional screening. An account can send up to 500 texts a month before it registers for 10DLC, and most approvals take 2 to 7 days. See pricing for the full list.