How to customize HTML title for a specific BrowserView

I've created a BrowserView called "foo" registered for "*". So, wherever the user appends /@@foo to the URL, it will view it.

My template has all the code filling the slots in the main template macros. The only thing I couldn't figure out is how to customize the HTML title tag (the one in the head element).

Looks like there is no slot for it, and the only docs I found tell about overriding the plone.htmlhead.title viewlet.

Isn't there a simpler solution? If not, how do I override the default viewlet for only my BrowserView (or only the template it uses)?

Maybe he means the HTML title tag, not the document title in the body.
In that case: there's no viewlet for that.

You can do it with Diazo in the same way Plone 5 display the "hero" banner in the front-page when you create a new site: you can copy the title content from another section of the page (a new viewlet?), then drop it. Or call a view on the context that returns the new title.

1 Like

@keul That's exactly what I am trying to do.

I could put a method in my view to return the value, but how do I insert the value in the HTML? Do I necessarily need to use Diazo?

(I am trying to avoid to use Diazo because creating a theme and a transformation XML seems like overkill to me, just for the sake of altering a bit of content in a HTML tag).

1 Like

I partially agree: using Diazo for such tasks seems a little hacky to me but it's not about performance: XSL tranformations are fast.
I think this is the quicker way; you can override the main_template probably... but isn't this ugly too?

1 Like

And what about registering a viewlet override for a specific template or view? Is it possible?

If so, I could customize plone.htmlhead.title for my foo view, right?

Upss... sorry. Wrong answer... title tag is not in the main_template.

You need simply to override the plone.htmlhead.title!
And yes, you can do it only for your view:

<browser:viewlet
            name="plone.htmlhead.title"
            manager="plone.app.layout.viewlets.interfaces.IHtmlHead"
            class="plone.app.layout.viewlets.common.TitleViewlet"
            view="your.package.IViewInterface"
            permission="zope2.View"
            />
1 Like

If you plan to use more than a Plone site in your Zope instance, don't forget to add a layer as well. Check the docs for more info:

http://docs.plone.org/develop/plone/views/layers.html

My "foo" BrowserView was going to be used in only one or two cases, and rarely, so I didn't feel like going all away to define a theme for my add-on.

The final solution was to create a subclass of ViewletBase in the same module of my BrowserView class, to create a method in the BrowserView to provide the values for the title tag, and a snippet for the registration of the plone.htmlhead.title override.

Thank you all for your help.

1 Like