Handling template tags in when migrating a theme to a tutor plugin

I tried to convert my theme to a tutor plugin but I came across the following error:

tutortheme/templates/dt_theme/lms/templates/ace_common/edx_ace/common/base_body.html", line 1, in template
    {% load i18n %}
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'load'.

The base_body.html file contains the following:

{% load i18n %}
{% load ace %}

{% get_current_language as LANGUAGE_CODE %}
{% get_current_language_bidi as LANGUAGE_BIDI %}

{# This is preview text that is visible in the inbox view of many email clients but not visible in the actual #}
{# email itself. #}

<div lang="{{ LANGUAGE_CODE|default:"en" }}" style="
    display:none;
    font-size:1px;
    line-height:1px;
    max-height:0px;
    max-width:0px;
    opacity:0;
    overflow:hidden;
    visibility:hidden;
">
    {% block preview_text %}{% endblock %}
</div>

Is there currently a way to get around this? If not, what would a solution look like?

Right. The problem is that your theme file is first rendered by Tutor (during tutor config save), before being rendered by edx-platform (at build time). And Tutor does not know anything about edx-platform-specific tags, such as {% load i18n %}.

One solution – albeit a very clumsy and inconvenient one – is to escape all {{ ... }} and {% ... %} tags: Template Designer Documentation — Jinja Documentation (3.1.x) This is the only possible solution in the case when your template file includes both Tutor and edx-platform variables.

If your template includes only edx-platform variables, then you could bypass Tutor rendering entirely. Currently there is no plug-n-play way to do that, but it should not be too hard to implement. For instance, binary files are only copied, and not rendered: tutor/env.py at 9a63dc70ceb2044ceb2027025914ab1fe7a5a025 · overhangio/tutor · GitHub
We could generalize this behaviour by creating an ad-hoc “ENV_TEMPLATE_SKIP_RENDERING” filter. Would that work for you?

1 Like

My templages only contains edx-platform variables for now but I want the option of using tutor variables.

So short-term I could wrap the whole template with:

{% raw %}
    {% load i18n %}
   {% load ace %}
   ...
{% endraw %}

That would make sense and it would make it applicable to other use cases!
Would this variable store the name of the file or the path?

The filter would take as argument the template path, which is a slash-separated path (even on Windows) that is relative to one of the template roots.

1 Like

Could you share this solution using ENV_TEMPLATE_SKIP_RENDERING if you implemented it?

Hi, would you mind to explain your solution? I’m having the same issue, but I doesn’t seem to understand what I need to do? Sorry, I’m new to openedx and tutor

I was able to fix the issue by wrapping the jinja code with

{% raw %}
    ....
{% endraw %}

as @BbrSofiane said

Hi, @regis , was this approach with ENV_TEMPLATE_SKIP_RENDERING solution implemented? If yes, can you tell more how to use filter?

Hi @zameel7 , do you have any problem when using your approach (with {% raw %} and {% endraw %}). When doing so I have error saying this tag (‘raw’) was not registered. Have you registered this tag somehow, somewhere?

It was working for me in Olive. Haven’t yet tested in palm. With tutor version are you using?

Palm version

@zameel7 , just talking about Olive version, can you please tell me: was it enough for you to wrap the jinja code with {% raw %} and {% endraw %} or
 it was also necessary to register ‘raw’ tag somewhere inside openedx code? (eg in templatetags directory for corresponding app or register in any other way)?

The wrapping was enough for me.

1 Like

thank you @zameel7 for quick and helpful reply

1 Like