-
Notifications
You must be signed in to change notification settings - Fork 12
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
Look at template fragments #82
Comments
Any plans to add fragments to Nimja ? |
I think you can use the self variable for this: |
Circling back to this, is there any chance you can show an example of how Nimja can compile a partial template? I am currently using Python for this, with the flask partial library, but I would love to move everything over to Nim/Nimja if possible. Thank you! |
@greenersharp can you show an example what you want to archive? Maybe even with code from your jinja2 code. |
When i look at the fragment library i think you can do the same with nimja. You currently cannot bind a context to So for example: foo
{% for entry in entries %}
{% scope %}
{% let user = entry.user %}
{% let another = entry.another %}
{% importnimja "templates/partials/_oneUser.nimja" %}
{% end %}
{% endfor %}
baa The benefit of using scope is, that you do not pollute the outer template with vars. _oneUser.nimja {# renders one user #}
{{ user.name }}
{{ another }} And if you want to create a "one user" page: oneUser.nimja {% extends some/base.nimja %}
{% block content %}
{# get the user somehow #}
{% let user = getUserFromDb() %}
{% let another = getOtherFromDb() %}
{% importnimja "templates/partials/_oneUser.nimja" %}
{% endblock %} Is this what you need? |
Thanks for the reply! Here is the python library I was referring to: It's commonly used alongside HTMX to render small pieces of a larger template. It really helps cut down the number of template files in a project. |
@greenersharp have you seen my other message? |
ah i see: yeah i think this could actually be nice. |
Awesome! The feature would pair really well with HTMX. |
I am not 100% sure how it should behave though, but i'll have a closer look. |
Signed-off-by: David Krause (enthus1ast) <krause@biochem2.uni-frankfurt.de>
hey @greenersharp if you like you can test the change. edit: I'm still in the process of writing tests for it ( https://github.com/enthus1ast/nimja/blob/d9b7682d91e021608c5353392849d16538dd48ac/tests/basic/test_fragments.nim ) the usage: you call proc foo(blockToRender: static string): string =
compileTemplateStr("""
BUG
{% block first %}first block{% endblock %}
{% block second %}second block{% endblock %}
BUG
""", blockToRender = blockToRender)
check "first block" == foo("first")
check "second block" == foo("second") |
Ah btw, if it works for you, are you interested to write an "real world" example for this? For the example folder? |
Signed-off-by: David Krause (enthus1ast) <krause@biochem2.uni-frankfurt.de>
Great, thank you so much, I'll definitely try and get a simple working example going |
While doing this PR i also played with htmx (for the first time). And i like it. |
If you write even a very simple example SPA, like a to-do app in vuejs or
react, you end up with thousands of lines of boilerplate code.
With HTMX all that boilerplate code is gone. It's really refreshing.
And the nimja/htmx combo might very well be the fastest out there.
I'm traveling at the moment but I'll definitely work on the parital example
!
…On Mon, Sep 16, 2024, 8:32 PM David Krause ***@***.***> wrote:
While doing this PR i also played with htmx (for the first time). And i
like it.
—
Reply to this email directly, view it on GitHub
<#82 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AZABNLWOQVR4FY4ZSUSXXKDZW4P5XAVCNFSM6AAAAAAQME5FPGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNJTGYZDQNRRGQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
It works amazingly well. Thank you so much for adding this feature! I'm new to nim and still learning, but does 'blocktorender' need to be a static string? it would be great if it could be mutable if possible. Imagine a use case where the rendered block (if any) is conditional on a url query parameter, something like this:
This way I only need to write Right now my code looks like this:
Thank you |
Yes it's a static string, I don't think there is a way around this unfortunately. It needs to compile a fresh proc for you when you change the parameters. Then on runtime it's fast. Nevertheless, I'll look into this problem. I can imagine that a case statement could work, where you list all the routes explicitly. Edit: and/or you could create a macro or template, that create the different routes for you. |
https://htmx.org/essays/template-fragments/
The text was updated successfully, but these errors were encountered: