zopefoundation / Zope

Zope is an open-source web application server.
https://zope.readthedocs.io
Other
352 stars 99 forks source link

Chameleon short-hand text insertion `${structure: ...}` throws exception #1077

Closed petschki closed 6 months ago

petschki commented 1 year ago

PROBLEM REPORT

What I did:

Instead of usually place unescaped HTML inside templates like

<span tal:replace="structure view/some_formatted_html" />

I tried the Chameleon"short-hand text insertion" for unescaped HTML like documented here https://chameleon.readthedocs.io/en/latest/

<p>${structure:view/some_formatted_html}</p>

What I expect to happen:

The formatted HTML string returned from the view should be placed unescaped inside the template.

What actually happened:

A traceback:

Traceback (innermost last):
  Module ZPublisher.WSGIPublisher, line 167, in transaction_pubevents
  Module ZPublisher.WSGIPublisher, line 376, in publish_module
  Module ZPublisher.WSGIPublisher, line 271, in publish
  Module ZPublisher.mapply, line 85, in mapply
  Module ZPublisher.WSGIPublisher, line 68, in call_object
  Module zope.browserpage.simpleviewclass, line 41, in __call__
  Module Products.Five.browser.pagetemplatefile, line 126, in __call__
  Module Products.Five.browser.pagetemplatefile, line 58, in __call__
  Module zope.pagetemplate.pagetemplate, line 129, in pt_render
   - Warning: Compilation failed
   - Warning: builtins.LookupError: Unknown expression type: 'structure'.
zope.pagetemplate.pagetemplate.PTRuntimeError: ['Compilation failed', "builtins.LookupError: Unknown expression type: 'structure'."]

What version of Python and Zope/Addons I am using:

Python: 3.10.7 Zope: 5.7 Plone: 6.0.0rc2 MacOS: Ventura 13.0.1

d-maurer commented 1 year ago

Peter Mathis wrote at 2022-12-7 06:46 -0800:

... Instead of usually place unescaped HTML inside templates like

I tried the Chameleon"short-hand text insertion" for unescaped HTML like documented here https://chameleon.readthedocs.io/en/latest/

<p>${structure:view/some_formatted_html}</p>

What I expect to happen:

The formatted HTML string returned from the view should be placed unescaped inside the template.

By default, Zope's PageTemplates use zope.tales not chameleon.tales as TALES expression engine. zope.tales does not know about the "new types" of chameleon.tales (described by "https://chameleon.readthedocs.io/en/latest/reference.html#new-features"), among them the structure: type.

You can define templates using chamelon.tales by overriding their pt_getEngine to return a TALES engine defined in Products.PageTemplates.expression.

Note that templates using different TALES engines are not fully compatible: You cannot use a macro defined by a template with a different TALES engine.

If those "new types" are really important, they might get supported by zope.tales.

dataflake commented 1 year ago

Moved the issue because it is effectively a feature request for zope.tales

icemac commented 1 year ago

Is there really a structure type in chameleon? @petschki What happens when using <p>${structure view/some_formatted_html}</p> (without the colon)? (Or maybe <p>${structure view.some_formatted_html()}</p> as chameleon defaults to the python type.)

d-maurer commented 1 year ago

Moved the issue because it is effectively a feature request for zope.tales

Sorry @dataflake I have been wrong: zope.tales cannot implement the structure "type" (not without a strong chameleon dependency).

structure is not a "genuine" type. Typical types control the computation of the value; in contrast, structure does not change the value computation but instead tags it with a command to the template engine not to quote the value when it is incorporated into the template. chameleon has put structure which logically belongs to template world into the expression world. In order for zope.tales to implement this, it must know chameleon implementation details (specifically how to wrap the value to tell the chameleon template engine not to quote).

If this should be implemented, the Zope/chameleon integration point (i.e. Products.PageTemplates, part of Zope) is the correct place (as it depends both on zope.tales and chameleon). Therefore, I will move the issue back. Sorry again!

d-maurer commented 1 year ago

Is there really a structure type in chameleon?

@icemac It is, see e.g. "https://chameleon.readthedocs.io/en/latest/reference.html#types".

The list of "types" contains further elements currently not supported by the Zope integration, e.g. import (supported in Zope by the predefined variable modules) and load.