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

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.

Related

How to append specific html to a Django code block using only Python/Django?

I have a form I'm working with in Django.
I have a built in error message I'm trying to get to render on the form.
My first step is to get the error message to render on the form and then I will go into the function and tweak when it shows up.
My problem emerges when it comes to doing it in python.
Normally, my preferred way would be to JQuery for the footer and use JavaScript to append/prepend the HTML. Then set it to show/hide based on conditionals.
However, for this I am wanting to do it in Python to make it easier for the people working w/ me.
Here's an example of the error message HTML I would like to use for appending to something else in JS.
error_field.append('<em for="name" class="form-error-message text-danger">');
Here is an example of the Django Code Block I would like to add it within
{% block form-footer %}
{{ block.super }}
{% endblock %}
What is the easiest way to accomplish this within Python/Django? To be clear, I can figure out the conditional stuff myself. Just the rendering of the specific HTML/CSS error class I have already created. I should be able to do the conditional/function part of this myself.
I can just show you an example, this is a part of my project
views.py
try:
user=User.objects.get(username=username)
except:
messages.info(request,'username does not exist')
return redirect('login')
return render(request,'users/login-register.html')
html
{% if messages %}
{% for i in messages %}
<div class="alert alert--{{i.tags}}">
<p class="alert__message">{{i}}</p>
<button class="alert__close">x</button>
</div>
{% endfor %}
{% endif %}
You can use this anywhere in your html page. This is a common page, and everything in here is extended. And of course this is an example similar to your problem. Check it out if you want

Break long {% with %} assignment into multiple lines in a Django template [duplicate]

I am creating a custom django template tag by using such a code :
#register.simple_tag(takes_context=True)
def render_listing(context, *args, **kwargs):
... my code ...
This works well, but in my template, it seems that all parameters must be on a single line, for example :
this works:
{% render_listing param1=val1 param2=val2 ... paramN=valN %}
but on multiple lines, it does not work :
{% render_listing param1=val1
param2=val2
...
paramN=valN %}
I tried multiple escape sequences but I did not succeeded,
Is there a way to specify a template tag on multiple lines ?
No, the Django template language does not support multiple line tags. See ticket 8652, which was closed as WONTFIX, or this thread from the django-developers mailing list.
Sometimes, if there is a repeated prefix, you can make it more readable by using the with tag. For example if you have,
{% render_listing param1=long.common.prefix.val1 param2=long.common.prefix.val2 param2=long.common.prefix.val3 %}
you could rewrite as
{% with prefix=long.common.prefix %}
{% render_listing param1=prefix.val1 param2=prefix.val2 param2=prefix.val3 %}
{% endwith %}
Often (but not always), a really long tag is an indication that you're putting too much logic in the template. See if you can move some of it into the view, model method, template tag or template filter.
It's pretty straightforward to enable, though hackish:
import re
from django.template import base
base.tag_re = re.compile(base.tag_re.pattern, re.DOTALL)
"Using" it is simple; one place I find it especially useful is {% include %} tags:
{% include 'my/sweet/modal-template.template' with
message="Hey, do you really want to frob the widget?"
yes="Heck yes I do!"
no="No frickin' way!"
icon="error"
%}
I haven't tested this in more recent versions of Django but I imagine it could be adapted; that worked at least back around 1.8. I should point out that in theory some tags that do custom parsing of their arguments could break; in practice I haven't had any trouble in the last ~10 years of Django programming.

django cms: template for placeholder

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 :-)

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

Including Blocks from Jinja templates

I would like to include blocks from templates instead of macros, as many templates will be including content from many other templates, so extends isn't an option.
I've read many answers about this, including blocks in included files, but the use case always seems to be different. I suspect this cannot be done.
template1.html
{% block description %}
<p> Here is a description </p>
{% endblock %}
And in template2.html
{% from 'template1.html' import description %} <- doesnt work
You have two options here.
Use a macro, which sounds like something you don't want to do.
Use a template_filter.
Assuming you're using Flask, this is as easy as:
#app.template_filter('get_block')
def template_filter(block, file):
html = render_template(file)
# Then use regex or something to parse
# or even simple splits (better with named blocks), like...
content = html.split('{%% block %s %%}'%(block))[-1]
content = content.split('{%% endblock %%}')[0]
return content
And to use it:
{% 'description'|get_block('template1.html') %}

Categories