我在Handlebars中添加了一个自定义帮助程序来完成if == "some string"
类型的帮助程序。帮助代码是这样的:
Handlebars.registerHelper('if_eq', function(a, b, opts) {
if(a == b) // Or === depending on your needs
return opts.fn(this);
else
return opts.inverse(this);
});
模板是这样的:
<div id="appStoreParametersModal" class="modal-dialog">
<div class="modal-content appStoreParametersModal">
<div class="modal-header hypersignModalHeader">
<h3>{{appName}}</h3>
</div>
<div class="modal-body">
<div id="app-params">
{{#each appParm}}
<form>
{{#if_eq uiControlType "text"}}
<div class="form-group">
<label for="{{qsName}}">{{uiName}}</label>
<input type="text" class="form-control" id="{{qsName}}" placeholder="{{uiName}}"/>
</div>
{{else if_eq uiControlType "dropdown"}}
<div class="form-group">
<label for="{{qsName}}">{{uiName}}</label>
<select class="form-control" id="{{qsName}}">
{{#each defaultVals}}
<option value="{{value}}">{{displayName}}</option>
{{/each}}
</select>
</div>
{{/if_eq}}
</form>
{{/each}}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-warning cancel" data-dismiss="modal" id="cancel">Cancel</button>
<button type="button" class="btn btn-success" id="appStoreNext">Next</button>
</div>
</div>
</div>
我收到了这个错误:
未捕获错误:if_eq与每个
不匹配
使用{{else}}
似乎有问题,因为如果我只使用if_eq
帮助程序而没有else
,那么它可以正常工作。我对Handlebars很新,所以我确定我错过了一些愚蠢的东西。
答案 0 :(得分:6)
if_eq doesn't match if
{{#if_eq stuff "stuff"}}
,但{{/if}}
而非{{/if_eq}}
错误地结束,则会引发<{import __builtin__
import collections
import contextlib
import sys
@contextlib.contextmanager
def replace_import_hook(new_import_hook):
original_import = __builtin__.__import__
__builtin__.__import__ = new_import_hook
yield original_import
__builtin__.__import__ = original_import
def clone_modules(patches, additional_module_names=None):
"""Import new instances of a set of modules with some objects replaced.
Arguments:
patches - a dictionary mapping `full.module.name.symbol` to the new object.
additional_module_names - a list of the additional modules you want new instances of, without
replacing any objects in them.
Returns:
A dictionary mapping module names to the new patched module instances.
"""
def import_hook(module_name, *args):
result = original_import(module_name, *args)
if module_name not in old_modules or module_name in new_modules:
return result
# The semantics for the return value of __import__() are a bit weird, so we need some logic
# to determine the actual imported module object.
if len(args) >= 3 and args[2]:
module = result
else:
module = reduce(getattr, module_name.split('.')[1:], result)
for symbol, obj in patches_by_module[module_name].items():
setattr(module, symbol, obj)
new_modules[module_name] = module
return result
# Group patches by module name
patches_by_module = collections.defaultdict(dict)
for dotted_name, obj in patches.items():
module_name, symbol = dotted_name.rsplit('.', 1) # Only allows patching top-level objects
patches_by_module[module_name][symbol] = obj
try:
# Remove the old module instances from sys.modules and store them in old_modules
all_module_names = list(patches_by_module)
if additional_module_names is not None:
all_module_names.extend(additional_module_names)
old_modules = {}
for name in all_module_names:
old_modules[name] = sys.modules.pop(name)
# Re-import modules to create new patched versions
with replace_import_hook(import_hook) as original_import:
new_modules = {}
for module_name in all_module_names:
import_hook(module_name)
finally:
sys.modules.update(old_modules)
return new_modules
。