django cms: template for placeholder - python

Placeholder : packages_details_header
Banner
static block (where we add the text in textarea and convert it into html at time of render)
two label
label for heading
But as per the html wrappers and css classes, I think it is difficult to implement above structure
So my suggestion is
can we have pre defined template for placeholder?
Where all above listed plugins are predefined in placeholder template, like as shown in following example
{% block content %}
{% placeholder "packagesdetailscontent" %}
<div class=”banner”> banner plugin </div>
<div class=”static”> Static plugin </div>
..
..
..
<h2> label plugin</h2>
{% endplaceholder "packagesdetailscontent" %}
{% endblock content %}`
Please let me know, if there is any solution on it
thanks,
B

You can limit plugins allowed in a placeholder using CMS_PLACEHOLDER_CONF
See http://django-cms.readthedocs.org/en/latest/reference/configuration.html#cms-placeholder-conf
Update:
I read your question again, if I understand it correctly.. you are struggling with wrapping plugin with html class?
Have you had a look at https://github.com/divio/djangocms-style
It would allow you wrap your existing plugin in whatever classes you need :-)

Related

Highlighting the inner code blocks in Django Template Engine with django-pygmentify

I'm using django-pygmentify package in order to highlight my code blocks in my Django templates. The thing is that this package only supports code blocks as input. I have a model field that keeps markdown data. This markdown content might contain code blocks. (using ``` symbol)
Now, how can I highlight its inner code blocks??
Imagine I have a field that only contains source code. Like:
print('Hey..!')
In that case, this one works properly.
{% load pygmentify_tags %}
...
{% pygmentify %}
{{post.code}}
{% endpygmentify %}
Imagine my field contains the following content.
## Hello
This is my first step working with Python.
```python
print('Hey..!')
‍```
In this case, how can I implement it?? I can render that whole markdown content with {{post.body|markdown|safe}}, but how can I highlight those code blocks?? I also want to give all those code blocks a class name .code-block for better styling. Should I create a custom template tag?
You may use html standard <code> tag like this:
{% load pygmentify_tags %}
...
{% pygmentify %}
<code>
{{post.code}}
</code>
{% endpygmentify %}
This will separate the code section and at the same time will apply pygmentify to it.

How to avoid rendering a block in a base template - or: how to define a multiline block-like variable?

I am rather new to jinja, so please excuse this potentially dumb question ...
I have a jinja macro sitting in a file named lib.jinja which looks as as follows
{% macro some_macro(some_content) %}
<div class="some_class">{{ some_content }}</div>
{%- endmacro %}
I import it from another file named content.htm, which looks roughly as follows:
{% from "lib.jinja" import some_macro %}
{% block content %}
Some content
{% endblock %}
{{ some_macro(self.some_macro(self.content())) }}
The rendered output looks as follows:
Some content
<div class="some_class">Some content</div>
Notice that the content appears twice, once rendered through/by the macro, another time - directly above - rendered like it was defined in the block named content in content.htm. I would love to avoid that the block is rendered twice - it is supposed to be fed into the macro and rendered through it only.
I appears that I can "work around" it by turning content.htm into a child template (by adding an extends tag at its top) of an empty base template.
Maybe I have a lack of understanding for the concepts here: A block is not a variable - I get that. If there is no extends tag, the template is treated as a base template, I assume. If there are no child templates or other directives, I guess the block is just rendered as-is and then, for a second time so to speak, picked up by the macro - correct?
How can I solve this in a sane manner, i.e. how could I handle this other than by extending an empty base template? Is there another type of block or variable that would fit my needs?
Just after I submitted this question, I finally found what I was looking for in the manual: Block Assignments.
{% from "lib.jinja" import some_macro %}
{% set content %}
Some content
{% endset %}
{{ some_macro(self.some_macro(content)) }}
The set and endset tags solve my problem.

Django template: Translate include with variable

