Render Django-CMS plugins differently on different

2019-08-06 05:55发布

问题:

Short version: I'd like to have the same kind of cmsplugin use different templates in different placeholders. Is this possible? Or, should I go and make my custom plugin?

Long version: Using Django and Django-CMS and using the cmsplugin-video-youtube plugin, I have two youtube videos on the home page and they are styled using Foundation 4's grid system to be stacked one and then another below it and at a certain 'breaking point' of the browser's width, they will be side-by-side. This is all working well.

Now I'd like to add some unlimited number of YouTube videos in a different placeholder that is in a different page, which is of a different template. And I would like to have them rendered one below the former and so on and on a certain 'breaking point' of the browser width they will be horizontal side-by-side pairs, one pair below the former and so on.

Since these YouTube video elements are of an unlimited, not predefined, number, I can't use the grid system. I figured I'd use the block grid system. This has me create the elements as <li> elements inside a <u> element. This requires that I change the template for the cmsplugin-video-youtube element to render those <li> elements around the YouTubes. The problem with this is that if I override the template for this plugin, by supplying templates/cmsplugin-video-youtube/embed.html, it will also override it and alter it for the instances of this plugin that are in the home page. And I like them as they are.

So what I thought is that it would be beneficial to have a way of overriding the template for a plugin only for a specific placeholder. That would allow me to specify the YouTube's rendering in a specific placeholder in my other page and not affect the instances of this plugin in my home page, which I like as they are.

Is there such an option - to override the template of a plugin for a specific placeholder?

Otherwise, I thought that this might require a custom plugin that is based on the existing cmsplugin-video-youtube, perhaps a subclass of it, I would assume, that would then have the template that I require, with the <li> elements, for this other page.

A different approach would be to attempt to iterate over the instances of the plugin instance and render them with the whole <ul><li> thing around them, one by one. I've inquired about this approach and according to some answers I got, it would seem to be not the most easiest route.

What would be your approach to this, please?

I've not received an answer yet. Please, even a pointer would be something!

回答1:

I know this question is very old. .but hopefully anyone with same problem will find this useful.

Looking at sourcecode for CMSPluginBase:

https://github.com/divio/django-cms/blob/develop/cms/plugin_base.py

context['placeholder'] = placeholder

Placeholder is passed as context in the render method.

So in the template you can use that to to render different output. A good example for this:

https://github.com/stefanfoulis/cmsplugin-filer/blob/develop/cmsplugin_filer_image/templates/cmsplugin_filer_image/plugins/image/default.html