Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templex exploration #189

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
a49012c
Add some padding for pattern lab tempalte display
tbrlpld Aug 5, 2023
871877c
Create template not dependent on project
tbrlpld Aug 5, 2023
fd62c1b
Use the decoupled teaser template to render projects
tbrlpld Aug 5, 2023
b085bc5
Move gap margin to conditionally existing element
tbrlpld Aug 5, 2023
12047b4
Demo teaser template directly fed from project
tbrlpld Aug 6, 2023
b7627ec
Add missing props to teaser dataclass
tbrlpld Aug 6, 2023
3c87b22
Move templex class next to template
tbrlpld Aug 6, 2023
eb408c0
Point from class to template
tbrlpld Aug 6, 2023
9b20f00
Fix pattern lab context file
tbrlpld Aug 6, 2023
0c5dfed
Use the templex associated template
tbrlpld Aug 6, 2023
37a2a48
Create include_templex template tag
tbrlpld Aug 6, 2023
37351e9
Remove intermediate teaser include stages
tbrlpld Aug 6, 2023
fe6a783
Replace the custom template tag with a decorator
tbrlpld Aug 14, 2023
119df8c
Use live an public projects instead of all
tbrlpld Sep 9, 2023
0e733c7
Move templex decorator to module
tbrlpld Sep 9, 2023
5bfdecf
Extend comments
tbrlpld Sep 9, 2023
fe2b5df
Create teaser grid templex
tbrlpld Sep 9, 2023
4dbad07
Make debug toolbar work in container
tbrlpld Sep 9, 2023
6beae8f
Bring back the tempalte tag
tbrlpld Sep 10, 2023
64d6161
Avoid recursive resolution of templex to dict
tbrlpld Sep 10, 2023
43a4e94
Update test to recent changes
tbrlpld Sep 10, 2023
21f3679
Update variable name
tbrlpld Sep 10, 2023
06dfd66
Remove the outdated project card templates
tbrlpld Sep 10, 2023
e535e25
Add heading templex
tbrlpld Oct 3, 2023
21abb03
Add template tag to allow extending of context in template
tbrlpld Oct 3, 2023
7c4d69e
Change property name
tbrlpld Oct 3, 2023
398718f
Convert props to context variables
tbrlpld Oct 3, 2023
011059c
Reuse heading templex
tbrlpld Oct 3, 2023
c8482ce
Add type hints to decorator
tbrlpld Oct 5, 2023
2500c5f
Add dataclass decorator to fix type errors
tbrlpld Oct 5, 2023
a45cf20
Create container templex 'section'
tbrlpld Oct 5, 2023
6c78783
Further attempt to get typing to work...
tbrlpld Oct 5, 2023
2ff702a
Use dataclass and abstract base class for templex
tbrlpld Oct 7, 2023
5e30253
Improve error message for required attribute override
tbrlpld Oct 7, 2023
c6f9f24
Fix format
tbrlpld Oct 7, 2023
bb5e4a8
Use immutable
tbrlpld Oct 7, 2023
8be8eaf
Remove useless variable
tbrlpld Oct 7, 2023
17177d8
Use custom initializers to clean up
tbrlpld Oct 7, 2023
4620c89
Remove commented out code
tbrlpld Oct 8, 2023
13d913a
Render non-templex object as strings
tbrlpld Oct 8, 2023
9d5411a
Allow iterables of templexes to be rendered
tbrlpld Oct 8, 2023
a575141
Fix type annotations
tbrlpld Oct 8, 2023
75c5ac4
Pass slippers children through templex tag
tbrlpld Oct 8, 2023
cb15755
Use the prose templex for the introduction
tbrlpld Oct 8, 2023
37cb111
Move code out of init file
tbrlpld Oct 14, 2023
16e819e
Add get_context_data method for flexibility
tbrlpld Oct 14, 2023
8d2b653
Test the signature
tbrlpld Jan 26, 2024
d7e61de
Another empty commit testing the signature
tbrlpld Jan 26, 2024
2143dd3
Another empty commit testing the signature
tbrlpld Jan 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions lpld/home/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from django.apps import apps
import dataclasses

from django.db import models
from django.utils import html as html_utils

from wagtail import fields
from wagtail import images as wagtail_images
from wagtail.admin import panels
from wagtail.templatetags import wagtailcore_tags

from lpld.core import models as core_models
from lpld.templates.atoms.heading import heading
from lpld.templates.molecules import section
from lpld.templates.organisms import prose
from lpld.templates.organisms.teaser_grid import teaser_grid


