glayu icon indicating copy to clipboard operation
glayu copied to clipboard

Working with pages

Open gnucifer opened this issue 7 years ago • 7 comments

Hi! I wrote you in the thread in r/elixir. I'm currently playing around with Glayu (after of half a week of tormenting myself getting up to date with js front-end stuff for creating a new template:)). I've really been enjoying the experience so far, but have a questions about pages. What I'm really after is a very simple primary navigation with a menu item for each page created. I have not given it much more thought, there are probably much better ways to go about this, but is there for example a function for getting pages (with links) so a menu could be generated in the template? I have poked around a bit in posts.ex and build/site_tree.ex but my Elixir knowledge is currently a bit too limited to figure out how to do this (if even possible). Would also be nice if pages where categorizable and could be fetched by category (for creating multiple page menus), but perhaps I am abusing Glayu here and there is another way of achieving this? Anyway, thanks for a really fun project! I am also trying to look into the code base to be able to solve things like this myself, but it will take some time with my limited Elixir experience.

gnucifer avatar Jul 25 '17 18:07 gnucifer

Hi! Thanks for using glayu and looking into the code :) If you have solved the JS part we have to solve the glayu part!

At the moment there isn’t a function for retrieving the site pages. You can get the site posts and category pages, but not the site pages (here is a menu example for the category pages sidebar.eex). You will have to create the menu links manually. I will add it to the current release (0.2.0)) and I will start working on it right now.

Please check the following proposals. There is a basic solution were all pages are listed, and a complex solution were pages are retrieved by category.

Basic Implementation

  • Pages don’t support tags nor categories. (it is how other static sites generators like Jekyll and Hexo work, they support categories on posts but not in pages).
  • provide a pages() function that lists all the site pages.

pages()

Will return all the site pages.

The list of returned plages will have the same structure that posts have. A map like:

%{  
  title: "My Page"
  content: "<p>content as html..</p>",
  date: DateTime.from_naive!(~N[2017-07-24 18:15:20], "Etc/UTC"),
  layout: :page,
  path: "/news/my-page.html",
  raw: "Page content",
  source: "/Users/pablomartinez/Documents/development/workspaces/glayu/glayu/test/fixtures/source/sports/my-page.md"
  ... other front-matter vars
}

The pages menu could be done like:

<ul>
<%= for p <- pages() do %>
  <li>
    <a href="<%= p.path%>"><%= p.title%></a>
  </li>
<% end %>
</ul>

Pages with Category

  • Pages support categories.
  • provide the functions pages() and pages(<category_keys>) where:

pages() list pages under the root folder


pages(<category_keys>)

Will return the pages of a given category.

Argument Description
<category_keys> Ordered list with the category keys (Ex: [news, sports])

A use case:

├── index.html (home.eex layout)
├── help.html (page.eex layout)
├── sitemap.html (page.eex layout)
├── contactus.html (page.eex layout)
├── privacy.html (page.eex layout)
├── world  
|   ├── 2017/06/22/article-1 (article.eex)
|   ├── 2017/06/22/article-2 (article.eex)
|   ...
|   ├── special-page.html (page.eex layout)
|   └── index.html (category.eex)
└── index.html (category.eex)

Assuming a menu like:

Home 
Help 
SiteMap 
Contact Us 
Privacy 
World
World -> Special Page

In the menu we are dealing with category pages and normal pages so the code will look like..

<ul>
<!-- List root pages -->
<%= for page <- pages() do %>
  <li>
    <a href="<%= p.path%>"><%= p.title%></a>
  </li>
<% end %>
<!-- List category pages -->
<%= for category <- categories() do %>
  <li>
    <a href="<%= category.path%>"><%= category.name%></a>
    <ul>
      <%= for cp <- pages(category.keys) do %>
      <li>
        <a href="<%= cp.path%>"><%= cp.title%></a>
      </li>
      <% end %>
    </ul>
    <% end %>
  </li>
<% end %>
</ul>
  • I would like to start implementing the basic one. Do you think it could cover your needs? I need to spent more time thinking on the second one.
  • Could you share some info about your sitemap, or the type of pages you want to create?

About site_tree.ex. You are looking at the right place. It stores the site structure, all the categories with their posts. The process begins at /glayu/tasks/build.ex :

  1. Scans the source folder looking for directories containing .md files. (/glayu/site_analyzers/contain_md_files.ex)
  2. Each directory is processed by a task that parses its files, populates the site_tree and renders the pages. (task_spawner.ex, build_site_tree_and_render_pages.ex)
  3. The site_tree is processed to render the category pages (render_category_pages.ex).
  4. The home page is generated and the assets folder copied to the public dir.

All your feedback and pull-requests are Wellcome!

Thanks again :)

pmartinezalvarez avatar Jul 25 '17 23:07 pmartinezalvarez

Wow, great response, thank you. The first is very much sufficient for me, I tried thinking about hierarchical page-menus and catagorizable pages a bit yesterday but did not manage to quickly come up with an elegant suggestion, but I like your second suggestion, it's much better than what I came up with. I will get back to you with more details, I'm currently not quite sure myself how to structure the site, but the simple "pages" functions would get me very far (don't think I will be needing page hierarchy for example). Hopefully I can start understanding the code better in the next week or so and be able to provide pull requests if I have another feature request.

gnucifer avatar Jul 26 '17 07:07 gnucifer

Great! I will start working on the first one. I think the second one is a little bit tricky from the user perspective. After reviewing how other static site generators deal with pages, I have got back to Wordpress Pages. It enables pages and subpages (by linking a page to a parent page), I think it could be a good approach without adding categories to the pages... I will continue thinking about it.

pmartinezalvarez avatar Jul 26 '17 08:07 pmartinezalvarez

I actually had an idea about pages, inspired by your suggestion, but also perhaps not ideal from user perspective. If creating a page called 'index', and assigning a category for that page. In the category template "category" page.content (and other properties) could be merged in to page in the context for that category, in effect creating page "overlays" for category pages. Then it would also be possible to create hierarchial page structures, and also include posts on "pages".

gnucifer avatar Jul 26 '17 11:07 gnucifer

Good suggestion! a way to override or merge current category pages is required. It could solve it.

pmartinezalvarez avatar Jul 26 '17 12:07 pmartinezalvarez

I have committed the basic solution into the master, I will preserve to issue to deal with the pages hierarchy.

pmartinezalvarez avatar Jul 26 '17 14:07 pmartinezalvarez

Ok, fantastic! Thank you so much!

gnucifer avatar Jul 26 '17 14:07 gnucifer