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

How do I make use of throw? #2557

Open
Jieiku opened this issue Jun 30, 2024 · 2 comments
Open

How do I make use of throw? #2557

Jieiku opened this issue Jun 30, 2024 · 2 comments

Comments

@Jieiku
Copy link
Contributor

Jieiku commented Jun 30, 2024

Documentation issue

The feature was merged here: Keats/tera#310

The documentation is here: https://keats.github.io/tera/docs/#throw

Summary

In the abridge theme when a user omits a required value in config.toml I would like to throw a specific error.

For instance the Abridge theme requires that compiles_sass is set to true, without it they see this error message:

Error: Failed to build the site
Error: Failed to render pager 1
Error: Reason: Failed to render 'index.html' (error happened in 'base.html').
Error: Reason: Function call 'get_url' failed
Error: Reason: `get_url`: Could not find or open file abridge.css

For a novice I think it would be best to explain exactly why they are seeing this error, which is that they simply need to set compiles_sass to true in config.toml

Here is what I have tried:

  {%- if config.compile_sass %}
    {%- set stylesheets=config.extra.stylesheets | default(value=[ "abridge.css" ]) -%}
    {%- if stylesheets %}{%- for i in stylesheets %}
      <link rel="stylesheet" href="{{     get_url(path=i                               , trailing_slash=false, cachebust=true) | safe }}" />
    {%- endfor %}{%- endif %}
  {%- else %}
    {% throw(message="Error: Theme requires that compile_sass is set to true in config.toml") %}
  {%- endif %}

but it does not seem to work, I get the following error:

zola serve

Building site...
Error: Failed to serve the site
Error: Error parsing templates from the /templates directory
Error: Reason: 
* Failed to parse "/home/jieiku/.dev/test/abridge/templates/partials/head.html"
  --> 25:5
   |
25 |     {% throw(message="Error: Theme requires that compile_sass is set to true in config.toml") %}
   |     ^---
   |
   = unexpected tag; expected an endif tag (`{% endif %}`) or some content

UPDATE: I just found another issue as well. It seems I cannot actually check the value of config.compile_sass from a template so I believe it is not exposed as a value you can check.

Proposed solution

I think it would be good to give a short example of how to use throw() on the tera or zola documentation sites.
I am guessing however that I did this correctly and it simply is not working anymore.
I really appreciate any help with this.

@welpo
Copy link
Contributor

welpo commented Jul 1, 2024

    {% throw(message="Error: Theme requires that compile_sass is set to true in config.toml") %}

throw is an expression, not a statement; you need to use {{ and }}, not {% and %}. This will work:

{{ throw(message="Error: Theme requires that compile_sass is set to true in config.toml") }}

I just found another issue as well. It seems I cannot actually check the value of config.compile_sass from a template so I believe it is not exposed as a value you can check.

I can't access it either (set to true, but ifs return false).

@Keats
Copy link
Collaborator

Keats commented Jul 1, 2024

We are not passing the whole config, see https://github.com/getzola/zola/blob/master/components/config/src/config/mod.rs#L316-L335

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants