Skip to content

Commit

Permalink
Merge pull request #689 from kethinov/0.6.11
Browse files Browse the repository at this point in the history
0.6.11
  • Loading branch information
kethinov authored Sep 16, 2024
2 parents 9fccd79 + b2b2fb2 commit b093459
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ node_modules
coverage
dist
test-results
test/client.*
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- Put your changes here...

## 0.6.11

- Added new setting `teddy.setEmptyVarBehavior('hide')` that will make it possible for variables which don't resolve to display as empty strings instead of displaying the variable.

## 0.6.10

- Added support for template literal `${templateLiteral}` variables.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ API documentation
- `teddy.maxPasses(n)`: Sets the maximum number of passes the parser can execute over your template. If this maximum is exceeded, Teddy will stop attempting to render the template. The limit exists to prevent the possibility of teddy producing infinite loops due to improperly coded templates.
- Default: 1000.
- `teddy.setEmptyVarBehavior('hide')`: Will make it possible for variables which don't resolve to display as empty strings instead of displaying the variable.
- Default: 'display'.

- `teddy.setCache(params)`: Declare a template-level cache.

- Params:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "https://github.com/rooseveltframework/teddy/graphs/contributors"
}
],
"version": "0.6.10",
"version": "0.6.11",
"files": [
"dist"
],
Expand Down
17 changes: 14 additions & 3 deletions teddy.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ function parseVars (templateString, model) {
// no parse flag is set; also handles if no escape flag is set as well
const originalMatch = match
match = match.substring(0, match.length - (lastFourChars.split('|').length - 1 > 1 ? 4 : 2)) // remove last 2-4 char
const parsed = getOrSetObjectByDotNotation(model, match)
let parsed = getOrSetObjectByDotNotation(model, match)
if (params.emptyVarBehavior === 'hide' && !parsed) parsed = '' // display empty string instead of the variable text verbatim if this setting is set
if (parsed || parsed === '') {
const id = model._noTeddyBlocks.push(parsed) - 1
try {
Expand All @@ -629,7 +630,8 @@ function parseVars (templateString, model) {
const originalMatch = match
match = match.substring(0, match.length - (lastFourChars.split('|').length - 1 > 1 ? 4 : 2)) // remove last 2-4 char
let parsed = getOrSetObjectByDotNotation(model, match)
if (!parsed && parsed !== '') parsed = `{${originalMatch}}`
if (params.emptyVarBehavior === 'hide' && !parsed) parsed = '' // display empty string instead of the variable text verbatim if this setting is set
else if (!parsed && parsed !== '') parsed = `{${originalMatch}}`
try {
templateString = templateString.replace(new RegExp(`\${${originalMatch}}`.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d'), 'i'), () => parsed)
templateString = templateString.replace(new RegExp(`{${originalMatch}}`.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d'), 'i'), () => parsed)
Expand All @@ -639,7 +641,8 @@ function parseVars (templateString, model) {
} else {
// no flags are set
let parsed = getOrSetObjectByDotNotation(model, match)
if (parsed || parsed === '') parsed = htmlEntities.encode(parsed)
if (params.emptyVarBehavior === 'hide' && !parsed) parsed = '' // display empty string instead of the variable text verbatim if this setting is set
else if (parsed || parsed === '') parsed = htmlEntities.encode(parsed)
else if (parsed === 0) parsed = '0'
else parsed = `{${match}}`
try {
Expand Down Expand Up @@ -739,6 +742,7 @@ function setDefaultParams () {
params.verbosity = 1
params.templateRoot = './'
params.maxPasses = 1000
params.emptyVarBehavior = 'display' // or 'hide'
}

// mutator method to set verbosity param. takes human-readable string argument and converts it to an integer for more efficient checks against the setting
Expand Down Expand Up @@ -772,6 +776,12 @@ function setMaxPasses (v) {
params.maxPasses = Number(v)
}

// mutator method to set empty var behavior param: whether to display {variables} that don't resolve as text ('display') or as an empty string ('hide')
function setEmptyVarBehavior (v) {
if (v === 'hide') params.emptyVarBehavior = 'hide'
else params.emptyVarBehavior = 'display'
}

// access templates
function getTemplates () {
return templates
Expand Down Expand Up @@ -996,6 +1006,7 @@ export default {
setVerbosity,
setTemplateRoot,
setMaxPasses,
setEmptyVarBehavior,
getTemplates,
setTemplate,
setCache,
Expand Down
15 changes: 13 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,17 @@ export default [
test: (teddy, template, model) => teddy.render(template, model),
expected: '<p>Some content</p>'
},
{
message: 'should render {variables} as blank when x is true (misc/undefinedVar.html)',
template: 'misc/undefinedVar',
test: (teddy, template, model) => {
teddy.setEmptyVarBehavior('hide')
const result = teddy.render(template, model)
teddy.setEmptyVarBehavior('display')
return result
},
expected: '<p></p><p></p>'
},
{
message: 'should render template literal ${variables} (misc/variableTemplateLiteral.html)', // eslint-disable-line
template: 'misc/variableTemplateLiteral',
Expand Down Expand Up @@ -1640,7 +1651,7 @@ export default [
fs.writeFileSync('test/client.cjs', 'const teddy = require("../dist/teddy.cjs")\nconsole.log(teddy)')
const output = execSync('node ./test/client.cjs', { encoding: 'utf-8' }).toString()

assert(output.includes('params: { verbosity:'))
assert(output.includes('emptyVarBehavior:'))

fs.rmSync('test/client.cjs')
},
Expand All @@ -1655,7 +1666,7 @@ export default [
fs.writeFileSync('test/client.js', 'import teddy from "../dist/teddy.js"\nconsole.log(teddy)')
const output = execSync('node ./test/client.js', { encoding: 'utf-8' }).toString()

assert(output.includes('params: { verbosity:'))
assert(output.includes('emptyVarBehavior:'))

fs.rmSync('test/client.js')
},
Expand Down

0 comments on commit b093459

Please sign in to comment.