Reference data as obj.loggedIn with default settings.
04
RegExp rules
Capture group, non-greedy match, and global flag.
05
Per-template override
Pass custom evaluate in the options object.
06
Security
Compile trusted template source only—logic tags run JavaScript.
Fundamentals
What Is _.templateSettings.evaluate?
_.templateSettings.evaluate is a RegExp that marks sections of template source containing JavaScript logic—if/else branches, loops, and other statements that control what gets rendered. Unlike interpolate tags, evaluate blocks do not automatically insert their return value into the output string.
💡
Beginner tip
Think of evaluate as “run this JavaScript inside the template.” Pair it with interpolate tags to print values inside branches: open a logic block, write static text or an interpolate tag, then close the block.
The default delimiter family matches ERB logic tags (open-percent without equals). You customize evaluate when you need different syntax while keeping interpolate on Mustache braces or another style.
Foundation
📝 Syntax
Assign a RegExp to the global settings object (or pass it per compile):
Pattern matching logic and control-flow tags. Captured JavaScript is emitted into the compiled render function body.
/<%([\s\S]+?)%>/g
capture group 1Statements
JavaScript inside delimiters—e.g. if (obj.ok) {, obj.items.forEach(function (item) {, }.
if (obj.premium) {
with interpolatePair tags
Logic blocks wrap static text and interpolate tags that produce visible output between evaluate segments.
... <%= obj.name %> ...
per-compile overrideOptional
Pass { evaluate: /.../g } to _.template() without changing global settings.
_.template(str, { evaluate })
All three delimiter RegExps—evaluate, interpolate, and escape—are scanned when Lodash parses template source. Keep their syntax distinct so tags are not ambiguous.
Hands-On
Examples Gallery
Practical evaluate patterns with copy-ready code, sample output, and interactive Try It Yourself labs.
📚 Getting Started
Conditional greeting with default evaluate delimiters.
Example 1 — if/else welcome message
Branch on obj.loggedIn and print the username with an interpolate tag inside the true branch.
Most projects leave evaluate on the default ERB logic tags and only customize interpolate or escape. Change evaluate when your template grammar requires distinct logic delimiters.
Compare
📋 evaluate vs related settings
Setting
Default tag style
Runs JS?
Prints output?
evaluate
Logic (open-percent, no equals)
Yes
No (by default)
interpolate
Value (open-percent-equals)
Yes
Yes
escape
Escape (open-percent-minus-equals)
Yes
Yes (escaped)
variable
N/A (string name)
N/A
Names data param
imports
N/A (object)
Helpers in scope
Used inside tags
🧠 How evaluate Works
1
Load evaluate RegExp
_.template() reads evaluate from global settings or per-call options.
Config
2
Parse template segments
Lodash splits source by evaluate, interpolate, and escape patterns into static strings and code chunks.
Parse
3
Embed logic in render fn
Captured evaluate code becomes statements in the compiled function—real if/else and loops at runtime.
Compile
=
⚙️
Control output shape
Logic decides which static text and interpolate segments run; the final string reflects those branches.
Important
📝 Notes
evaluate must be a RegExp, not a function.
Logic tags do not print values automatically—use interpolate for output.
Reference data as obj.field unless you renamed variable.
Changes apply at compile time; recompile after editing the pattern.
Never compile template source from untrusted users—evaluate blocks execute JavaScript.
Keep evaluate, interpolate, and escape delimiters visually distinct to avoid parse ambiguity.
Wrap Up
Conclusion
_.templateSettings.evaluate defines which delimiters wrap logic and control flow in Lodash templates. Use it for if/else, loops, and branching while interpolate handles the values users see.
Most teams keep the default ERB logic tags, customize interpolate when needed, and move on to imports for helpers like _.forEach.
Keep logic in evaluate blocks and values in interpolate tags
Use obj. prefix consistently for render data
Document delimiter choices alongside interpolate and escape settings
Prefer precomputing complex data in JavaScript when templates grow large
Recompile templates after changing the evaluate RegExp
❌ Don’t
Assign a function to templateSettings.evaluate
Expect evaluate blocks to print values without interpolate tags
Use bare property names like loggedIn when variable is obj
Compile untrusted template strings from user input
Overlap evaluate and interpolate delimiters so tags become ambiguous
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about evaluate
Use these points when adding logic to Lodash templates.
5
Core concepts
⚙️01
RegExp
Logic delimiter pattern.
Basics
📝02
No auto print
Use interpolate.
Key rule
🔄03
if / loops
Control flow in tags.
Pattern
📦04
obj.*
Default data prefix.
Data
🔒05
Trusted source
Logic runs as JS.
Security
❓ Frequently Asked Questions
It is a RegExp that tells _.template() which tags contain JavaScript logic to execute without directly printing output. The default pattern matches ERB-style logic tags (open-percent, no equals sign).
A RegExp. Lodash uses it at compile time to find control-flow blocks. Do not assign a callback function to evaluate.
interpolate tags evaluate an expression and insert the result into the output string. evaluate tags run JavaScript for side effects—conditionals, loops, variable setup—without automatically printing a value.
By default Lodash uses a RegExp matching open-percent ... close-percent tags (the form used for if blocks and loops, without the equals sign used by interpolate).
With the default variable setting (obj), yes—write obj.loggedIn and obj.users inside evaluate blocks, not bare loggedIn.
At compile time. Change templateSettings.evaluate before calling _.template(). Already-compiled render functions keep the pattern from when they were built.
Did you know?
Lodash templates borrow ERB’s three-tag model from Ruby on Rails: logic tags (evaluate), value tags (interpolate), and escape tags (escape). That split keeps control flow separate from what gets printed.