diff --git a/EXAMPLES.md b/EXAMPLES.md index 576946a..c58c400 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -1,3 +1,105 @@ +# Examples of blocks usage : +## With language as version +For instance, you could have different version of a widget `jinja2/widgets/navbar.html` : +```jinja +{% macro navbar( + LINK1 = "", TEXT1 = "", + LINK2 = "", TEXT2 = "", + LINK3 = "", TEXT3 = "", +) +%} + +{% endmacro %} +``` + +A block `jinja2/blocks/en/navbar.html` : +```jinja +{% from "widgets/navbar.html" import navbar %} + +{{ navbar( + LINK1 = "/index.html", TEXT1 = "Home", + LINK2 = "/contact.html", TEXT2 = "Contact us", + LINK3 = "/login/connect.html", TEXT3 = "Connexion", + ) }} +``` +A block `jinja2/blocks/fr/navbar.html` : +```jinja +{% from "widgets/navbar.html" import navbar %} + +{{ navbar( + LINK1 = "/index.html", TEXT1 = "Accueil", + LINK2 = "/contact.html", TEXT2 = "Contactez-nous", + LINK3 = "/login/connect.html", TEXT3 = "Connexion", + ) }} +``` + +In `base.html`, we can include a navbar which depends of each page language by : +```jinja +{% from "blocks/block.html" import block %} + +{{ block( + NAME = "navbar", + VERSION = object.metadata.lang|default(""), + DEFAULT_NAME = "en/navbar.html" +) }} +``` +For all pages, if the page contains the metadata `lang` then the block will be searched as `blocks//navbar.html`, and will be `blocks/en/navbar.html` if not found.\ +Else, the `default("")` filter allow to overpass the error and the block will be searched as `blocks/navbar.html`, and will be `blocks/en/navbar.html` if not found.\ + +## With a product as version : +For this example, we have a widget that prints information about a product in `jinja2/widgets/product.html` : +```jinja +{% macro product( + PRODUCT = {"name": "", "description": "", "price":""}, +) +%} +
+

{{ PODUCT.name }}

+

{{ PRODUCT.description }}

+

Buy it for {{ PRODUCT.price }}$ !

+
+{% endmacro %} +``` +With two version of blocks `jinja2/blocks/prod1/product.html` and `jinja2/blocks/prod2/product.html` : +```jinja +{% from "widgets/product.html" import product %} +{{ product( + PRODUCT = + { + "name": "product1" + "description": "..." + "price": "12" + } +) }} +``` +```jinja +{% from "widgets/product.html" import product %} +{{ product( + PRODUCT = + { + "name": "product2" + "description": "..." + "price": "36" + } +) }} +``` +And finally we can use this blocks with : +```jinja +{% from "blocks/block.html" import block %} +{{ block( + NAME = "product" + VERSION = "prod1" +)}} +{{ block( + NAME = "product" + VERSION = "prod2" +)}} +``` + # Exemple for automatic widget inclusion First you can verify that all widgets are found with the `listwidget` command.\ diff --git a/README.md b/README.md index c1317d7..7f76fc1 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,20 @@ This directory is for the static content, like images, CSS and JavaScript files It is accessible in templates by the Jinja or Django `static` function. \ See the [Django doc](https://docs.djangoproject.com/en/5.0/howto/static-files/#configuring-static-files) for static files, or the [Jinja2 version](https://docs.djangoproject.com/en/5.0/topics/templates/#module-django.template.backends.django). +### Blocks or widgets +Widgets are reusable templates used to factor some content in pages. Widgets will be searched in the templates directories, in `/widgets/` + +Blocks are parts of content that can have multiple versions. +They have no parameter as they use widgets when necessary. +It is possible to use the macro `block(NAME, [VERSION], [DEFAULT_NAME])`, to include a bloc and specify its version. In this case, the block will be searched in the templates directories, in `/blocks//` . + +The `VERSION` and `DEFAULT_NAME` arguments of `block()` are optionnal.\ +If no `VERSION` is given, the block will be searched directly in `blocks/`. + +For `VERSION`, you can use page metadata, jinja variables, or just strings. + +For examples, see `EXAMPLES.md`. + ## CLI For each command, the option `-h` give u some help. @@ -114,7 +128,6 @@ For each command, the option `-h` give u some help. `./manage.py make-widgets` to make a file that groups all jinja2 widgets macros for easier includes. It is automatically called by `runserver` and `distill-local` commands. \ See an example in `EXAMPLE.md` - ## Others JFM-Engine is a friendly fork of [JSSG](https://github.com/jtremesay/jssg/) made in agreement with the JSSG developer because of different goals. \