How can I get the result of a template without HTML-Tags?

How can I get the result of a template without HTML-Tags?

When I use the following view SituationView with a plain text template situation.pt I get a result with HTML-Tags.

# view.py
class SituationView(ViewMixinForTemplates, BrowserView):
    index = ViewPageTemplateFile("situation.pt")

    # setting the content_type to 'text/plain' returns html-tags too.
    # index = ViewPageTemplateFile("situation.pt", content_type='text/plain')
# situation.pt
some text
$ curl "http://localhost:8081/Plone/de/situation/@@situation-view"

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><body><p>some text
</p></body></html>

That might be a Diazo thingy.
Could you try to disable theming for the view by setting the X-Theme-Disabled header?

https://docs.plone.org/external/plone.app.theming/docs/index.html#advanced-disable-diazo-transformation-by-setting-the-x-theme-disabled-header

@jaroel: Thanks. This is exactly what I was searching for.

Reading the source code of plone.app.theming.policy.isThemeEnabled() I found two important things:

  • the value of the header X-Theme-Disabled is irrelevant for disabling the theme

  • theming can also be disabled using the request parameter diazo.off. In my example above: http://localhost:8081/Plone/de/situation/@@situation-view?diazo.off=true.

The source code for plone.app.theming.policy.isThemeEnabled():

    def isThemeEnabled(self, settings=None):
        """Whether theming is enabled."""

        ...

        # Disable theming if the response sets a header
        if self.request.response.getHeader('X-Theme-Disabled'):
            return False
        
        # Check for diazo.off request parameter
        true_vals = ('1', 'y', 'yes', 't', 'true')
        if (debug_mode and self.request.get(
                'diazo.off', '').lower() in true_vals):
            return False

The following is also a solution:

# view.py
class SituationView(ViewMixinForTemplates, BrowserView):
    index = ViewPageTemplateFile("situation.pt")
    def __call__(self, *args, **kw):
        #self.request.response.setHeader('X-Theme-Disabled', 'True')
        self.request.response.setHeader('Content-Type', 'text/plain')
        return super().__call__(*args, **kw)

But I'm still wondering why the following doesn't set the header Content-Type:

index = ViewPageTemplateFile("situation.pt", content_type='text/plain')

You can also set the headers in your page template.

Here we're downloading an Excel sheet from Plone:

download_xlsx_template = ViewPageTemplateFile('templates/download_xlsx.pt')
[ do something ]
return self.download_xlsx_template(
                    self.context,
                    filename = filename,
                    data     = output.read()
                )

download_xlsx.pt

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:i18n="http://xml.zope.org/namespaces/i18n"
      lang="en"
      i18n:domain="mdb_theme"
      tal:define="filename options/filename;
                       dummy python:context.REQUEST.response.setHeader('Cache-Control', 'no-cache');
                       dummy python:context.REQUEST.response.setHeader('Pragma', 'no-cache');
                       dummy python:context.REQUEST.response.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                       dummy python:context.REQUEST.response.setHeader('Content-Disposition', 'attachment;; filename=' + filename  );
                       dummy python:context.REQUEST.response.write(options['data'])"
    />

@mtrebron Thanks. This is a really good idea. No need to mess with the view.

Actually you only need:

<tal:_ tal:define="_ python:context.REQUEST.response.setHeader('Content-Type', 'text/plain')" />

Or simply:

<?python context.REQUEST.response.setHeader('Content-Type', 'text/plain') ?>
1 Like