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

url_for() equivalent to dynamically pick static files paths #194

Open
DreamDevourer opened this issue Oct 21, 2023 · 1 comment
Open

url_for() equivalent to dynamically pick static files paths #194

DreamDevourer opened this issue Oct 21, 2023 · 1 comment

Comments

@DreamDevourer
Copy link

DreamDevourer commented Oct 21, 2023

This can be a little confusing to understand, so I will give you a case:

I have the following directory structure:

  • @src/
    --static/
    ---styles/
    ----style_base.css
    ----style_module.css
    ----style_template.css
    ---styles_engine.css
    --base.html
    --index.html
    --about/
    ---about-me.html

For context, the "styles_engine.css" file has some {% include "style_*.css" %} tags there to import all the other CSS files.

The use for the url_for() would be great for the "base.html" file, where it is imported to all other HTML templates. In practice, manually importing the "styles_engine.css" file works on the index.html if I do the raw path method, but it breaks on a subdirectory in the case of "about/about-me.html". url_for() would fix the paths dynamically.

Another example of this, is if I use any image or font inside the CSS file, it would break depending on the directory position of the template.

Does anyone have any idea of any alternatives to the url_for()? Currently my turn around is to do semthing like this:

{# This is the global.html #}
{% set stylesEngine = "styles-engine.css" %}
{# This is the base.html #}

{% from '_partials/global.html' import stylesEngine %}

{% if internalDirectory %}
        {# Used for pages/templates inside sub directories #}
        {% set stylesEngine = "../styles-engine.css" %}
{% endif %}

<link href="{{ stylesEngine }}" rel="stylesheet" type="text/css" />

In this case "internalDirectory" is set manually inside the template.

@NickCrews
Copy link
Collaborator

I'm sorry, I don't think I understand your problem still. What would your code look like if this feature was implemented? Is this a jinja2 problem, or a staticjinja problem? Maybe if you link to a repo with the actual code and walk through what is annoying that would help. I'm tempted to say this isn't in the scope of staticjinja, and if you need this feature you should just write a script yourself that uses jinja.

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

2 participants