... or how to reuse your plugins inside sections with different design
Problem: Frequently on the websites I am developing I need to display same set of data in several different ways. For example if I have a news box that needs to appear in different sections of the website e.i. in sidebar, main content etc. Using Django CMS plugins make this quite easy.
For simplicity we will take the following case. An image/text tuple with two layout variations - image on left of text and image on right.
Same data but different layout. All you need to do is just to allow your users to change the plugin template according to their needs. If you don't have experience with Django CMS Plugins I advice you to check how to create custom Django CMS Plugins before you continue with solution.
Solution: First you will have to create a tuple holding your templates(and their human readable names) and add a field that will hold the chosen template to the plugin model.
#models.py PLUGIN_TEMPLATES = ( ('image_on_left.html', 'Image on left'), ('image_on_right.html', 'Image on right'), ) class SamplePlugin(CMSPlugin): # your plugin properties here template = models.CharField('Template', max_length=255, choices = PLUGIN_TEMPLATES)
#cms_plugins.py class CMSSamplePlugin(CMSPluginBase): model = SamplePlugin name = 'Sample plugin' render_template = PLUGIN_TEMPLATES def render(self, context, instance, placeholder): if instance and instance.template: self.render_template = instance.template #your stuff here return context
Final words: Yep, this is all. Simple isn't it? It is amazing how sometimes such small things are so useful. If you are having bigger difference in the layout of your templates you will probably have to put a little more stuff in the context that some of your templates may not need but it is OK. Feel free to comment and if you are using this "trick" please add your use case - it will be interesting to see in how many different cases this works.