These are chat archives for synrc/n2o

17th
Apr 2019
Martin Rodriguez
@ixmrm01
Apr 17 18:22
Hello everyone, I am trying to run this project:
https://habr.com/ru/post/273979/
I already updated it with the current version of n2o (at least compiles without errors), the index.htm file is not static, it's a DTL template, then, a blank page appears in the browser if I call it this way:
http://localhost:8001/app/index.htm, or this other way: http://localhost:8001/
So, what is the correct url or what should I fix?
Thank you.
Martin Rodriguez
@ixmrm01
Apr 17 22:06

My routes.erl file:

-module(routes).
-include_lib("n2o/include/n2o.hrl").
-export([init/2, finish/2]).

finish(State, Ctx) -> {ok, State, Ctx}.
init(State, #cx{req=Req}=Cx) ->
Path = case n2ouno:ver() of
cow1 -> n2o_cowboy:path(Req); % cowboy 1.0
cow2 -> #{path:=P}=Req, P % cowboy 2.5
end,
Fix = route_prefix(Path),
n2o:info(?MODULE,"Route: ~p~n",[{Fix,Path}]),
{ok, State, Cx#cx{path=Path,module=Fix}}.

route_prefix(<<"/ws/",P/binary>>) -> route(P);
route_prefix(<<"/",P/binary>>) -> route(P);
route_prefix(P) -> route(P).

route(<<>>) -> index;
route(<<"index",/binary>>) -> index; % github static
route(<<"post",
/binary>>) -> post;
route(<<"app/index",/binary>>) -> index; % priv static
route(<<"app/post",
/binary>>) -> post;
route(_) -> index.

My base.htm file:

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}Erlang blog example{% endblock %}</title>

<!-- Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]> <script src="//oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="//oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]-->
<style>
.container {
max-width: 40em;
}
</style>
</head>
<body>
<div class="container">
{% block content %}{% endblock %}
</div>

<script>{{script}}</script>
<script src='n2o/utf8.js?v=1'></script>
<script src='n2o/bert.js?v=1'></script>
<script src='n2o/heart.js?v=1'></script>
<script src='n2o/ieee754.js?v=1'></script>
<script src='n2o/n2o.js?v=1'></script>
<script>host = location.hostname === 'n2ouno.n2o.space' ? 'ns.synrc.com' : location.hostname;
port = 8001;</script>
<script src='n2o/ftp.js?v=1'></script>
<script src='n2o/nitro.js?v=1'></script>
<script>protos = [$bert]; N2O_start();</script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>

My index.htm file:

{% extends "base.htm" %}
{% block title %}Latest posts{% endblock %}
{% block content %}

<h1>Latest posts</h1>
{{ posts }}
{% endblock %}

My index.erl file:

-module(index).
-compile(export_all).
-include_lib("nitro/include/nitro.hrl").
-include_lib("n2o/include/n2o.hrl").
-include_lib("records.hrl").

posts() -> [

#panel{body=[
    #h2{body = #link{body = P#post.title, url = "/post?id=" ++ nitro:to_list(P#post.id)}},
    #p{body = P#post.text}
  ]} || P <- posts:get()].

main() -> #dtl{file="index", app=n2ouno, ext="htm", bindings=[{posts, posts()}]}.