Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/pklPublic

Accessing amended values without declaring locals#940

Answeredbybioball
rohan-datar asked this question inQ&A
Discussion options

I'm trying to create a template with certain constant values that'd I'd like to be accessible to users of the template. Is there a way to do this without creating a local in the amended module?

Here's a very minimal example of what I'm talking about

// template.pklmoduletemplateconstvalue ="some constant value"config:Listing<String>
// config.pklamends"template.pkl"config =newListing{"foo""bar""baz-\(value)"}

Trying to evaluate this returns an error:Cannot find property `value`.

I can sort of overcome this by defining a local property:

// config2.pklamends"template.pkl"localval = valueconfig =newListing{"foo""bar""baz-\(val)"}

However, in more complex scenarios this becomes very inconvenient. When you have several levels of amended templates for instance. Needing to create a local property in every place in the template hierarchy where you need to use the value quickly becomes a problem. Is there a better way to accomplish this?

You must be logged in to vote

You can access this usingmodule:

config {"foo""bar""baz-\(module.value)"}

Replies: 1 comment 5 replies

Comment options

You can access this usingmodule:

config {"foo""bar""baz-\(module.value)"}
You must be logged in to vote
5 replies
@rohan-datar
Comment options

Thanks! That works great. I had no ideamodule was a keyword.

As a follow up, is there a way to do something similar for amended objects?

@bioball
Comment options

Can you elaborate on that? Do you have an example?

@rohan-datar
Comment options

I realized that the amending syntax for objects forces you to give them an identifier in scope, so this isn't an issue.

i.e in

tortoise = (dodo) {name ="Galápagos tortoise"taxonomy {`class` ="Reptilia"   }}

dodo is accesssible inside the definition oftortoise

@bioball
Comment options

Gotcha.

Also, there issuper, which is useful in cases where you have an amends declaration. For example:

tortoise {name ="\(super.name) tortoise"taxonomy {`class` ="Reptilia"   }}

Note thattortoise { ... } is practically the same astortoise = (super.tortoise) { ... }.

@StefMa
Comment options

I found this and I have the feeling my case / question is similar but I don't get it to work.
My template:

something .. somethinglocalbaseHeaderComment ="# Do not modify!\n# This file was generated from a template using https://github.com/StefMa/pkl-gha"headerComment:String?=nulllocal finalHeaderComment:String=if (headerComment!=null)"\(headerComment)\n\n"+ baseHeaderCommentelse baseHeaderCommentoutput {text ="\(finalHeaderComment)\n\n\(super.text)"renderer =newYamlRenderer{converters {      ["headerComment"]= (headerComment:String?)->null    }  }}

My consumercan (don`t have to) override it like this:

amends"../GitHubAction.pkl"headerComment ="""  # SPDX-FileCopyrightText: Copyright line here  #  # SPDX-License-Identifier: License here  """

However, I don't like that I have theheaderComment as a "templating value".
Actually it isn't something for the (in this case) GitHub Action yml directly.
I would be more happy if I could to likemodule.headerComment = "" orsuper.headerComment = "".
Just to scope it a bit away from the "real github action templating".
But it seems that is not possible?

I also thought introducing a class likepklGhaSettings { headerComment = "" }?! 🤔

An advice how to model this would be great.
Maybe having this as a templating property isn't that worth as I think it is?! 🤔

Answer selected byrohan-datar
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@rohan-datar@bioball@StefMa

[8]ページ先頭

©2009-2025 Movatter.jp