Skip to content

Commit

Permalink
Add more docs for overriding default tempates (aminalaee#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonocodes authored May 15, 2024
1 parent cdff6b4 commit d062cfa
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions docs/working_with_templates.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
The template uses `Jinja2` template engine and by default looks for a `templates` directory in your project.
The template uses `Jinja2` template engine and by default looks for a `templates` directory in your project. As the first step you should create a `templates` directory in you project.

If your `templates` directory has the default template files like `sqladmin/list.html` or `sqladmin/create.html` then they will be used.
Otherwise you can create custom templates and use them.

## Customizing templates

As the first step you should create a `templates` directory in you project.
## Customizing specific templates

Since `Jinja2` is modular, you can override your specific template file and do your changes.
For example you can create a `custom_details.html` file which overrides the `sqladmin/details.html` from
Expand All @@ -26,6 +21,26 @@ SQLAdmin and in the `content` block it adds custom HTML tags:
details_template = "custom_details.html"
```

## Overriding default templates

If you need to change one of the existing default templates in SQLAdmin such that it affects multiple pages, you can do so by copying the existing template from `templates/sqladmin` into your `templates/sqladmin` directory. It will then be used instead of the one in the package. For example if there is some Javascript you want to run on every page you may want to do it in layout.html like so:

!!! example

```html title="myproject/templates/sqladmin/layout.html"
...
</div>
</div>
{% endblock %}

{% block tail %}
<script type="text/javascript">
console.log('hello world');
</script>
{% endblock %}

```

## Customizing Jinja2 environment

You can add custom environment options to use it on your custom templates. First set up a project:
Expand Down Expand Up @@ -90,7 +105,7 @@ Usage in templates:
```python
def value_is_filepath(value: Any) -> bool:
return isinstance(value, str) and os.path.isfile(value)

admin.templates.env.globals["value_is_filepath"] = value_is_filepath
```

Expand Down

0 comments on commit d062cfa

Please sign in to comment.