Replies: 12 comments 27 replies
-
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply, just got some basics working, when I Use it with nimja template engine (Docs)
edit the index.html file
run it works now, but the page looks very ugly, how can I use tailwindcss with happyx? |
Beta Was this translation helpful? Give feedback.
-
You can use it now via cdn. I think about usage without cdn later. |
Beta Was this translation helpful? Give feedback.
-
You can do this way: hpx create --name:htmx_example --kind:SSR --templates --language:Nim
cd htmx_example
npm init
npm install -D tailwindcss
npx tailwindcss init After this you should change /** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,htmx,nim,js}"],
theme: {
extend: {},
},
plugins: [],
} Next step is create @tailwind base;
@tailwind components;
@tailwind utilities; Say tailwind to watch this: npx tailwindcss -i ./src/public/input.css -o ./src/public/output.css --watch Last step is use <!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>{{ title }}</title>
<script src="https://unpkg.com/[email protected]"></script>
<link href="/public/output.css" rel="stylesheet">
</head>
<body>
You at {{ title }} page ✨
<button
hx-get="https://v2.jokeapi.dev/joke/Any?format=txt&safe-mode"
hx-target="#output"
class="text-2xl rounded-md bg-red-400 hover:bg-red-500 active:bg-red-600 duration-300"
>Click for Joke</button>
<p id="output"></p>
</body>
</html> And run it: cd src
nim c -r main
Also you can use this way for Python/NodeJS HappyX projects or SPA/HPX project types |
Beta Was this translation helpful? Give feedback.
-
thanks for the help! now I have a tailwindcss running example. <!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>{{ title }}</title>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://cdn.tailwindcss.com"></script>
<!-- <link href="/public/output.css" rel="stylesheet"> -->
</head>
<body>
<main class="grid min-h-full place-items-center bg-white px-6 py-24 sm:py-32 lg:px-8">
<div class="text-center">
{% if title == "Ethosa" %}
<p class="text-3xl font-semibold text-indigo-600"> welcome {{ title }}</p>
{% else %}
<p class="text-base font-semibold text-red-600">{{ title }}</p>
{% endif %}
<h1 class="mt-4 text-3xl font-bold tracking-tight text-gray-900 sm:text-5xl">Page for a Joke</h1>
<p id="joke" class="mt-6 text-base leading-7 text-gray-600">The easiest way to go from htmx to a Joke. </p>
<div class="mt-10 flex items-center justify-center gap-x-6">
<button
hx-get="https://v2.jokeapi.dev/joke/Any?format=txt&safe-mode"
hx-target="#joke"
class="rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
Get a Joke
</button>
</div>
</div>
</main>
</body>
</html> but I still have a question, if i want to archive the same result with |
Beta Was this translation helpful? Give feedback.
-
With serve "127.0.0.1", 5000:
# at http://127.0.0.1:5000/my/nice/ANYTHING_STRING_HERE
get "/my/nice/{title:string}":
# Some code in Nim here
echo "blabla"
return buildHtml:
tHtml:
tHead:
tMeta(charset = "utf-8")
tTitle: { title }
tScript(src = "https://unpkg.com/[email protected]")
tScript(src = "https://cdn.tailwindcss.com")
tBody:
tMain(class = "grid min-h-full place-items-center bg-white px-6 py-24 sm:py-32 lg:px-8"):
if title == "Ethosa":
tP(class = "text-3xl font-semibold text-indigo-600"):
"Welcome, {title}"
else:
tP(class = "text-base font-semibold text-red-600"):
{title}
tH1(class = "mt-4 text-3xl font-bold tracking-tight text-gray-900 sm:text-5xl"):
"Page for a Joke"
tP(id = "joke", class = "mt-6 text-base leading-7 text-gray-600"):
"The easiest way to go from htmx to a Joke."
tDiv(class = "mt-10 flex items-center justify-center gap-x-6"):
tButton(
"hx-get" = "https://v2.jokeapi.dev/joke/Any?format=txt&safe-mode",
"hx-target" = "#joke",
class="rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
"Get a Joke" |
Beta Was this translation helpful? Give feedback.
-
BonusYou can create file named import happyx
proc jokePage(title: string): TagRef =
return buildHtml:
tHtml:
tHead:
tMeta(charset = "utf-8")
tTitle: { title }
tScript(src = "https://unpkg.com/[email protected]")
tScript(src = "https://cdn.tailwindcss.com")
tBody:
tMain(class = "grid min-h-full place-items-center bg-white px-6 py-24 sm:py-32 lg:px-8"):
if title == "Ethosa":
tP(class = "text-3xl font-semibold text-indigo-600"):
"Welcome, {title}"
else:
tP(class = "text-base font-semibold text-red-600"):
{title}
tH1(class = "mt-4 text-3xl font-bold tracking-tight text-gray-900 sm:text-5xl"):
"Page for a Joke"
tP(id = "joke", class = "mt-6 text-base leading-7 text-gray-600"):
"The easiest way to go from htmx to a Joke."
tDiv(class = "mt-10 flex items-center justify-center gap-x-6"):
tButton(
"hx-get" = "https://v2.jokeapi.dev/joke/Any?format=txt&safe-mode",
"hx-target" = "#joke",
class="rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
"Get a Joke" And use it in import happyx
import templates
serve "127.0.0.1", 5000:
get "/my/nice/{title:string}":
return jokePage(title) |
Beta Was this translation helpful? Give feedback.
-
it works! but when I try to abstract some component like the htmx button, it has some problem, import happyx
proc htmxButton(get_url: string, target_id: string, name: string): TagRef =
return tButton(
"hx-get" = get_url,
"hx-target" = target_id,
class="rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
name
proc jokePage*(title: string): TagRef =
return buildHtml:
tHtml:
tHead:
tMeta(charset = "utf-8")
tTitle: { title }
tScript(src = "https://unpkg.com/[email protected]")
tScript(src = "https://cdn.tailwindcss.com")
tBody:
tMain(class = "grid min-h-full place-items-center bg-white px-6 py-24 sm:py-32 lg:px-8"):
tDiv(class="text-center"):
if title == "Ethosa":
tP(class = "text-3xl font-semibold text-indigo-600"):
"Welcome, {title}"
else:
tP(class = "text-base font-semibold text-red-600"):
{title}
tH1(class = "mt-4 text-3xl font-bold tracking-tight text-gray-900 sm:text-5xl"):
"Page for a Joke"
tP(id = "joke", class = "mt-6 text-base leading-7 text-gray-600"):
"The easiest way to go from htmx to a Joke."
tDiv(class = "mt-10 flex items-center justify-center gap-x-6"):
# try to call proc htmxButton(), but it failed
tButton(
"hx-get" = "https://v2.jokeapi.dev/joke/Any?format=txt&safe-mode",
"hx-target" = "#joke",
class="rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
"Get a Joke" also tried this, but no luck. template htmxButton(get_url: string, target_id: string, name: string) =
tButton(
"hx-get" = get_url,
"hx-target" = target_id,
class="rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
name |
Beta Was this translation helpful? Give feedback.
-
@jerrygzy anyway you should use proc htmxButton(get_url: string, target_id: string, name: string): TagRef =
return buildHtml:
tButton(
"hx-get" = get_url,
"hx-target" = target_id,
class="rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
{name} # you should use {} for non-strings statements also And usage is: tDiv(class = "mt-10 flex items-center justify-center gap-x-6"):
{htmxButton("something_url", "something_id", "something_title")} Bonus
In SSR, like in SPA, you can use components to solve this issue. But SSR don't allow to use component HtmxButton:
get_url: string
target_id: string
name: string
`template`:
tButton(
"hx-get" = self.get_url,
"hx-target" = self.target_id,
class="rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
{self.name} And usage is: tDiv(class = "mt-10 flex items-center justify-center gap-x-6"):
HtmxButton("something_url", "something_id", "something_title") |
Beta Was this translation helpful? Give feedback.
-
the proc is working nicely again, but the component has some errors. import happyx
proc htmxJokeWidget(get_url: string, name: string): TagRef =
return buildHtml:
tDiv(class = "mt-6 flex-col items-center justify-center gap-x-6"):
tP(id = "joke", class = "mt-6 text-base leading-7 text-gray-600"):
"The easiest way to go from htmx to a Joke."
tButton(
"hx-get" = get_url,
"hx-target" = "#joke",
class="mt-6 rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
{name} # you should use {} for non-strings statements also
component HtmxButton:
get_url: string
target_id: string
name: string
`template`:
tButton(
"hx-get" = self.get_url,
"hx-target" = self.target_id,
class="rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
{self.name}
proc jokeWidget(name: string): TagRef =
return buildHtml:
tDiv(class = "mt-6 flex-col items-center justify-center gap-x-6"):
tP(id = "joke", class = "mt-6 text-base leading-7 text-gray-600"):
"The easiest way to go from htmx to a Joke."
HtmxButton("https://v2.jokeapi.dev/joke/Any?format=txt&safe-mode", "#joke", name)
proc jokePage*(title: string): TagRef =
return buildHtml:
tHtml:
tHead:
tMeta(charset = "utf-8")
tTitle: { title }
tScript(src = "https://unpkg.com/[email protected]")
tScript(src = "https://cdn.tailwindcss.com")
tBody:
tMain(class = "grid min-h-full place-items-center bg-white px-6 py-24 sm:py-32 lg:px-8"):
tDiv(class="text-center"):
if title == "Ethosa":
tP(class = "text-3xl font-semibold text-indigo-600"):
"Welcome, {title}"
else:
tP(class = "text-base font-semibold text-red-600"):
{title}
tH1(class = "mt-4 text-3xl font-bold tracking-tight text-gray-900 sm:text-5xl"):
"Page for a Joke"
#{htmxJokeWidget("https://v2.jokeapi.dev/joke/Any?format=txt&safe-mode","Get a Joke")}
{jokeWidget("Another Joke")}
the code above is using component HtmxButton in jokeWidget, and the error msg is
|
Beta Was this translation helpful? Give feedback.
-
now I have this abstracted code. but reusing it seems to have a bug. below are my code files and cmds I used.
import happyx
import std/[random,strutils]
import ../htmx/comp
proc header(title: string): TagRef =
return buildHtml:
tHead:
tMeta(charset = "utf-8")
tTitle: { title }
tScript(src = "https://unpkg.com/[email protected]")
tScript(src = "https://cdn.tailwindcss.com")
proc jokeWidget(name: string): TagRef =
const joke_url = "https://v2.jokeapi.dev/joke/Any?format=txt&safe-mode"
let joke_id = name.replace(" ","_") & $rand(100)
return buildHtml:
tDiv(class = "mt-6 flex-col items-center justify-center gap-x-6"):
tP(id = joke_id, class = "mt-6 text-base leading-7 text-gray-600"):
"The easiest way to go from htmx to a Joke."
HtmxButton(joke_url, "#" & joke_id, name)
proc jokePage*(title: string): TagRef =
return buildHtml:
tHtml:
{header(title)}
tBody:
tMain(class = "grid min-h-full place-items-center bg-white px-6 py-24 sm:py-32 lg:px-8"):
tDiv(class="text-center"):
if title == "Ethosa":
tP(class = "text-3xl font-semibold text-indigo-600"):
"Welcome, {title}"
else:
tP(class = "text-base font-semibold text-red-600"):
{title}
tH1(class = "mt-4 text-3xl font-bold tracking-tight text-gray-900 sm:text-5xl"):
"Page for a Joke"
#{htmxJokeWidget("https://v2.jokeapi.dev/joke/Any?format=txt&safe-mode","Get a Joke")}
{jokeWidget("Get a Joke")}
{jokeWidget("Another Joke")}
import happyx
component HtmxButton:
get_url: string
target_id: string
name: string
`template`:
tButton(
"hx-get" = self.get_url,
"hx-target" = self.target_id,
class="mt-6 rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
{self.name}
# Import HappyX
import
happyx
import
templates/index
# SQL
const Schema = "CREATE TABLE IF NOT EXISTS Example(string TEXT NOT NULL)"
# Declare template folder
templateFolder("templates")
proc render(title: string): string =
## Renders template and returns HTML string
##
## `title` is template argument
renderTemplate("index.html")
# Serve at http://127.0.0.1:5000
serve("127.0.0.1", 5000):
# on GET HTTP method at http://127.0.0.1:5000/TEXT
get "/{title:string}":
#req.answerHtml render(title)
{.gcsafe.}:
return jokePage(title)
# on any HTTP method at http://127.0.0.1:5000/public/path/to/file.ext
staticDir "public"
Result |
Beta Was this translation helpful? Give feedback.
-
finally got 3.1.0 installed, but reusing component is still not working.
import happyx
import std/[random,strutils]
#import ../htmx/comp
proc header(title: string): TagRef =
return buildHtml:
tHead:
tMeta(charset = "utf-8")
tTitle: { title }
tScript(src = "https://unpkg.com/[email protected]")
tScript(src = "https://cdn.tailwindcss.com")
component HtmxButton:
get_url: string
target_id: string
name: string
`template`:
tButton(
"hx-get" = self.get_url,
"hx-target" = self.target_id,
class="mt-6 rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
):
{self.name}
const joke_url = "https://v2.jokeapi.dev/joke/Any?format=txt&safe-mode"
component JokeWidget:
name: string
`template`:
nim:
let joke_id = self.name.replace(" ","_") #& $rand(100)
tDiv(class = "mt-6 flex-col items-center justify-center gap-x-6"):
tP(id = joke_id, class = "mt-6 text-base leading-7 text-gray-600"):
"The easiest way to go from htmx to a Joke."
HtmxButton(joke_url, "#" & joke_id, self.name)
proc jokePage*(title: string): TagRef =
return buildHtml:
tHtml:
{header(title)}
tBody:
tMain(class = "grid min-h-full place-items-center bg-white px-6 py-24 sm:py-32 lg:px-8"):
tDiv(class="text-center"):
if title == "Ethosa":
tP(class = "text-3xl font-semibold text-indigo-600"):
"Welcome, {title} !"
else:
tP(class = "text-base font-semibold text-red-600"):
{title}
tH1(class = "mt-4 text-3xl font-bold tracking-tight text-gray-900 sm:text-5xl"):
"Page for a Joke"
jokeWidget("Get a Joke")
jokeWidget("Another Joke")
Result after cmd |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe. 🤔
I am trying to use htmx for a SSR project, there is a old example using karax, since happyx is not using karax, what is the right way of using htmx in my project?
Describe the solution you'd like 💡
A tutorial for the happyx documentation will be great.
Beta Was this translation helpful? Give feedback.
All reactions