Home > Software engineering >  Jinja Templating - Using a macro to output a string with a link on a word or sentence in the output
Jinja Templating - Using a macro to output a string with a link on a word or sentence in the output

Time:06-07

My default template to use bootstrap

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home Page</title>
    <LINK href="{{ url_for('static', filename='reset.css') }}"
          rel="stylesheet">
    <link rel="stylesheet"
          href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
          crossorigin="anonymous">
    <LINK href="{{ url_for('static', filename='custom.css') }}"
          rel="stylesheet">
    <link rel="shortcut icon"
          href="#">

</head>
<body>
{{
components.main_navigation_bar()
}}
{% block content %} {%
endblock %}
{{ components.main_footer() }}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X 965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH 8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
        integrity="sha384-ZMP7rVo3mIykV 2 9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
        crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
        integrity="sha384-ChfqqxuZUCnJSK3 MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
        crossorigin="anonymous"></script>
</body>
</html>

From my level_0_01_global_macros.html

{% macro apa_reference_citation(content) %}
     <div >
         {{ content }}
     </div>
{%- endmacro %}

Now I am importing default template and macro's to my home_page.html

{% extends "level_0_00_default_layout.html" %}
{% import 'level_0_01_global_macros.html' as components %}

Now I am using the macro to write a citation on the home_page.html - this outputs to my browser server 127.0.0.1:5000/

{% block content %}

{{ components.apa_reference_citation('
Achdut, N., & Refaeli, T. (2020).
Unemployment and Psychological Distress among Young People during the COVID-19 Pandemic:
Psychological Resources and Risk Factors. International journal of environmental research and public
health,
17(19), 7163. https://doi.org/10.3390/ijerph17197163
') }}

{% endblock content %}

I want to add a link to the citation above on the "https://doi.org/10.3390/ijerph17197163" part of it. So on the webpage that this outputs to someone can click on that citation link and go to the website that has the study.

Things I tried:

-I tried inserting an <a></a> tag around the link, it just outputs a string when using "" or ``.

-Someone on reddit told me to put this in <a href='https://doi.org/10.3390/ijerph17197163'>https://doi.org/10.3390/ijerph17197163</a> The output was an error message that said, ""jinja2.exceptions.TemplateSyntaxError: expected token ',' got 'https'"

-I couldn't find an escape from the output above to insert any variable or elements.

-I did see I was able to wrap the entire macro in an <a></a> tag but I need to link specific words and not an entire citation or paragraph.

-I looked on the Jinja documentation and couldn't find anything that could answer my question. I am new to this so I apologize if it was there and I didn't see it.

-I browsed for similarly asked questions but the only ones I saw that were similar were ones on the macro itself and not when using it and they were for only one element like a form or an image.

-I posted on reddit:

Is there a way to accomplish this? Thank you in advance

CodePudding user response:

Instead of passing the entire content of the quote as a parameter, you can embed it in the call. In this way it is also possible for you to use the anchor as html in the code.

level_0_01_global_macros.html
{% macro apa_reference_citation() -%}
  <div >
    {{ caller() }}
  </div>
{% endmacro -%}
home_page.html
{% extends 'level_0_00_default_layout.html' %}
{% import 'level_0_01_global_macros.html' as components %}

{% block content %}

{% call components.apa_reference_citation() -%}
  Achdut, N., & Refaeli, T. (2020).
  Unemployment and Psychological Distress among Young People during the COVID-19 Pandemic:
  Psychological Resources and Risk Factors. International journal of environmental research and public health,
  17(19), 7163. <a href="https://doi.org/10.3390/ijerph17197163">https://doi.org/10.3390/ijerph17197163</a>
{% endcall -%}

{% endblock content %}
  • Related