class HomePage(core_models.BasePage):
Expand All @@ -31,14 +37,35 @@ class HomePage(core_models.BasePage):
def get_context(self, request):
context = super().get_context(request)

ProjectPage = apps.get_model("projects", "ProjectPage")
context["projects"] = ProjectPage.objects.all()
extra_context = {
"title": heading.Heading(text=self.title),
"introduction": prose.Prose(
children=wagtailcore_tags.richtext(self.introduction),
),
"profile_image": self.profile_image,
"projects": self.get_projects_section(),
}

return context
return {**context, **extra_context}

def get_meta_description(self):
def get_meta_description(self) -> str:
return self.search_description or self.get_introduction_without_tags() or ""

def get_introduction_without_tags(self):
def get_introduction_without_tags(self) -> str:
"""Return introduction but without the HTMl tags."""
return html_utils.strip_tags(self.introduction)

def get_projects_section(self) -> section.Section:
return section.Section(
html_id="projects",
html_class="mt-16 lg:mt-32 pt-16 lg:mt-32",
children=[
heading.Heading(
text="These are things I have build before",
level=2,
size="md",
extra_class="max-w-lg lg:max-w-2xl",
),
teaser_grid.TeaserGrid.from_project_pages()
]
)
11 changes: 9 additions & 2 deletions lpld/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@

if DEBUG:
# The internal ips settings is needed to activate the debug toolbar.
INTERNAL_IPS = [
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#configure-internal-ips
# We also need to find the internal IPs when this is running in a container.
import socket # only if you haven't already imported this
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
docker_ips = [ip[: ip.rfind(".")] + ".1" for ip in ips]
local_ips = ["127.0.0.1", "10.0.2.2"]
custom_ips = [
ip.strip() for ip in os.environ.get("INTERNAL_IPS", "").split(",") if ip
]

INTERNAL_IPS = [*docker_ips, *local_ips, *custom_ips]

# Application definition

Expand All @@ -59,6 +65,7 @@
"lpld.navigation",
"lpld.projects",
"lpld.technologies",
"lpld.templex",
"lpld.utils",
"django.contrib.admin",
"django.contrib.auth",
Expand Down
Empty file added lpld/templates/__init__.py
Empty file.
Empty file.
Empty file.
5 changes: 3 additions & 2 deletions lpld/templates/atoms/heading/heading.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load slippers %}
{% load slippers templex %}
{% var level=level|default:"1" %}
{% var size=size|default:"lg" %}
{% spaceless %}
Expand All @@ -17,6 +17,7 @@
{{ extra_class }}
"
>
{{ children }}
{{ text }}
{% templex children %}
</h{{ level }}>
{% endspaceless %}
16 changes: 16 additions & 0 deletions lpld/templates/atoms/heading/heading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import dataclasses
from typing import Union, Iterable

from lpld import templex


@dataclasses.dataclass
class Heading(templex.Templex):
template = "atoms/heading/heading.html"

text: str
level: int = 1
size: str = ""
extra_class: str = ""


4 changes: 3 additions & 1 deletion lpld/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
</head>
<body>
{% block body %}
{{ pattern_library_rendered_pattern }}
<div class="p-12">
{{ pattern_library_rendered_pattern }}
</div>
{% endblock body %}
{{ sentry_settings|json_script:"sentry-settings" }}
</body>
Expand Down
Empty file.
45 changes: 0 additions & 45 deletions lpld/templates/molecules/project-card/project-card.html

This file was deleted.

33 changes: 0 additions & 33 deletions lpld/templates/molecules/project-card/project-card.yaml

This file was deleted.

13 changes: 13 additions & 0 deletions lpld/templates/molecules/section/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import dataclasses

from lpld import templex


@dataclasses.dataclass(frozen=True)
class Section(templex.Templex):
template="molecules/section/section.html"

children: templex.TemplexRenderable
html_id: str = ""
html_class: str = ""

8 changes: 8 additions & 0 deletions lpld/templates/molecules/section/section.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% load templex %}

<section
{% if html_id %}id="{{ html_id }}"{% endif %}
{% if html_class %}class="{{ html_class }}"{% endif %}
>
{% templex children %}
</section>
Empty file.
45 changes: 45 additions & 0 deletions lpld/templates/molecules/teaser/teaser.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{% load l10n wagtailcore_tags wagtailimages_tags %}

