Commits

Fred T-H  committed 79b77be

Abstracting away titles and dates

This fix makes it so titles and dates for blog entries can be
injected straight in the base template and overwritten on demand.
The values are taken from the blog's index to avoid duplication of
information.

This introduces reserved variables under the name 'meta' that will
be injected by blogerl into the namespace we have.

  • Participants
  • Parent commits 8fd542d

Comments (0)

Files changed (6)

File apps/blogerl/src/blog.erl

 
 compile_files([], _, _) -> [];
 compile_files([A = #article{file=File} | Rest], Vars, Markdown) ->
+    AllVars = [{meta, [{date, format_date(A#article.date)},
+                       {title, A#article.title}]} | Vars],
     MarkdownRequired = fun(Re) ->
         case re:run(File, Re) of
             nomatch -> false;
             {ok, Bin} = file:read_file(File),
             MD = markdown(Bin),
             {ok, tpl} = erlydtl:compile(MD, tpl,
-                         [{vars, Vars}, {doc_root, filename:dirname(File)}]);
+                         [{vars, AllVars}, {doc_root, filename:dirname(File)}]);
         false ->
-            ok = erlydtl:compile(File, tpl, [{vars, Vars}])
+            ok = erlydtl:compile(File, tpl, [{vars, AllVars}])
     end,
     {ok, Text} = tpl:render([]),
     [A#article{text=Text} | compile_files(Rest, Vars, Markdown)].

File demo-blog/index.cfg

 {"Tue, 13 Jul 2010 00:00:00 EDT", "Hello, World", "hello.tpl"}.
 {"Sun, 10 Jul 2011 00:00:00 EST", "Markdown Test", "markdown.md.tpl"}.
-{"Wed, 14 Jul 2010 00:00:00 EST", "Second article", "second.tpl"}.
+{"Wed, 14 Jul 2010 00:00:00 EST", "Second Article", "second.tpl"}.

File demo-blog/src/base.tpl

   <head>
     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
     <link rel="shortcut icon" href="{{ url.img }}favicon.ico" type="image/x-icon"/>
-    <title>Blogerl Demo -> {% block title %}home{% endblock %}</title>
+    <title>Blogerl Demo -> {% block title %}{{ meta.title }}{% endblock %}</title>
     <link rel="stylesheet" href="{{ url.css }}screen.css" media="screen, projection" />
     <link href="{{ url.base }}feed.rss" type="application/rss+xml" rel="alternate" title="ferd.ca newsfeed" />
   </head>
     
     <article>
         {% block date %}
+            <span class="date">{{ meta.date }}</span>
         {% endblock %}
         {% block content %}
         {% endblock %}

File demo-blog/src/index.tpl

 {% extends "base.tpl" %}
 {# index template. Name 'index.tpl' reserved. #}
 
+{% block title %}home{% endblock %}
+{% block date %}{% endblock %}
+
 {% block content %}
 <h2>Oh, Hello There!</h2>
 

File demo-blog/src/markdown.md.tpl

 {% extends "base.tpl" %}
 
-{% block date %}<span class="date">Who cares about dates?</span>{% endblock %}
-{% block title %}Markdown Test{% endblock %}
-
 {% block content %}
 {% markdown %}
 Markdown Test
                 
                 value
 
-Nam adipiscing semper erat, quis faucibus tortor consectetur et. Donec sit amet metus dui, eu varius justo. Morbi vel nulla sollicitudin ante ullamcorper mattis. Aenean ut libero diam, tempor condimentum sem. Integer sit amet leo id arcu facilisis sollicitudin. Cras est lorem, feugiat eget sagittis quis, placerat vel massa. Morbi ac elit tellus. In in lectus libero. Duis tincidunt semper mauris nec pretium. Pellentesque sed dui quis lectus faucibus sollicitudin non non est.
+Nam adipiscing semper <em>erat</em>, quis faucibus tortor consectetur et. Donec sit amet metus dui, eu varius justo. Morbi vel nulla sollicitudin ante ullamcorper mattis. Aenean ut libero diam, tempor condimentum sem. Integer sit amet leo id arcu facilisis sollicitudin. Cras est lorem, feugiat eget sagittis quis, placerat vel massa. Morbi ac elit tellus. In in lectus libero. Duis tincidunt semper mauris nec pretium. Pellentesque sed dui quis lectus faucibus sollicitudin non non est.
 {% endmarkdown %}
 {% endblock %}

File demo-blog/src/second.tpl

 {% extends "base.tpl" %}
 
-{% block date %}<span class="date">2010-08-12</span>{% endblock %}
-{% block title %}Second Article{% endblock %}
-
 {% block content %}
 <h2>Second Article</h2>