zordius / lightncandy

An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ),
https://zordius.github.io/HandlebarsCookbook/
MIT License
610 stars 76 forks source link

Possible to access parsed variables? #345

Open jdlrobson opened 4 years ago

jdlrobson commented 4 years ago

This is possibly a feature request but hopefully it's already possible.

Given a template

<div>
  <p>{{#x}}{{value}}{{/x}}</p>
  <p>{{msg-foo}}</p>
</div>

Is there a method that can parse the template to predict the keys it uses?

E.g. in this case such a function would return.

['msg-foo', 'x']

My use cases are 1) throw an error when certain variables are passed to the template renderer e.g. if i dont pass a key foo to the template it should throw an error 2) i want to programmatically define template variables. For example any template key prefixed with msg- would be substituted beforehand e.g.

$templateData =[];
foreach($keysPrefixedWithMsg as $key){
$templateData[$key] = generateDataValue($key):
}

Let me know if I am not explaining well!

Thanks in advance!