Skip to content

Commit

Permalink
Add layout endpoints, rename base template
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Nov 29, 2023
1 parent 10dad7f commit bd71b44
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 3 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/nationalarchives/tna-frontend-jinja/compare/v0.1.4...HEAD)
## [Unreleased](https://github.com/nationalarchives/tna-frontend-jinja/compare/v0.1.5...HEAD)

### Added
### Changed
Expand All @@ -14,6 +14,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
### Security

## [0.1.5](https://github.com/nationalarchives/tna-frontend-jinja/compare/v0.1.4...v0.1.5) - 2023-11-29

### Added

- Routes added for layout templates

### Changed

- Base template changed from `generic.html` to `base.html`

## [0.1.4](https://github.com/nationalarchives/tna-frontend-jinja/compare/v0.1.3...v0.1.4) - 2023-11-29

### Changed
Expand Down
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,45 @@
![Python version](https://img.shields.io/pypi/pyversions/tna-frontend-jinja?style=flat-square&logo=python&logoColor=white)
[![Licence](https://img.shields.io/github/license/nationalarchives/tna-frontend-jinja?style=flat-square)](https://github.com/nationalarchives/tna-frontend-jinja/blob/main/LICENCE)

## Quickstart
## Quickstart for Flask projects

Use the Flask application's `jinja_loader` to allow templates included from either your app (in the below example called `app`) and the `tna_frontend_jinja` package.

Ensure you application is first on the list. This means you can overwrite the standard templates by creating a template with the same filename in your project.

```py
from flask import Flask
from jinja2 import ChoiceLoader, PackageLoader


def create_app():
app = Flask(__name__)

app.jinja_loader = ChoiceLoader(
[
PackageLoader("app"),
PackageLoader("tna_frontend_jinja"),
]
)
```

### Using the templates

```jinja
{% from "components/button/macro.html" import tnaButton -%}
{{ tnaButton({
'text': 'Save and continue'
}) }}
```

The options available to each component macro can be found in the [National Archives Design System Components](https://nationalarchives.github.io/design-system/components/) documentation.

The included templates are a like-for-like port, the only difference between the Nunjucks examples and their Jinja equivalents is having to quote key names, e.g. `'text'` instead of `text`.

We test each component against its published [component fixtures](https://github.com/nationalarchives/tna-frontend/blob/main/src/nationalarchives/components/button/fixtures.json) to ensure complete compatibility.

## Test the templates

```sh
python -m venv venv
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="tna-frontend-jinja",
version="0.1.4",
version="0.1.5",
author="Andrew Hosgood",
author_email="[email protected]",
description="TNA Frontend Jinja templates",
Expand Down
2 changes: 2 additions & 0 deletions test/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ def create_app():
app = Flask(__name__, template_folder="../../tna_frontend_jinja/templates")

from .components import bp as components_bp
from .layouts import bp as layouts_bp

app.register_blueprint(components_bp)
app.register_blueprint(layouts_bp)

return app
5 changes: 5 additions & 0 deletions test/app/layouts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from flask import Blueprint

bp = Blueprint("layouts", __name__)

from test.app.layouts import routes # noqa: E402,F401
8 changes: 8 additions & 0 deletions test/app/layouts/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from test.app.components import bp

from flask import render_template


@bp.route("/")
def base():
return render_template("layouts/base.html")

0 comments on commit bd71b44

Please sign in to comment.