Closed vaetas closed 3 years ago
Hello, if the option using variableStart: '{{{'
& variableEnd: '}}}'
is not suitable for variables, then you can override Environment.fromString
for wrapping tags in {% raw %}...{{% endraw %}}
:
import 'package:jinja/jinja.dart';
class NewEnvironment extends Environment {
NewEnvironment({Loader? loader})
: regExp = RegExp(r'({{<\s*/?\w+\s*>}})'),
super(loader: loader);
final RegExp regExp;
@override
Template fromString(String source, {String? path}) {
source = source.replaceAllMapped(
regExp, (match) => '{% raw %}${match[1]}{% endraw %}');
return super.fromString(source, path: path);
}
}
void main() {
final env = NewEnvironment();
final tmpl = env.fromString('{{< user >}}Hello {{ name }}!{{< /user >}}');
print(tmpl.render(name: 'Jhon'));
}
Is this suitable?
Thanks, this is perfect!
I've used the custom Environment and it seems to work correctly. Just for clarity, I've slightly edited this regexp to RegExp(r'({{<\s*\/?\w+\s*\/?>}})')
so it also works with trailing slashes.
Thank you again for help.
Hello,
I would like to use the following tags for other functionality:
{{< something >}}
and{{< /something >}}
with content between (opening and closing tag){{< something />}}
However, this library throws the following error:
I see that I can configure
variableStart
andvariableEnd
. This only accepts strings and I'm not sure how to configure this so Jinja only parses tags like{{ abcd... }}
and skips{{< abcd... >}}
without throwing error.Would you have an idea how I could accomplish this behavior?
Thanks!