开发者

Cheetah #include doesn't place #def within scope

When I include a开发者_运维问答 file using #include (without raw) it parses the content correctly, but the #def which I want to access within the original file doesn't exist & an error is thrown.

Base Template:

#def sayHello($name)
hello $name
#end def

Template:

#include "../../BaseTemplate.template"
$sayHello("fred")


The reason this is happening is that sayHello is a function added to "Base Template" (which is actually a class), but you can't access that in the calling template.

I'm not sure the best way to solve the problem you're having, but I believe you can make defHello a static method with #@staticmethod before the def line, and then import the template instead of including it.

This page has more information.


Well, looks like you can do this through the attribute:

self._CHEETAH__cheetahIncludes

Which is a dictionary that contains all of your includes.

Use it from your template after the include like:

 $self._CHEETAH__cheetahIncludes[include_id].the_function(*args)

For me the include_id happened to be the file path (I found this out by printing out the _CHEETAH__cheetahIncludes dictionary first, heh). Is this guaranteed? No idea.

Do I recommend this? No. Is there a better way to access the function? Appears to not be. Are there any shortcomings? No idea, use at your own risk. Could this be useful to someone? Maybe :)

I mean it could be a useful feature because people are trying to do it, but is this feature necessary? Probably not.

My use case was I wanted to reuse a snippet that would access the parents searchList as includes do, but I wanted to reuse this include with a single hard coded argument in other templates. I could easily just make this variable that changes the behavior part of the searchList in the first place, and that is probably what you should do as well :). This would also allow me to pass it in from the application layer instead of hard coding it in my template!

This is most likely good design because it prevents the shorts cuts us developers will often take if allowed to do so easily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