Skip to content

Commit

Permalink
docs fix code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdamusic committed Oct 30, 2022
1 parent f34f4d1 commit eaee00b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Is Ontospy for Me?](#is-ontospy-for-me)
- [Generating Ontology Documentation](#generating-ontology-documentation)
- [Miscellaneous Tips](#miscellaneous-tips)
- [Version 2 Upgrade](#upgrading-to-v-2.0)
- [Quick Links](#quick-links)

## Welcome to Ontospy's Documentation!
Expand Down
37 changes: 16 additions & 21 deletions docs/pages/django-to-jinja.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This is to make Ontospy footprint more lightweight, as well as to simplify futur

This page contains information of Django specific template filters, or constructs, that are not directly usable with Jinja and how they have been updated.

{% raw %}

## NOW

Expand All @@ -14,9 +15,7 @@ Django's template tag `now` can be used in Jinja after installing the extension
Then you can do

```python
{% raw %}
{% now 'utc', '%a, %d %b %Y %H:%M:%S' %}
{% endraw %}
```


Expand All @@ -27,21 +26,18 @@ Django's template tag `ifchanged` does not exist in Jinja. So a custom logic for
From

```python
{% raw %}
{% for each in o.annotations %}
{% ifchanged each.1 %}
{% if not forloop.first %}</dl>{% endif %}
<dt>{{each.1}}</dt>
{% endifchanged %}
<dd>{{each.2|linebreaks}}</dd>
{% endfor %}
{% endraw %}
```

To

```python
{% raw %}
{% for each in o.annotations() %}
{% if each.1 != variable_watcher %}
{% if not loop.first %}</dl>{% endif %}
Expand All @@ -51,7 +47,6 @@ To
{% endif %}
<dd>{{each.2|linebreaks}}</dd>
{% endfor %}
{% endraw %}
```


Expand All @@ -60,7 +55,7 @@ To
Django's template tag `ifchanged` has a slightly diffent syntax in [Jinja](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.default).

From
{% raw %}

```python
{{s.qname|default:each.qname}}
```
Expand All @@ -69,13 +64,13 @@ To
```python
{{s.qname|default(each.qname)}}
```
{% endraw %}


## LINEBREAKS

Django's template filter `linebreaks` does not exist in Jinja. It can be implemented as a custom filter (see this [thread on SO](https://stackoverflow.com/questions/4901483/how-to-apply-django-jinja2-template-filters-escape-and-linebreaks-correctly))

{% raw %}

```python
import re
from markupsafe import Markup, escape
Expand All @@ -93,57 +88,57 @@ def linebreaks_filter(eval_ctx, value):

env.filters['linebreaks'] = linebreaks_filter
```
{% endraw %}


## METHOD CALLS

Django's templating language is 'relaxed' when it comes to resolving an object *attribute* or *method* call. In both cases, it's enough to pass the *attribute* or *method* name.

With Jinja, *method* calls need to be followed by parentheses, like in Python. See also https://stackoverflow.com/questions/59589889/difference-between-an-item-and-an-attribute-jinja-python

{% raw %}

From
```python
"""{% for each in o.annotations %}"""
{% for each in o.annotations %}
```

To
```python
"""{% for each in o.annotations() %}"""
{% for each in o.annotations() %}
```
{% endraw %}


PS this applies to `each.children()` , `each.parents()`, `each.rdf_source()` etc..


{% raw %}

## IFEQUAL

From

```python
"""{% ifequal objtype "class" #}"""
{% ifequal objtype "class" #}"""
```

To

```python
"""{% if objtype == "class" #}"""
{% if objtype == "class" #}"""
```

## WITH

From
```python
"""{% with main_entity as each #}"""
{% with main_entity as each #}"""
...
{% endwith %}
```

To

```python
"""{% set each = main_entity #}"""
{% set each = main_entity #}"""
# no need to close anything
```

Expand All @@ -154,13 +149,13 @@ See https://jinja.palletsprojects.com/en/3.1.x/templates/#comments
From

```python
"""{% comment %}
{% comment %}
```

To

```python
"""{%
{%
comment
#}"""
```
Expand Down

0 comments on commit eaee00b

Please sign in to comment.