Why Nim π€
Nim is powerful programming language that compiles into C, Cpp, ObjC and JS (what we need).
You can install it here
HappyX β¨
HappyX is an asynchronous web framework, written in Nim.
Source code
We'll work with it.
nimble install happyx
Getting Started π
You should create project. Choose project name and mark project type as SPA
.
hpx create
You should move into project folder after this.
cd PROJECT_NAME
Development π
Let's launch project.
hpx dev --reload
The dev
command will hosts our project at localhost:5000
.
Flag --reload
will checks all changes in project.
Also you can use flags
--host=0.0.0.0
and--port:1234
π‘
Now you can change anything and see result in opened web page! π
Let's change main.nim
. For it you should open src/
folder and open main.nim
file. You will see
import
happyx, # import main library
components/[hello_world] # import components
# Binds single page application to element with id "app"
# In our case - <div id="app"></div>
# Command `hpx create` reproduce all need files
appRoutes("app"):
"/": # Route our app
component HelloWorld # It is a component usage
Try to change "/"
route
"/":
component HelloWorld
tButton: # tButton will reproduce <button> tag
"Click me!" # This will reproduce only text
@click: # This will handle button clicks
# Here you can use real Nim code π‘
# As example you can write in browser console
echo "Clicked!"
OK, save it and see result! π
Component Development π
Move into src/components/
folder. Here automatically created component hello_world.nim
import happyx # Import main library
# component declaration
component HelloWorld:
`template`: # HTML part π
# Here you can write HTML
"Hello, world!"
`script`: # Script part, optionally π
# Here you can use real Nim code π‘
echo "Start coding!"
# Also can be `style` part with component styles π΄
# `style`:
# """
# div {
# background: gray;
# }
# """
That's all! Good Luck! π
Also you can read about API in Nim
Top comments (1)
π₯π₯π₯