TT模板修改
在加载模板(Template Toolkit)时,在将其缓存为 Perl 代码之前,是否可以轻松修改模板(Template Toolkit)?我想对其运行正则表达式。
Is it possible to easily modify template (Template Toolkit) when it is loaded, before it is cached as Perl code? I want to run a regular expression on it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以提供自己的 Template::Provider 作为标准模板的子类。来自精美手册:
所以,它应该很容易,但是容易,当然,很大程度上取决于你的技能。
You can supply your own Template::Provider that subclasses the standard one. From the fine manual:
So, it should be pretty easy but easy, of course, greatly depends on your skill.
上面的 Template::Provider 建议可能是最好的方法。但还有一种更简单(如果稍微有点hacky)的方法。您可以将模板读入标量,并在将其传递到模板处理器之前对其运行所需的任何转换。
秘密在于 process() 方法将各种类型的值作为其第一个参数。如果您给出标量,则假定该标量是包含模板的文件的名称。但如果它是对标量的引用,则它假定该标量包含实际模板。有关详细信息,请参阅文档细节。
The Template::Provider suggestion above is probably the best way to do it. But there's also a simpler (if slightly hacky) approach. You can read the template into a scalar and run whatever transformations on it that you want before passing it to the template processor.
The secret is that the process() method takes various types of value as its first parameter. If you give if a scalar then that's assumed to be the name of a file that contains the template. But if it's a reference to a scalar, then it assumes that that scalar contains the actual template. See the documentation for more details.