retypeapp / retype

Retype is an ✨ ultra-high-performance✨ static site generator that builds a website based on simple text files.
https://retype.com
Other
1.02k stars 201 forks source link

Star Ratings? #561

Closed latenitefilms closed 1 year ago

latenitefilms commented 1 year ago

Is there an existing way in Retype to do nice clean star rating like this?

https://www.w3schools.com/howto/howto_css_star_rating.asp

latenitefilms commented 1 year ago

I ended up solving with an included Markdown file:

<!-- Font Awesome Icon Library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
    .checked {
      color: orange;
    }
</style>
<span class="review-rating">
    <span class="fa fa-star checked"></span>
    <span class="fa fa-star checked"></span>
    <span class="fa fa-star checked"></span>
    <span class="fa fa-star checked"></span>
    <span class="fa fa-star checked"></span>
</span>
geoffreymcgill commented 1 year ago

So this is not for visitors to leave a :star: rating, but for you to just add sequential star icons in your content?

latenitefilms commented 1 year ago

Yes, correct. You can see it in action here:

https://fcp.cafe/workflowextensions/#braw-toolbox

I wanted something that visually looks something like on the Mac App Store.

geoffreymcgill commented 1 year ago

Here are a few samples just using emojis and the included icons:

Rating: :star::star::star:

Rating: :star::star::star: /5

Rating: :star::star::star: of 5

Rating: :icon-star-fill::icon-star-fill::icon-star-fill::icon-star::icon-star:
Screen Shot 2023-06-02 at 3 17 56 PM

Then I tried your Font-Awesome sample with a couple minor changes:

<!-- Font Awesome Icon Library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
    .fa-star {
        font-size: 20px;
        color: #ccc;
    }
    .fa-star.checked {
        color: orange;
    }
</style>

Rating: <span class="fa fa-star checked"></span><span class="fa fa-star checked"></span><span class="fa fa-star checked"></span><span class="fa fa-star"></span><span class="fa fa-star"></span>
Screen Shot 2023-06-02 at 3 19 44 PM

It would be best to put the CSS into the _includes/head.html template file:

<!-- Font Awesome Icon Library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
    .fa-star {
        font-size: 20px;
        color: #ccc;
    }
    .fa-star.checked {
        color: orange;
    }
</style>

Then on the individual pages, you would only need to include the HTML elements:

Rating: <span class="fa fa-star checked"></span><span class="fa fa-star checked"></span><span class="fa fa-star checked"></span><span class="fa fa-star"></span><span class="fa fa-star"></span>

which renders as the following:

Screen Shot 2023-06-02 at 3 27 17 PM

Then I thought, maybe the Retype templating and custom data variables could clean things up.

I added the following to the project retype.yml file:

data:
  star: "<span class=\"fa fa-star checked\"></span>"
  unstar: "<span class=\"fa fa-star\">"

Then in the .md pages, you can simplify to the following:

Rating: {{ star }}{{ star }}{{ star }}{{ unstar }}{{ unstar }}

which renders as the following:

Screen Shot 2023-06-02 at 3 27 17 PM

Then I thought, how about componentizing each of the ratings into its own include. So I made _includes/rating3.md with the following syntax:

{{ star }}{{ star }}{{ star }}{{ unstar }}{{ unstar }}

and in the .md pages, you would add the following include statement:

Rating: {{ include "rating3" }}

which renders as the following:

Screen Shot 2023-06-02 at 3 27 17 PM

You would make one rating include for each rating you would support, such as _includes/rating5.md:

{{ star }}{{ star }}{{ star }}{{ star }}{{ star }}

and include in the page using:

Rating: {{ include "rating5" }}

If using the template data variables and includes, in the future if you change how the HTML or CSS or whatever is configured, then you can easily update the templates, and then all rating components across the project would be instantly updated. Instead of having to update the content of all Pages.

Hope this helps.

latenitefilms commented 1 year ago

Thanks so much for all the information and experimentation!

I did actually already come up with something similar. I have five-stars.md:

https://github.com/CommandPost/FCPCafe/edit/main/docs/_includes/five-stars.md

And I have the CSS in the head.html:

https://github.com/CommandPost/FCPCafe/blob/main/docs/_includes/head.html

I will however "steal" your CSS color and sizing tweaks! Thank you!!

latenitefilms commented 1 year ago

This is where I ended up for anyone who's interested:

https://fcp.cafe/workflowextensions/#user-reviews

latenitefilms commented 1 year ago

@geoffreymcgill - is Retype templating and custom data variables documented anywhere?

Can I use markdown in custom data variables or just HTML?

geoffreymcgill commented 1 year ago

is Retype templating and custom data variables documented anywhere?

Unfortunately, there is still much work to be done with documenting the templating the data variables. It's on my list.

Can I use markdown in custom data variables

Yes, you can use Markdown in the data variables.

The following sample demonstrates:

data:
  sample: "Hello, is **this** working? :rocket:"

sample.md

# sample

{{ sample }}
Screenshot 2023-06-11 at 10 19 28 PM

And you can include the data variables inside of includes:

_includes/my-content.md

## Include

{{ sample }}

sample.md

# sample

{{ include "my-content" }}
Screenshot 2023-06-11 at 10 19 43 PM

Hope this helps.

latenitefilms commented 1 year ago

Legend, thanks for the fast reply! You beat me to even testing it myself! Appreciate it!

latenitefilms commented 1 year ago

Sorry, one more question... can you pass variables in?

For example, can I turn this:

[!badge variant="warning" target="blank" text="IMDB"](https://www.imdb.com/title/tt11881160/)

...into something like this:

{{ imdb url="https://www.imdb.com/title/tt11881160/" }}
geoffreymcgill commented 1 year ago

Yes, you can pass variables into an include. The following sample demonstrates:

sample.md

# Sample

{{ include "imdb" url:"https://www.imdb.com/title/tt11881160/" }}

_includes/imdb.md

[!badge variant="warning" target="blank" text="IMDB"]({{ $.url }})

The above creates the following page:

Screenshot 2023-06-13 at 10 40 35 PM

Hope this helps.