-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial template include support (#38)
- Loading branch information
Showing
11 changed files
with
246 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .includes import IncludesLiveView |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<nav class="navbar"> | ||
<div class="navbar-left"> | ||
<div class="logo"> | ||
{% include "includes/_logo.svg" %} | ||
</div> | ||
<div class="company-name">MyCompany</div> | ||
</div> | ||
<div class="navbar-center"> | ||
{% for page in pages %} | ||
<a data-phx-link="patch" data-phx-link-state="push" href="/includes?page={{page}}" | ||
{% if current_page == page %}aria-current="page" {%endif%}>{{page}}</a> | ||
{% endfor %} | ||
</div> | ||
<div class="navbar-right"> | ||
<div class="user-profile"> | ||
<img src="{{avatar_url}}" alt="User Avatar" class="avatar"> | ||
</div> | ||
</div> | ||
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
body > a:first-of-type { | ||
display: none; | ||
} | ||
|
||
.navbar { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 10px 20px; | ||
background-color: #f0f0f0; | ||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); | ||
} | ||
|
||
.navbar-left { | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
|
||
.company-name { | ||
font-size: 18px; | ||
font-weight: bold; | ||
margin-left: 10px; | ||
color: #333; | ||
} | ||
|
||
.navbar-center { | ||
display: flex; | ||
align-items: center; | ||
gap: 20px; | ||
} | ||
|
||
.navbar-center a { | ||
text-decoration: none; | ||
color: #333; | ||
font-size: 18px; | ||
padding: 5px 10px; | ||
border-radius: 4px; | ||
text-transform: capitalize; | ||
transition: background-color 0.3s ease, color 0.3s ease; | ||
} | ||
|
||
.navbar-center a:hover { | ||
background-color: #f1f1f1; | ||
} | ||
|
||
.navbar-center a[aria-current="page"] { | ||
background-color: #007BFF; | ||
color: #ffffff; | ||
font-weight: bold; | ||
pointer-events: none; | ||
} | ||
|
||
.navbar-right { | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
.user-profile { | ||
display: flex; | ||
align-items: center; | ||
cursor: pointer; | ||
} | ||
|
||
.avatar { | ||
width: 40px; | ||
height: 40px; | ||
border-radius: 50%; | ||
border: 2px solid #ccc; | ||
} | ||
|
||
.content { | ||
padding: 20px; | ||
max-width: 900px; | ||
margin: 20px auto; | ||
background-color: #f0f0f0; | ||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); | ||
border-radius: 8px; | ||
} | ||
|
||
.content h1 { | ||
margin-bottom: 20px; | ||
font-size: 32px; | ||
text-transform: capitalize; | ||
} | ||
|
||
.content p { | ||
margin-bottom: 10px; | ||
} | ||
|
||
code { | ||
background-color: #f5f7fa; | ||
color: #2e3a59; | ||
padding: 1em; | ||
border-radius: 8px; | ||
border: 1px solid #d1d5db; | ||
margin: 1em 0; | ||
display: block; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{% include "includes/_navbar.html" with avatar_url=user.avatar_url %} | ||
|
||
<div class="content"> | ||
<h1>{{current_page}}</h1> | ||
<p> | ||
This is a simple example of how to re-use template code in PyView. | ||
</p> | ||
<p> | ||
This page includes a navbar that is defined in a separate file, and passes the user's avatar as a parameter: | ||
</p> | ||
<code> | ||
{{ '{% include "includes/_navbar.html" with avatar_url=user.avatar_url %}' }} | ||
</code> | ||
<p> | ||
Templates do have access to the parent template context, but it can be useful to have template parameters | ||
(e.g. using a template in a for loop and passing each element as a parameter). | ||
</p> | ||
<p> | ||
You read more about the <a href="http://www.dmulholl.com/docs/ibis/master/tags.html#include">include tag</a> | ||
feature in the template engine documentation. | ||
</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from pyview import LiveView | ||
from dataclasses import dataclass, field | ||
import random | ||
|
||
|
||
@dataclass | ||
class User: | ||
user_id: int = field(default_factory=lambda: random.randint(1, 100)) | ||
|
||
@property | ||
def avatar_url(self): | ||
return f"https://avatar.iran.liara.run/public/{self.user_id}" | ||
|
||
|
||
@dataclass | ||
class IncludesContext: | ||
user: User = field(default_factory=User) | ||
pages: list[str] = field(default_factory=lambda: ["home", "about", "contact"]) | ||
current_page: str = "home" | ||
|
||
|
||
class IncludesLiveView(LiveView): | ||
""" | ||
Template Includes | ||
This example shows how to include templates in other templates. | ||
""" | ||
|
||
async def mount(self, socket, session): | ||
socket.context = IncludesContext() | ||
|
||
async def handle_params(self, url, params, socket): | ||
if "page" in params: | ||
socket.context.current_page = params["page"][0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ packages = [ | |
{ include = "pyview" }, | ||
] | ||
|
||
version = "0.0.21" | ||
version = "0.0.22" | ||
description = "LiveView in Python" | ||
authors = ["Larry Ogrodnek <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from pyview.vendor.ibis import Template | ||
import pyview.vendor.ibis as ibis | ||
from pyview.vendor.ibis.loaders import DictLoader | ||
import pytest | ||
|
||
@pytest.fixture | ||
def template_loader(): | ||
ibis.loader = DictLoader( | ||
{ | ||
"header.html": "<p>{{gr}}, World!</p>", | ||
} | ||
) | ||
|
||
def test_template_include(template_loader): | ||
a = Template("<div>{% include 'header.html' with gr=greeting %}</div>") | ||
d = {"greeting": "Hello"} | ||
|
||
assert a.tree(d) == { | ||
"s": ["<div>", "</div>"], | ||
"0": {"s": ["<p>", ", World!</p>"], "0": "Hello"}, | ||
} | ||
assert a.render(d) == "<div><p>Hello, World!</p></div>" |