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

Raw caller() #783

Open
ArmorDarks opened this issue Jun 13, 2016 · 0 comments
Open

Raw caller() #783

ArmorDarks opened this issue Jun 13, 2016 · 0 comments

Comments

@ArmorDarks
Copy link

I've faced quite strange, and a bit nontrivial issue.

it seems that there is no way to output content of caller() as raw (same as {% raw %} block and in same time as rendered.

Sounds a bit weird, so let me just show.

We have a macro, which accepts some code as input, and outputs example, which used as part of codestyle/codeguide, etc.:

{##
 # Display provided code as an example
 #
 # @param {string} title Title of example
 #
 # @return {string} Complex html with example
 #}
{% macro example(title) %}

<p>{{ title }}</p>

{# Here we outputting rendered version, for preview #}
<div>{{ caller() }}</div>

{# And here we're outputting code as it is, so that visitor could see it on page #}
<div>
  <pre><code>{{ caller()|escape }}</code></pre>
</div>

{% endmacro %}

And this worked for html quite well.. However, it obviously won't work if you want to print Nunjucks code as an example.

This

{% call example(title = 'Macros examples') -%}
<p>{{ 'Content of Nunjuks expression' }}</p>
{% endcall %}

will output <p>Content of Nunjuks expression}</p> in <code> while we want to get <p>{{ 'Content of Nunjuks expression' }}</p>.

To solve that issue we have {% raw %} block... but irony is that we can't use it here. If we will wrap caller() with raw block, we will get... yes... {{ caller() }}.

And if we will try to use {% raw %} in {% call %} it won't work too, because we will get raw output in place, where we want to get rendered preview.

Of course there is nothing wrong with it and it's obviously should work that way, but I just can't figure out good way to workaround it.

So far what I've done:

  1. Added {% raw %} block inside{% call %}`

    {% call example(title = 'Macros examples') -%}
    <p>{% raw %}{{ 'Content of Nunjcuks expression' }}{% endraw %}</p>
    {% endcall %}
  2. Wrote special filter, which forces second render of output of caller():

    env.addFilter 'renderCaller', (caller) ->
      nunjucks.renderString(caller.val, this.getVariables());
  3. Used it on caller() with code preview:

    {% macro example(title) %}
    
    <p>{{ title }}</p>
    
    {# Here we outputting rendered version, for preview #}
    <div>{{ caller()|renderCaller }}</div>
    
    {# And here we're outputting code as it is, so that visitor could see it on page #}
    <div>
      <pre><code>{{ caller()|escape }}</code></pre>
    </div>
    
    {% endmacro %}

    I just wonder, maybe someone knows more elegant and not that hacky way to solve that issue?

    Seems like raw filter would be best candidate for that role, but even Jinja doesn't have such. And after I've checked macro's caller() output it started to be clear why — caller getting already compiled content, so you can't output it in raw form from there. Though, I don't know is it just a Nunjucks caller() receives already compiler content, or in Jinja it's done in different way.

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

1 participant