djc / askama

Type-safe, compiled Jinja-like templates for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conditional compiling?

b1ek opened this issue · comments

I like to print out debug info like this:

#[cfg(debug_assertions)] {
    log::debug!("debug info here");
}

This helps optimize out the production build, especially with debug-only crates.
Thing is, is it possible to use these macros in templates? There is not a word about that functionality in Template syntax

That's not possible, but you could simply have a macro in the scope of your #[derive(Template)]:

macro_rules! log_debug {
    ($($tt:tt)*) => {{
        #[cfg(debug_assertions)] {
            ::log::debug!($($tt)*);
        }
    }}
}
{% call log_debug!("debug info here") %}

That's not possible, but you could simply have a macro in the scope of your #[derive(Template)]:

macro_rules! log_debug {
    ($($tt:tt)*) => {{
        #[cfg(debug_assertions)] {
            ::log::debug!($($tt)*);
        }
    }}
}
{% call log_debug!("debug info here") %}

I see, that would do pretty much what i described.

However, in the template i would want to print the info into the template, like this:

<!--
  Memory: 0.2/16GB Available
  Chrome memory usage: 14GB
-->
{% cfg(debug_assertions) %}
    blah blah blah
{% endcfg %}

Is that kind of syntax possible? It is not described anywhere in the docs IIRC. The thing is i want that code to be completely excluded from the production build, as it gets rather massive over time

I think this should work:

{% if cfg!(debug_assertions) %}
    blah blah blah
{% endif %}

I think this should work:

{% if cfg!(debug_assertions) %}
    blah blah blah
{% endif %}

Thanks, i will try! Not sure that this will compile out the code tho, but it might work

Yup, that did work and that code is compiled out. Thanks!