django cms [aldryn newsblog]替换插件的模板 [英] django cms [aldryn newsblog] replace template for plugin

查看:287
本文介绍了django cms [aldryn newsblog]替换插件的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在不同的子页面上替换相同的插件模板。在首页我需要特定的滑块模板的最新文章,详细的文章我需要小列表,搜索结果没有图像等
[注意:关于aldryn newsblog应用程序的一切 - 我不是说我自己的插件!] / p>

*(类似于每个插件实例的自定义模板)



如何替换它?扩展模板不完全是我需要的 - 继承是从底层 - 从下面的子模板到base.html - 但该插件已经硬编码了较低的模板。



模板是不合理的,那么我们在MVC中考虑。
*(像这里以不同的方式呈现Django-CMS插件在不同的splaceholder /模板



或者也许只是使用hardcoded(包括插件)编写自定义模板?但是使用django cms占位符编辑器是非常有用的,最好继续这样工作:///



所以,我创建front.html基本模板首页,
将一些插件放置到占位符 - 并且需要在此front.html中替换此插件的子模板,并在其他位置保留该插件的子模板 - 这是主要目标。



当django cms / aldryn newsblog提供每个插件实例的自定义模板选项将是最好的:
(像这样一个 http://www.ilian 。/ / django-cms-plugins-with-selectable-template /

解决方案

正确的(这里晚了),你需要一种方式来覆盖插件实例基础上的插件模板,因为黑客django模板是不行的(我同意)。



Django CMS允许您在实例的基础上覆盖插件模板。
所示http://docs.django-cms.org/en/develop/how_to/custom_plugins.html?highlight=get_render_template#the-simplest-plugin



在您的CMSPluginBase子类中添加以下内容:

  def get_render_template(self,context,instance,placeholder):
#去这里
return'sometemplate.html'

至于如何知道哪个模板使用页面的反向ID:


$($)

b $ b

  page = instance.page 

templates = {
'homepage':'plugin_home.html',
'关于':'plugin_about.html',
'contact':'plugin_contact.html',
}
返回模板[plugin.page.reverse_id]

这种方法有一些缺点:




  • 依赖插件被绑定到页面。 (插件可以在页面之外)

  • 只能使用具有反向ID设置和反向ID的页面
    每页都是唯一的,这意味着您必须列出反向ID
    为您要更改模板的每个页面。



使用页面扩展名每页设置一个类别:



结帐 http://docs.django-cms.org/en/develop/how_to/extending_page_title.html
使用这种方法,您可以设置某种类别到多个页面,所以您可以像这样一个目标中的多个页面:

  page = instance.page 
extension = get_page_extension(page)#查看文档
templates = {
'category_1':'plugin_category_1.html',
'category_2':'plugin_category_2.html',
'category_3':'plugin _category_3.html',
}
返回模板[extension.category.name]

优点:




  • 可以一次定位多个页面



缺点:




  • 依赖插件绑定到一个页面。

  • 有点复杂



使用模板上下文变量:
在您的模板中,具体取决于你如何渲染你的插件,你可以
提供一个这样的上下文变量:

  {%with category = 'category_1'%} 
{%占位符'内容'%}
{%endwith%}

  {%with category ='category_1'%} 
{%render_plugin yourplugin%}
{%endwith%}

然后在您的 get_render_template 方法,您可以访问此上下文变量并执行以下操作:

 #使用.get() ide fallback 
category = context ['category']

templates = {
'category_1':'plugin_category_1.html',
'category_2':'plugin_category_2。 html',
'category_3':'plugin_category_3.html',
}
返回模板[category] ​​

优点:




  • 没有额外的款式。

  • 可以目标多个页面一次。



缺点:




  • 我可以想到的唯一一个是
    模板中的这些随机的 {%with%}



我完全错过了newblog部分,所以为了覆盖newsblog插件或任何插件,只需将子类化为您要覆盖的插件类,并注销原始文件,然后注册您的



也许你可以将上面的整个模板逻辑放在一个混合中,在整个项目中使用。


I need to replace same plugin template on different subpages - e.g. on frontpage I need specific slider template for latest articles, in detail article I need small list, on search result without images etc. [note: everything about aldryn newsblog app - I don't mean my own plugin!]

*(something like custom template per plugin instance)

How to replace it ? Extending template is not quite what I need - inheritance is from bottom - from lower subtemplate to base.html - but that plugin have hardcoded lower template.

Tons of IF block in template is irrational then we think in MVC. *( like here Render Django-CMS plugins differently on different splaceholders/templates )

Or maybe just write custom template with using hardcoded including plugins ? But using django cms placeholder editor is very useful and it'll be better to keep working in that way :///

So, I create front.html base template for frontpage, put some plugins to placeholders - and need to replace subtemplates for this plugins only in this front.html and keep subtemplates for that plugin in other places - this is main goal.

It will be the best, when django cms / aldryn newsblog provide option "custom template" per plugin instance :| ( like this one http://www.ilian.io/django-cms-plugins-with-selectable-template/ )

解决方案

If I understand your question correctly (it's late here), you need a way to override plugin templates on a plugin instance basis because hacking django templates is not the way to go (I agree).

Django CMS does allow you to override plugin templates on an instance basis. As shown in http://docs.django-cms.org/en/develop/how_to/custom_plugins.html?highlight=get_render_template#the-simplest-plugin

In your CMSPluginBase subclass add the following:

  def get_render_template(self, context, instance, placeholder):
      # criteria goes here
      return 'sometemplate.html'

As far as how to know which template to render when (criteria), you can do a few things.

Use the page's reverse id:

page = instance.page

templates = {
    'homepage': 'plugin_home.html',
    'about': 'plugin_about.html',
    'contact': 'plugin_contact.html',
}
return templates[plugin.page.reverse_id]

This approach has a few drawbacks:

  • Relies on plugin being bound to a page. (plugins can live outside of pages)
  • Can only work with pages that have reverse id set and reverse ids are unique per page which means you would have to list reverse id for every page you want to change template for.

Use a page extension to set a category per page:

Checkout http://docs.django-cms.org/en/develop/how_to/extending_page_title.html With this approach you can then set some sort of category to multiple pages and so you can target multiple pages in one shot like so:

    page = instance.page
    extension = get_page_extension(page) # Check out docs for this
    templates = {
        'category_1': 'plugin_category_1.html',
        'category_2': 'plugin_category_2.html',
        'category_3': 'plugin_category_3.html',
    }
    return templates[extension.category.name]

Pros:

  • Can target multiple pages in one shot

Cons:

  • Relies on plugin being bound to a page.
  • A bit more complex

Use a template context variable: In your templates, depending on how you're rendering your plugins, you can provide a context variable like so:

{% with category='category_1' %}
{% placeholder 'content' %}
{% endwith %}

or

{% with category='category_1' %}
{% render_plugin yourplugin %}
{% endwith %}

Then in your get_render_template method you can access this context variable and do the following:

# Use .get() to provide fallback
category = context['category']

templates = {
   'category_1': 'plugin_category_1.html',
   'category_2': 'plugin_category_2.html',
   'category_3': 'plugin_category_3.html',
}
return templates[category]

Pros:

  • No extra models.
  • Can target multiple pages in one shot.

Cons:

  • The only one I can think of is these random {% with %} in templates.

I completely missed the newblog part, so in order to override the newsblog plugins or any plugin, just subclass the plugin class you want to override and unregister the original and then register yours, make sure yours has the same class name.

Maybe you can make the whole template logic above into a mixin to use throughout your project.

这篇关于django cms [aldryn newsblog]替换插件的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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