Originally created by @philippemerle on GitHub (Mar 4, 2024).
I would like to write something like
filename = read("prop:source")
template = import(filename)
// Do something on template.
But evaluating this produces the following error
–– Pkl Error ––
Mismatched input: `filename`. Expected: SLQuote
2 | template = import(filename)
^^^^^^^^
at dynamic_import (file:///.../dynamic_import.pkl, line 2)
Is a way to dynamically import a pkl file?
Originally created by @philippemerle on GitHub (Mar 4, 2024).
I would like to write something like
```
filename = read("prop:source")
template = import(filename)
// Do something on template.
```
But evaluating this produces the following error
```
–– Pkl Error ––
Mismatched input: `filename`. Expected: SLQuote
2 | template = import(filename)
^^^^^^^^
at dynamic_import (file:///.../dynamic_import.pkl, line 2)
```
Is a way to dynamically import a pkl file?
Pkl doesn't support dynamic imports, so you can't do that.
The error message can definitely be improved, though. It's not very clear what the error is.
@stackoverflow commented on GitHub (Mar 4, 2024):
Pkl doesn't support dynamic imports, so you can't do that.
The error message can definitely be improved, though. It's not very clear what the error is.
Note: globbed imports resolve to a mapping where the keys are the expanded form of the glob. So, the provided filename must match something that the glob pattern would expand to.
@bioball commented on GitHub (Mar 4, 2024):
You can emulate "dynamic imports" using [globbed imports](https://pkl-lang.org/main/current/language-reference/index.html#globbed-imports).
For example, you can do:
```groovy
allTemplates = import*("**.pkl")
filename = read("prop:source")
template = allTemplates[filename]
```
Note: globbed imports resolve to a mapping where the keys are the expanded form of the glob. So, the provided `filename` must match something that the glob pattern would expand to.
Thank for the proposal. I found another way with a shell script:
#! /bin/sh
uri="file://$(cd"$(dirname "$1")"&&pwd -P | sed -- 's/ /%20/g')/$(basename "$1")"
pkl eval - <<EOF
import "$uri" as template
// Do something on template.
EOF
@philippemerle commented on GitHub (Mar 4, 2024):
Thank for the proposal. I found another way with a shell script:
```sh
#! /bin/sh
uri="file://$(cd "$(dirname "$1")" && pwd -P | sed -- 's/ /%20/g')/$(basename "$1")"
pkl eval - <<EOF
import "$uri" as template
// Do something on template.
EOF
```
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @philippemerle on GitHub (Mar 4, 2024).
I would like to write something like
But evaluating this produces the following error
Is a way to dynamically import a pkl file?
@stackoverflow commented on GitHub (Mar 4, 2024):
Pkl doesn't support dynamic imports, so you can't do that.
The error message can definitely be improved, though. It's not very clear what the error is.
@philippemerle commented on GitHub (Mar 4, 2024):
Thank you for the explanation. This was I thought well.
@bioball commented on GitHub (Mar 4, 2024):
You can emulate "dynamic imports" using globbed imports.
For example, you can do:
Note: globbed imports resolve to a mapping where the keys are the expanded form of the glob. So, the provided
filenamemust match something that the glob pattern would expand to.@philippemerle commented on GitHub (Mar 4, 2024):
Thank for the proposal. I found another way with a shell script:
@bioball commented on GitHub (Mar 4, 2024):
Closing this, as we do not have any plans to implement this.