I have a template in which you can pass a text variable. I want to include this template into another one but with a translated text as it's variable. How can you achieve this?
I would like something like this:
{% include "a_dir/stuff.html" with text={% trans "Load more promotions" %} %}
I tough about writing my own template tag that will perform a ugettext but then when creating the .po file, the text variable will not be taken automatically.
I don't want to do this work in the view since all our translations take place in the templates.
You can put the translated string into a variable with the as syntax. For instance:
{% trans "Load more promotions" as promotions %}
{% include "a_dir/stuff.html" with text=promotions %}
See the docs for more details.
A shorter way is
{% include 'a_dir/stuff.html' with text=_("Load more promotions") %}
which also works fine with variables

How do I build reusable widgets in jinja2?

I want to define a widget something vaguely like this:
{% block css %}
.mywidget {
css: goes_here;
{% endblock %}
{% block widget %}
<div class="mywidget">
<!-- structure goes here -->
</div>
{% endblock %}
{% block script %}
$( ".mywidget" ).addFunctionality(stuff)
{% endblock %}
In other words, a deceleration of what CSS the widget needs, what its contents are (preferably parametrized in some way), and what scripts it requires at the end of the file. Then, I would like to be able to extend a layout template, add widgets to the body (possibly multiple widgets of the same type with different parameters of some kind), and have the CSS and javascript properly added to the top and bottom of the layout template, once per widget type.
This seems like a pretty clean and straightforward design, and coming from a native UI design perspective, I am confused as to why I cannot find any examples of how to do something like this.
You have fleshed out part of the design for a widget system, but really have only shown how you would go about designing the widget. The other part is how you would end up USING the widget in Jinja.
For example, you could use Jinja Macros to define the widget. Create a file "mywidget.html" and use...
{% macro css() -%}
.mywidget {
css: goes_here;
}
{% endmacro %}
{% macro widget() -%}
<div class="mywidget">
<!-- structure goes here -->
</div>
{% endmacro %}
{% macro script() -%}
$( ".mywidget" ).addFunctionality(stuff)
{% endmacro %}
Then, in your HTML using this widget, you could do...
{% import 'mywidget.html' as mywidget %}
...
<html>
<head>
<style>
{{ mywidget.css() }}
</style>
<head>
<body>
{{ mywidget.body() }}
<script>
{{ mywidget.script() }}
</script>
</body>
</html>
Of course, the problem here is that you need to manually put all of the widgets into the various areas. Once you get a larger number of widgets, it might be hard to keep track of them, and it would be easy to, for example, have the mywidget.script() code created multiple times, which would cause duplicate event fires.
And, of course, you could always have Python objects as part of the context rendering the final solution. The important thing to note is that Jinja just renders text from templates. The templates don't even have to be HTML template, you could use Jinja to render a plain text e-mail. Therefore, it would be difficult to imagine the authors of the library trying to create these sort of "widget" system and expect everyone to be happy by the result. Why increase the complexity of the library with such a complex feature that they will need to support (especially since Jinja provides people with the tools to build such a framework already)?

Dynamic template "Includes" with django

I am building a Django website and my side bar can have different elements for different users. So my main sidebar template has a div for every plugin to be included and the specific HTML for every one of these plugins is included in their own template file.
example:
<div id="plugins">
<div id="plugin1">
{% include 'plugin1.html' %}
</div>
<div id="plugin2">
{% include 'plugin2.html' %}
</div>
</div>
Now I want to build this list dynamically how could I do it? As the template is only parsed once so I could not send it a '{% include 'plugin1.html'}' string in the context
Any ideas?
You can use a variable inside the include tag:
{% include my_user_html %}
You can generate a variable in the view as above containing your template or you can use a template tag to generate the template path for you based on another variable, i.e. a phase. Register the following tag, customize it to your needs:
#register.filter
def get_template_phase(template_string, phase):
template_string = template_string.replace('<', '{').replace('>', '}')
return template_string.format(phase=phase)
Place the above in your templatetags and register it.
Usage:
{% include 'includes/home__<phase>.html'|get_template_phase:'nomination' %}

Categories