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

render_to_string #61

Closed
Eraldo opened this issue May 16, 2016 · 5 comments
Closed

render_to_string #61

Eraldo opened this issue May 16, 2016 · 5 comments

Comments

@Eraldo
Copy link

Eraldo commented May 16, 2016

How can I add css to my header when rendering something to a string that later gets displayed on a template that inherits from base.

Here it would make sense that only at then end when the base template is rendered it also grabs all addtoblock elements and pulls them into the header or something like that.

What do you think? Or do you have alternative ideas on how to handle it?

@ojii
Copy link
Collaborator

ojii commented May 16, 2016

I'm not sure I understand what you're trying to do.

Is this what you're doing (semi-pseudocode)?

def foo():
    context1 = SekizaiContext()
    output = render_to_string('template1.html', context1)
    context2 = SekizaiContext({
        'output': output
    })
    return render_to_string('tempalte2.html', context2)
{# template1.html #}
{% addtoblock "css" %}hoge{% endaddtoblock %}
{# template2.html #}
{% render_block "css" %}
{{ output|safe }}

@Eraldo
Copy link
Author

Eraldo commented Jun 3, 2016

Close..
Difference: Imagine that foo being a template tag that does only use (render) template1.

Example:

@register.simple_tag(takes_context=True)
def label(context, label=None, **kwargs):
    label = label or context.get('label', {})
    label_context = label
    label_context.update(kwargs)
    label_template = 'atoms/label.html'
    return render_to_string(label_template, context=label_context)

So.. the difference is that label (template tag) only renders its template.
And I call the template tag in my main template somewhere.
The mein sekizai block tags are in the main template.

Does it make sense now?

@ojii
Copy link
Collaborator

ojii commented Jun 6, 2016

sekizai cannot work if you isolate the context. You can however manually pass it:

from sekizai.helpers import get_varname

@register.simple_tag(takes_context=True)
def label(context, label=None, **kwargs):
    label = label or context.get('label', {})
    label_context = label
    label_context.update(kwargs)
    label_context[get_varname()] = context[get_varname()] # inherit sekizai context
    label_template = 'atoms/label.html'
    return render_to_string(label_template, context=label_context)

@ojii
Copy link
Collaborator

ojii commented Jun 9, 2016

Closing this as my last comment should explain how to do this.

@ojii ojii closed this as completed Jun 9, 2016
@Eraldo
Copy link
Author

Eraldo commented Jun 9, 2016

Yes that explains it -- thank you @ojii. :)

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