Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making layouts #31

Open
lephyrius opened this issue Jul 30, 2020 · 7 comments
Open

Making layouts #31

lephyrius opened this issue Jul 30, 2020 · 7 comments
Labels
Status: Proposal Request for comments Type: Feature New Feature

Comments

@lephyrius
Copy link

I want to have a generic layout for all my pages. How I currently do it is I have a generic struct with title/body values and inject them in the layout template.
I think there should be a bit more sophisticated way of doing it preferably a way to specify a layout.

@Kogia-sima
Copy link
Member

Hello, @lephyrius !

Sailfish is based on EJS, which does not support this feature. However, I think it is better for the sailfish to have this feature for creating simple templates.

Sailfish compiler is properly separated for each stage (lexer, transformer, resolver, optimizer), so the implementation is not so hard. In contrast, its design is not stabilized yet. Could you tell me its use case? I want to stabilize its design based on the use cases.

What I'm so far planning is

  • layout.stpl
<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
  </head>
  <body>
    <section>
      <% yield body %>
    </section>
    <footer>
      <% yield footer %>
    </footer>
  </body>
</html>
  • page.stpl
<% extend!("layout.stpl", body { %>
  <h1>Hello, world!</h1>
<% }, footer { %>
  Copyright &copy; <%= copyrights %>
<% }); %>

If you want to create a layout with the existing version, you need 3 templates.

  • layout.stpl
<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
  </head>
  <body>
    <section>
      <%- body.render_once() %>
    </section>
    <footer>
      <% footer.render_once() %>
    </footer>
  </body>
</html>
  • page-body.stpl
<h1>Hello, world!</h1>
  • page-footer.stpl
Copyright &copy; <%= copyrights %>
  • source code
#[derive(TemplateOnce)]
#[template(path = "layout.stpl")]
struct Layout<T1: TemplateOnce, T2: TemplateOnce> {
  title: String,
  body: T1,
  footer: T2
}

#[derive(TemplateOnce)]
#[template(path = "page-body.stpl")]
struct PageBody;

#[derive(TemplateOnce)]
#[template(path = "page-footer.stpl")]
struct PageFooter {
  copyrights: String
}

let result = Layout {
  title: "page".to_owned(),
  body: PageBody,
  footer: PageFooter {
    copyrights: "2020 Ryohei Machida".to_owned()
  }
}.render_once().unwrap();

@Kogia-sima Kogia-sima added Status: Proposal Request for comments Type: Feature New Feature labels Jul 31, 2020
@lephyrius
Copy link
Author

I really like everything!
I implemented the bottom part like you have suggested.

@gengjun
Copy link

gengjun commented Nov 23, 2023

hi @lephyrius @Kogia-sima, @vthg2themax ,the layout code you talked about above is not working for me. I'm using the latest 0.8.1 release, and with just copy paste your code, I aways got this. Is there anything i need to change ?

error: Rust Syntax Error (unexpected token, expected `;`)
       
       position: line 4, column 62
       
         |
       4 | __sf_rt::render_text!(__sf_buf, "</title>\n  </head>\n  <body>\n    <section>\n      ");
         |                                                              ^
 --> layout.rs:3:10
  |
3 | #[derive(TemplateOnce)]
  |          ^^^^^^^^^^^^
  |
  = note: this error originates in the derive macro `TemplateOnce` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no method named `render_once` found for struct `Layout` in the current scope
  --> layout.rs:29:6
   |
5  |   struct Layout<T1: TemplateOnce, T2: TemplateOnce> {
   |   ------------------------------------------------- method `render_once` not found for this struct
...
22 |       let result = Layout {
   |  __________________-
23 | |         title: "page".to_owned(),
24 | |         body: PageBody,
25 | |         footer: PageFooter {
...  |
28 | |     }
29 | |     .render_once()
   | |     -^^^^^^^^^^^ method not found in `Layout<PageBody, PageFooter>`
   | |_____|
   | 
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `render_once`, perhaps you need to implement it:
           candidate #1: `TemplateOnce`
help: some of the expressions' fields have a method of the same name
   |
29 |     .body.render_once()
   |      +++++
29 |     .footer.render_once()
   |      +++++++

For more information about this error, try `rustc --explain E0599`.
error: could not compile `sailfish-examples` (bin "layout") due to 2 previous errors

@gengjun
Copy link

gengjun commented Nov 24, 2023

The error messages made thought this TemplateOnce trait is not working for generic struct, but turns out just a simple problem
This worked:

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
  </head>
  <body>
    <section>
      <%- body.render_once().unwrap() %>
    </section>
    <footer>
      <% footer.render_once().unwrap() %>
    </footer>
  </body>
</html>

@rocinant3
Copy link

Are there any plans to continue supporting the project with new features? Speed ​​is good, but it would be nice to improve the DX level with new updates

@vthg2themax
Copy link
Collaborator

@rocinant3 Unfortunately I am just maintaining this project currently. I cannot speak for @Kogia-sima however. I am willing to merge any positive changes to the project in accordance with our stated goals, but I am unable to devote very much time to new features. I have 2 kids, and a family which all take priority, and this is an interesting side project. Pull Requests are always welcome though!

@rocinant3
Copy link

@vthg2themax thanks for the answer. I'll start working on a fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Proposal Request for comments Type: Feature New Feature
Projects
None yet
Development

No branches or pull requests

5 participants