如何在Plone中注入模板代码? [英] How to inject template code in Plone?

查看:227
本文介绍了如何在Plone中注入模板代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在每篇 Plone文章(在页面的标题和第一段之间)注入一些HTML代码?我正在运行Plone 4.有没有人有关于如何实现这一点的提示?



另一个问题是:是否可以在每个Plone中随机放置一些HTML代码文章?

解决方案

使用 viewlets 。 Viewlet可以插入到渲染页面的不同部分,具体取决于您注册的Viewlet管理器以及Viewlet应用的接口。



Viewlet管理器确定Viewlet插入的位置,并且注册Viewlet的界面决定Viewlet将在哪种类型的URL上可见。注册*表示到处显示,并注册为Products.ATContentTypes.interfaces.IATDocument表示它只有在查看页面时才可见。



要查看哪些Viewlet管理器可用以及哪些Viewlet在其中注册,对于任何给定的URL,只需添加/ @@管理-viewlets到URL。你可以看到有一个 plone.abovecontent 和一个 plone.abovecontenttitle viewlet管理器,可以让你插入HTML在那里。其中大部分是在 plone.app.layout 包中定义的,您需要找到那个名称的接口(在egg目录中找到它) )。

你使用ZCML注册一个viewlet,所以你需要已经有一个为你的网站加载的python包。要插入任意模板,只需将其注册到浏览器:viewlet 指令:



< pre-class =lang-xml prettyprint-override> < browser:viewlet
name =your.html.snippet
for =Products.ATContentTypes.interfaces.IATDocument
manager =plone.app.layout.viewlet.interfaces.IAboveContent
template =htmlsnippet.pt
permission =zope.Public
/>

现在,基于 htmlsnippet.pt template已注册为 plone.abovecontent viewlet管理器。

 < div i18n:domain = 你的 > 
< h3 i18:translate =html_snippet_header>这是将被注入所有页面的HTML片段!< / h3>
< / div>

您也可以添加一个完整的浏览器视图类来备份viewlet,模板可以访问类,通过view /命名空间,所以你可以添加一个方法来返回随机文本插入。



你可以通过前面提到的' / @@ manage-viewlets'URL,或者您可以使用GenericSetup配置文件,其中viewlets.xml让您管理注册和Viewlets的订购:



<?xml version =1.0?>
< object>
< order manager =plone.abovecontentskinname =My Theme
based-on =Plone Default>
< viewlet name =your.html.snippetinsert-before =*/>
< / order>
< / object>

有关更多信息,请参阅 viewlets教程


My goal is to inject some HTML-Code in front of every Plone article (between the page's header and the first paragraph)? I'm running Plone 4. Does anyone have a hint on how to realize that?

The other question is: is it possible to place some HTML code randomly in every Plone article?

解决方案

Use viewlets for both usecases. A viewlet can be inserted in different parts of the rendered page, depending on both the viewlet manager you register it to and and what interfaces the viewlet applies to.

The viewlet manager determines the location the viewlet is inserted at, and the interface you register the viewlet for determines on what kind of URL the viewlet will be visible at. Registering for "*" means show everywhere, and registering it for "Products.ATContentTypes.interfaces.IATDocument" means it'll only be visible when viewing a Page.

To see what viewlet managers are available and what viewlets are registered there, for any given URL, just add "/@@manage-viewlets" to the URL. There you can see that there is a plone.abovecontent and a plone.abovecontenttitle viewlet manager that would enable you to insert HTML there. Most of these are defined in the plone.app.layout package, and you'll need to find the interface that is registered with that name there (find it in your eggs directory of the buildout).

You register a viewlet using ZCML, so you need to already have a python package that is loaded for your site. To insert an arbitrary template, simply register it with the browser:viewlet directive:

<browser:viewlet
    name="your.html.snippet"
    for="Products.ATContentTypes.interfaces.IATDocument"
    manager="plone.app.layout.viewlet.interfaces.IAboveContent"
    template="htmlsnippet.pt"
    permission="zope.Public"
    />

Now, a viewlet based on the htmlsnippet.pt template is registered for the plone.abovecontent viewlet manager. That template is a full zope page template, so you can use things like internationalisation in there:

<div i18n:domain="your">
    <h3 i18:translate="html_snippet_header">This is a snippet of HTML that'll be injected into all Pages!</h3>
</div>

You can also add a full browser view class to back up the viewlet, and the template can access that class via the "view/" namespace, so you could add a method there to return random text to be inserted.

You can either manually enable the viewlet via the aforementioned '/@@manage-viewlets' URL or you can use a GenericSetup profile, where viewlets.xml let's you manage the registrations and ordering of viewlets:

<?xml version="1.0"?>
<object>
  <order manager="plone.abovecontent" skinname="My Theme"
         based-on="Plone Default">
    <viewlet name="your.html.snippet" insert-before="*"/>
  </order>
</object>

For more information, I refer you to the viewlets tutorial on Plone.org.

这篇关于如何在Plone中注入模板代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