<a class="flex flex-row items-start group no-underline" href="{{ href }}">
{% if video or image %}
<div class="mr-4 shrink-0 w-24 sm:w-32 xl:w-48 {% if image_shadow %} px-0.5 sm:px-1 xl:px-2 {% endif %} ">
{% if video %}
<video
src="{{ video.url }}"
width="{{ video.width | unlocalize }}"
height="{{ video.height | unlocalize }}"
class="w-full {% if image_shadow %} rounded shadow-md {% endif %}"
autoplay
loop
>Animation of {{ title }}</video>
{% elif image %}
<picture>
{% image image width-96 as image_fallback %}
{% image_url image "width-96|format-webp" as image_96 %}
{% image_url image "width-128|format-webp" as image_128 %}
{% image_url image "width-192|format-webp" as image_192 %}
{% image_url image "width-256|format-webp" as image_256 %}
{% image_url image "width-384|format-webp" as image_384 %}
<source media="(min-width: 1280px)" srcset="{{ image_192 }}, {{ image_384 }} 2x" type="image/webp">
<source media="(min-width: 640px)" srcset="{{ image_128 }}, {{ image_256 }} 2x" type="image/webp">
<source srcset="{{ image_96 }}, {{ image_192 }} 2x" type="image/webp">
<img
src="{{ image_fallback.url }}"
alt="Screenshot of {{ title }}"
class="w-full {% if image_shadow %} rounded shadow-md {% endif %}"
loading="lazy"
width="{{ image_fallback.width }}"
height="{{ image_fallback.height }}"
/>
</picture>
{% endif %}
</div>
{% endif %}

<div>
<span class="text-neutral-700 group-hover:text-neutral-800 group-active:text-neutral-900">
<h3 class="decoration-3 group-hover:underline underline-offset-2 mb-3 mt-2 text-xl sm:text-2xl font-bold">{{ heading }}</h3>
</span>
<p class="text-sm sm:text-base xl:text-lg text-neutral-500">{{ introduction }}</p>
</div>
</a>
33 changes: 33 additions & 0 deletions lpld/templates/molecules/teaser/teaser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import dataclasses
from typing import Optional, TYPE_CHECKING

from wagtail.images import models as images_models
from wagtailmedia import models as media_models

from lpld import templex

if TYPE_CHECKING:
from lpld.projects import models as projects_models


@dataclasses.dataclass(frozen=True)
class Teaser(templex.Templex):
template="molecules/teaser/teaser.html"

heading: str
introduction: str
href: str
image: Optional[images_models.AbstractImage]
image_shadow: bool
video: Optional[media_models.AbstractMedia]

@classmethod
def from_project_page(cls, project_page: "projects_models.ProjectPage") -> "Teaser":
return cls(
heading=project_page.title,
introduction=project_page.introduction,
href=project_page.get_url(),
image=project_page.image,
image_shadow=project_page.image_shadow,
video=project_page.video,
)
33 changes: 33 additions & 0 deletions lpld/templates/molecules/teaser/teaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
context:
image: True
image_shadow: True
heading: "Remember Me"
introduction: "Developed for fun in my spare time."
href: "https://example.com"

tags:
image:
"image width-96 as image_fallback":
target_var: "image_fallback"
raw:
url: "https://via.placeholder.com/96x120/"
width: 96
height: 120
image_url:
'image "width-96|format-webp" as image_96':
target_var: "image_96"
raw: "https://via.placeholder.com/96x120/"
'image "width-128|format-webp" as image_128':
target_var: "image_128"
raw: "https://via.placeholder.com/128x160/"
'image "width-192|format-webp" as image_192':
target_var: "image_192"
raw: "https://via.placeholder.com/192x240/"
'image "width-256|format-webp" as image_256':
target_var: "image_256"
raw: "https://via.placeholder.com/256x320/"
'image "width-384|format-webp" as image_384':
target_var: "image_384"
raw: "https://via.placeholder.com/384x480/"


Empty file.
10 changes: 10 additions & 0 deletions lpld/templates/organisms/prose/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import dataclasses

from lpld import templex


@dataclasses.dataclass(frozen=True)
class Prose(templex.Templex):
template = "organisms/prose/prose.html"

children: templex.TemplexRenderable
4 changes: 2 additions & 2 deletions lpld/templates/organisms/prose/prose.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% load templex %}
<div class="
prose
xl:prose-xl
Expand All @@ -12,6 +13,5 @@
{% endif %}
{{ extra_class }}
">
{{ children }}
{% templex children %}
</div>

Empty file.
7 changes: 7 additions & 0 deletions lpld/templates/organisms/teaser_grid/teaser-grid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul class="mt-16 grid grid-cols-1 md:grid-cols-2 md:gap-x-12 gap-y-12 sm:gap-y-16 ">
{% for teaser in teasers %}
<li>
{{ teaser.render }}
</li>
{% endfor %}
</ul>
Loading
Loading