何时对我的 tal:condition 使用 nocall?

发布于 2024-10-28 05:51:06 字数 186 浏览 6 评论 0原文

我知道,为了提高性能,最好在 上使用 nocall 以避免调用对象。希望(链接到)一些背景知识,因为这对我来说听起来有点模糊:-)

那么你什么时候使用 nocall ?把它放在我所有的条件下会受伤吗?

谢谢 !

I know that for performance it's good practice to use nocall on a <tal:condition> in order to avoid calling an object. Would appreciate (links to) a bit of background as this sounds a little vague to me :-)

So when do you use nocall? Can it hurt to put it on all my conditions?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

影子的影子 2024-11-04 05:51:06

我倾向于使用 tal:condition="python:variable" 代替。这样我就可以随时编写正常的、正确的 Python 表达式,而不必担心默认路径表达式的神奇行为。

路径表达式将执行许多操作,例如,如果表达式中的变量可调用,则调用该变量。您经常会在 TAL 中处理工具或内容项,它们都是可调用的。

最常见的错误是使用 tal:condition="content_object"。内容对象可能来自多个 API,例如调用任何类型的引用字段都将返回内容对象。目录搜索将返回“brains”,但在列表中您通常需要访问这些属性的更多属性,因此您有一个 tal:define="obj Brain/getObject"。

调用内容对象会导致该对象被呈现,就像浏览器请求它一样。由于渲染页面通常需要 500 毫秒到 2 秒,因此您可以使渲染页面的速度减慢该时间量。如果您在超过 25 个项目的循环中执行此操作,我预计页面需要 30 秒或更长时间才能呈现。

I tend to use tal:condition="python: variable" instead. That way I can always write normal proper Python expressions, without having to fear magic behavior from the default path expressions.

Path expressions will do a number of things, for example call the variable in the expression if it is callable. Often you deal with tools or content items in TAL, which are all callable.

The most common mistake is to use a tal:condition="content_object". The content object might come from a number of API's, for example calling any kind of reference field will return content objects. Catalog searches will return "brains" but in listings you often need to access more attributes of these, so you have a tal:define="obj brain/getObject".

Calling a content object causes the object to be rendered as if a browser would have requested it. As rendering pages usually takes between 500ms and 2 seconds, you make rendering your page slower by that amount of time. If you do this in a loop over 25 items, I'd expect the page to take 30 seconds or more to render.

梅窗月明清似水 2024-11-04 05:51:06

nocall 让您获得对象属性或方法的“处理程序”。
如果您想知道该对象是否具有该属性或方法,您应该使用:

<div tal:condition="nocall:context/method|nothing">
  ...
</div>

|nothing 的工作方式类似于 python 代码中的 except 块: if context/方法失败(未定义),不返回任何内容。 (这可能不完全是真正的解释,但工作原理是这样的)。

使用 nocall 的另一个原因是获取您知道已定义且稍后将使用的方法的处理程序:

<div tal:define="method nocall:context/method">
    <span tal:content="python:method(3)" />
    <span tal:content="python:method('hello')" />
    <span tal:content="python:method('whatever')" />
</div>

nocall lets you get a "handler" to an object's attribute or method.
If you want to know if the object has that attribute or method you should use:

<div tal:condition="nocall:context/method|nothing">
  ...
</div>

The |nothing works similar as an except block in python code: if context/method fails (is undefined), return nothing. (This might not be exactly the real explanation but works like this).

Another reason to use nocall is to get a handler of method that you know is defined and you'll use later:

<div tal:define="method nocall:context/method">
    <span tal:content="python:method(3)" />
    <span tal:content="python:method('hello')" />
    <span tal:content="python:method('whatever')" />
</div>
乖乖兔^ω^ 2024-11-04 05:51:06

我假设您只会将 nocall: 添加到已经对不可调用的项目进行测试的条件中,并且可能避免 python 内置 callable 测试可能会给你带来性能提升。

这个问题的简短答案是否定的,这对你没有帮助。在我的 Macbook Pro 笔记本电脑上,运行 callable(True) 1000 次,每个循环的时钟时间为 119 纳秒,而普通 True 语句的每个循环时间为 71 纳秒。因此,对于简单的 Python 对象,callable 测试只需 48 纳秒。另一方面,将 nocall: 添加到 TALES 语句需要额外的处理,几乎肯定会超过您刚刚保存的 callable 测试的 48ns 开销。

因此,为了提高性能而添加 nocall: 会适得其反。您最好实施适当的缓存(查看 plone.app.caching与 Varnish 结合使用),或者看看 Chameleon 是否适合您的用例。

I'm presuming that you would only add nocall: to conditions that already test on items that are not callable in the first place, and that perhaps avoiding the python builtin callable test might be give you a performance boost.

The short answer to that question is no, that would not help you. On my Macbook Pro laptop, running callable(True) a 1000 times clocks in at 119ns per loop, vs. 71ns per loop for the plain True statement. So for simple python objects, the callable test takes a mere 48ns. Adding the nocall: to a TALES statement on the other hand, requires extra processing that almost certainly will exceed the 48ns overhead of the callable test you just saved.

Thus, adding nocall: for the sake of improving performance would backfire. You'd be better off implementing proper caching (look at plone.app.caching in combination with Varnish), or take a look if Chameleon could work for your use-case.

天暗了我发光 2024-11-04 05:51:06

不要把它放在你所有的条件上。它会受伤!尤其是那些必须遵循您的代码的人:-)

Don't put it on all your conditions. It can hurt! Especially the people who have to follow your code :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文