Django CMS改进文本插件
cmsplugin-text-ng的Python项目详细描述
django CMS的Text插件的增强版本。它允许包装 可由CMS内容编辑器选择的模板内的文本插件。
注意
此插件不是用来替换cms.plugins.text。它是一个 对某些用例的增强。对于大多数类型的内容,您应该 可能仍然使用cms.plugins.text或编写专门定制的 插件。
要求
- Django 1.4+
- Django CMS 2.3+
- djangocms text ckeditor(仅当使用cms 3+)
安装
- Add ^{tt4}$ to your ^{tt5}$.
- Create some templates (more on that soon) and add them in the admin.
基本示例:静态模板
假设你想要一个带有facebook“like”按钮的文本插件。你的 模板可能如下所示:
<div class="text left"> {{ body|safe }} </div> <div class="fb-like right"> <h2>Like this page on facebook!</h2> <fb:like send="false" width="450" show_faces="true"></fb:like> </div>
高级示例:动态模板
假设您想在每个插件上设置<h2>-标记的内容 基础。没问题!这就是{% define %}template标记的作用:
{% load text_ng_tags %} {% define h2_content as text %} <div class="text left"> {{ body|safe }} </div> <div class="fb-like right"> <h2>{{ h2_content }}</h2> <fb:like send="false" width="450" show_faces="true"></fb:like> </div>
当你编辑这个插件时,你会有一个文本框,上面写着“h2_content” 标签。其内容将在呈现插件时添加到上下文中。你 可以像任何上下文变量一样访问它:{{ h2_content }}。
模板标记的as text部分引用变量的类型。 cmsplugin文本ng带有一个类型(text)。此外,还有一个 image键入使用 django-filer将图像添加到模板上下文。如果你想用它, 确保filer和cmsplugin_text_ng.contrib.textng_file都是 列在您的INSTALLED_APPS中。
真的(开玩笑)高级示例:定义自己的类型
所以,您需要在“like”按钮下面添加一些html代码,然后 内容编辑坚持使用tinymce。我们来吧!使用令人敬畏的 HTMLField在django-tinymce中,我们建立了一个带有tinymce'd的模型 文本区域:
from django.utils.translation import ugettext_lazy as _ from tinymce.models import HTMLField from cmsplugin_text_ng.models import TextNGVariableBase from cmsplugin_text_ng.type_registry import register_type class TextNGVariableHTML(TextNGVariableBase): value = HTMLField(null=True, verbose_name=_('value')) class Meta: verbose_name = _('html text') verbose_name_plural = _('html texts') register_type('htmltext', TextNGVariableHTML)
有几点需要注意:
- your type has to inherit from ^{tt17}$.
- the field containing the data that should end up in the context has to be named “value”
- it has to be nullable (the ^{tt18}$ part).
- the type name (^{tt19}$ in the example) has to be unique over the whole project. You might want to prefix it with something unique to your app.
cmsplugin text ng将抱怨(大声!)如果不满足这些条件。
我们在哪里?对,模板。在模板中使用你新的,很棒的类型, 只要使用{% define %}标记就可以了,就像这样:
{% load text_ng_tags %} {% define h2_content as text %} {% define html_content as htmltext %} <div class="text left"> {{ body|safe }} </div> <div class="fb-like right"> <h2>{{ h2_content }}</h2> <fb:like send="false" width="450" show_faces="true"></fb:like> {{ html_content|safe }} </div>
完成。