# The nim code
#? stdtmpl(subsChar = '$', metaChar = '#')
#import "../database"
#import user
#import xmltree
proc `$!`(text: string): string = escape(text)
proc renderMain*(body: string): string =
result = ""
return $"""
<!doctype html>
<html>
<head>
<title>Blog Written in Nim</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
${body}
</body>
</html>
"""
proc renderLogin*(): string =
result = ""
return $"""
<div id="login">
<span>Login</span>
<span>Please type in your username...</span>
<form action="login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
</div>
"""
It appears RST is messing up or something. For reference, here the more legible version of this code:
.. code-block:: nim
# The nim code
#? stdtmpl(subsChar = '$', metaChar = '#')
#import "../database"
#import user
#import xmltree
proc `$!`(text: string): string = escape(text)
proc renderMain*(body: string): string =
result = ""
return $"""
<!doctype html>
<html>
<head>
<title>Blog Written in Nim</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
${body}
</body>
</html>
"""
proc renderLogin*(): string =
result = ""
return $"""
<div id="login">
<span>Login</span>
<span>Please type in your username...</span>
<form action="login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
</div>
"""
@isaiah, I'm not sure which part do you need the help with, but I'm pretty sure the code as written doesn't do what you want it to. It's not easy to deduce your intent, so please, tell us what you're trying to achieve.
The code above doesn't really require any real templating as the only substitution I see is the {body} in the first proc. So it could be written as:
const
MainHeader = """
<!doctype html>
<html>
<head>
<title>Blog Written in Nim</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
"""
Footer = """
</body>
</html>
"""
LoginDiv = """
<div id="login">
<span>Login</span>
<span>Please type in your username...</span>
<form action="login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
</div>
"""
func renderMain*(body: string): string =
MainHeader & body & Footer
func renderLogin*(): string =
LoginDiv
echo renderMain(" <h1>Happy holidays!</h1>\n" & renderLogin())
If the Source Code Filters documentation is not doing it for you, here's one more example of using it for basic HTML templating: https://github.com/indiscipline/catalay/blob/main/src/preview.nimf