GRAV cms Twig access to specific array index in theme blueprint yaml - twig

I have a theme I made that hold some configuration value in its config file:
enabled: true
dropdown:
enabled: true
motto: 'il desiderio di coltivare.'
color1: '#0522ff'
color2: '#ff0000'
custom_logo:
user/themes/terretinte/images/ttlogoh.svg:
name: ttlogoh.svg
type: image/svg+xml
size: 3416
path: user/themes/terretinte/images/ttlogoh.svg
to access the custom_logo path I have used in the twig:
<img src="{{config.themes.terretinte.custom_logo|first.path}}" alt="Terre Tinte" width="160" height="38">
My question is:
is this the correct way?
since "config.themes.terretinte.custom_logo" returns an array... couldn't I just specify the index value instead of first? (i.e. in a situation where the value I need is the second)
I've tried:
{{config.themes.terretinte.custom_logo[0].path}} -- doesn't work
{{config.themes.terretinte.custom_logo(0).path}} -- doesn't work
{{config.themes.terretinte.custom_logo|path}} -- doesn't work
{{config.themes.terretinte.custom_logo.path}} -- doesn't work
what would it be the best way to access the specifc key at any position without iterating with a for loop all keys?
Thanks.

Your YAML syntax has actually created subobjects, as opposed to a list. At the time of writing this answer, you have the YAML entry of:
custom_logo:
user/themes/terretinte/images/ttlogoh.svg:
name: ttlogoh.svg
type: image/svg+xml
size: 3416
path: user/themes/terretinte/images/ttlogoh.svg
By using the [] notation, you're trying to access this as an array, but you have not actually created an array. Rather, you've created a sub variable. I'm not entirely sure how well twig handles the / in variable names, but since it isn't throwing an error, it's likely fine. The fix for this will change slightly based on how you want to procede.
Do you really want multiple custom logos in an array?
It seems slightly odd to me that a theme would have multiple logos. If you're only going to use the first, there's no reason to create an array. This is a simple fix.
custom_logo:
name: ttlogoh.svg
type: image/svg+xml
size: 3416
path: user/themes/terretinte/images/ttlogoh.svg
One the line user/themes/terretinte/images/ttlogoh.svg: has been removed, we can now access each of the variables much more easily.
<img src="{{config.themes.terretinte.custom_logo.path}}" alt="Terre Tinte" width="160" height="38">
If you really want multiple logos in an index
As listed above, using a text field with a : creates a named variable. If we simply remove that entire line and replace it with a -, we now have an indexable array. Add as many - as you'd like.
custom_logo:
-
name: ttlogoh.svg
type: image/svg+xml
size: 3416
path: user/themes/terretinte/images/ttlogoh.svg
Do you really want named logos?
I can actually think of a few cases in which you'd want multiple logos. That being said, I'm unsure as to when you'd want to see them in an array, as opposed to named variables. Below is an example of how you could have multiple logos that are named and would then be used in twig.
custom_logo:
normal:
name: ttlogoh.svg
type: image/svg+xml
size: 3416
path: user/themes/terretinte/images/ttlogoh.svg
black_and_white:
name: ttlogoh_bw.svg
type: image/svg+xml
size: 3416
path: user/themes/terretinte/images/ttlogoh_bw.svg
These could then be referenced as:
<img src="{{config.themes.terretinte.custom_logo.normal.path}}" alt="Terre Tinte" width="160" height="38">
<img src="{{config.themes.terretinte.custom_logo.black_and_white.path}}" alt="Terre Tinte" width="160" height="38">

Related

Is there a way to update or merge string literals with kustomize?

I'm trying to manage Argo CD projects with helm definitions using kustomize.
Unfortunately Argo manages helm values with string literals, which gives me headaches in conjunction with kustomize configuration.
I have this base/application.yml
apiVersion: argoproj.io/v1alpha1
kind: Application
source:
chart: something
helm:
values: |
storageClass: cinder-csi
... many more lines identical to every stage
and I'd like to create variants using kustomize overlays, where I'd like to add a single line solely important for the dev stage to the base values.
This is NOT working, it simply replaces the existing base definiton.
overlay/dev/kustomize.yml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patchesJson6902:
- target:
kind: Application
patch: |-
- op: add
path: /source/helm/value
value: "storageSize: 1Gi"
To me it seems kustomize can not append values to string literals. My current solution requires to repeat the whole values string literal in every stage variant, with just a few lines of difference, which heavily violates DRY principles.
Any help is appreciated.
There's an open PR to add support for arbitrary YAML in the values field. If merged, I would expect it to be available in 2.4. Reviews/testing are appreciated if you have time!
One workaround is to use the parameters field and set parameters individually. It's not ideal, but maybe could help until 2.4 is released.

Azure Pipeline Matrix Strategy Variable Expansion problem in conjunction with templates

For often used tasks in azp I created an own repository with a yml file, I'll show you a subpart of that:
create-and-upload-docu.yml:
parameters:
- name: Documentation
type: string
default: ''
- name: Language
type: string
default: ''
- name: ArchiveBaseDir
type: string
default: ''
steps:
- script: |
ARCHIVERELPATH=${{parameters.Documentation}}-${{parameters.Language}}.zip
ARCHIVEDIR=$(echo -n ${{parameters.ArchiveBaseDir}} | sed -e 's#/$##')/${{parameters.Documentation}}/${{parameters.Language}}
echo "##vso[task.setvariable variable=archiveRelPath;isOutput=true]$ARCHIVERELPATH"
echo "##vso[task.setvariable variable=archiveDir;isOutput=true]$ARCHIVEDIR"
name: ${{parameters.Documentation}}_${{parameters.Language}}_params
- task: DeleteFiles#1
inputs:
Contents: '$(Build.ArtifactStagingDirectory)/$(${{parameters.Documentation}}_${{parameters.Language}}_params.archiveRelPath)'
The relevant part is: the "script" has the name which is unique in a job - so I can use this kind of expansion for setting variables within the template:
$(${{parameters.Documentation}}_${{parameters.Language}}_params.archiveRelPath)
This works fine as long as I had called the template with fixed values, like
- template: create-and-upload-docu.yml#templates
parameters:
Documentation: 'adocuvalue'
Language: 'en_US'
ArchiveBaseDir: '$(Build.ArtifactStagingDirectory)/build/'
But now I want to use a matrix to have a few documentations with a few languages:
jobs:
- job: Documentation_CI
displayName: "Docu CI"
timeoutInMinutes: 30
strategy:
matrix:
main_en_US:
Documentation: main
Language: en_US
main_de_AT:
Documentation: main
Language: de_AT
steps:
- checkout: self
- template: create-and-upload-docu.yml#templates
parameters:
Documentation: ${{variables.Documentation}}
Language: ${{variables.Language}}
ArchiveBaseDir: '$(Ws)/build/'
But at the time where ${{}} expressions are expanded, it seems that the matrix variables are not already set; this means that the template script part is called __params and the pipeline has the following error
Publishing build artifacts failed with an error: Input required: ArtifactName
Is there a somewhat simple way to achive what I want (being able to set some variables within templates with a unique naming schema):
can I somehow use ${{ expressions but need a different naming to get to the hard-coded matrix style variables
can I workaround my problem any simple way?
Additional Info: we run a Azure 2020 on prem.
Is there a somewhat simple way to achive what I want (being able to set some variables within templates with a unique naming schema):
Sorry for any inconvenience.
I am afraid there is no such way to resolve this at this moment.
Just as you test, the syntax ${{}} is parsed at compile time. We could not get the value when we use it as name or display name in the task, since it will be parsed at compile time. But the matrix variables have not been set during compilation. That the reason why we get the value _params.
There is a feature request about this. And you could add your request for this feature on our UserVoice site (https://developercommunity.visualstudio.com/content/idea/post.html?space=21 ), which is our main forum for product suggestions:

Dynamically set file path in log4rs

I already asked this question in the Rust subreddit but wanted to ask it here too.
I'm using the log4rs crate and want to find a way to generate more than one log file. I have a YAML file set up with the file appender created, and am trying to have the path be unique so it doesn't have to either append or truncate the original file.
appenders:
file:
kind: file
path: "log/{h({d(%H:%M:%S)})}.log"
But this does not work and gives me this error:
log4rs: error deserializing appender file: The filename, directory name, or volume label syntax is incorrect. (os error 123)
I know that log4rs has a way to do patterns but it doesn't seem to work specifically for the path parameter.
I also saw this other crate called log4rs_routing_appender which looks promising but I don't know if I will need to use that.
Finally, I want to be able to do this non-programmatically (i.e. only with one YAML file), and am wondering if it's possible within log4rs
Thanks a lot!
I do not believe what you want is possible with yaml configuration. However, log4rs provides another way to build it's logger, which is through log4rs::Config::builder():
// get current date
let date = chrono::Utc::now();
// create log file appender
let logfile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::default()))
// set the file name based on the current date
.build(format!("log/{}.log", date))
.unwrap();
// add the logfile appender to the config
let config = Config::builder()
.appender(Appender::builder().build("logfile", Box::new(logfile)))
.build(Root::builder().appender("logfile").build(LevelFilter::Info))
.unwrap();
// init log4rs
log4rs::init_config(config).unwrap();
log::info!("Hello, world!");
Ok(())
I understand that you want to use YAML configuration. However, as you said, patterns do not seem to work with the path variable, seeing as writing this fails:
path:
pattern: "log/requests-{d}-{m}-{n}.log"
Another option would be to manually parse the yaml with serde_yaml (which log4rs actually uses internally) and parse custom variables with regex.
I realize the rolling_file type make it so that it automatically increments numbers to the log names! This is the example of what I did.
appenders:
default:
kind: console
encoder:
kind: pattern
pattern: "{h({d(%H:%M:%S)})} - {m}{n}"
log_file:
kind: rolling_file
append: true
path: "logs/log.log"
encoder:
pattern: "{h({d(%m-%d-%Y %H:%M:%S)})} - {m}{n}"
policy:
kind: compound
trigger:
kind: size
limit: 10mb
roller:
kind: fixed_window
base: 1
count: 100
pattern: "logs/log{}.log"
root:
level: info
appenders:
- default
- log_file
This generates log{}.log (replace {} with incrementing numbers) files within the logs folder after the file reaches 10MB of size. Since I set append: true the log file will keep accumulating until it reaches the size limit.
Hopefully this helps others too!

multiple syntax highlighting in sublimetext 3 with .sublime-syntax file type

I'm using zk framework and I need to do a sublime-syntax file that highlights in XML syntax, but in the zscript snippet uses java syntax highlighting and in style context uses CSS syntax.
This is an example of zk code:
<zk>
<style>
.myClass{
width=300px;
}
</style>
<div id="panel1" class="myClass" visible="true" >
<hlayout width="100px">
<image id="icon1" src="/Desktop/image1.png" width="32px"></image>
<image id="icon2" src="/Desktop/image1.png" width="50px"></image>
</hlayout>
</div>
<zscript><![CDATA[
try{
if (panel1.isVisible()) {
//do something
}
}catch(Exception e){
//catch exception
}
]]></zscript>
</zk>
I saw that some things have been changed recently and the current solutions that are on-line are not very clear, for example at this link I found the following note:
As of Sublime Text Build 3084, a new syntax definition format has been
added, with the .sublime-syntax extension.
It is highly encouraged to be used in favor of the legacy format
described in this document, unless compatibility with older versions
is desired.
Documentation is available here:
http://www.sublimetext.com/docs/3/syntax.html
So I need something like a tutorial on how to build a new multiple syntax file with SublimeText3.
ok I solved my problem by installing PackageDev (Ctrl+Shift+P, select Package Control: Install Package, type and select PackageDev in order to install it), then I've selected: Tools -> Packages -> Package Developement -> New Syntax Definition.
Here I wrote this code:
%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
name: zul
file_extensions:
- zul
scope: text.zul
contexts:
main:
- match: ""
push: "Packages/XML/XML.sublime-syntax"
with_prototype:
- match: '< *zscript *>'
push: Packages/Java/Java.sublime-syntax
with_prototype:
- match: '(?=</ *zscript *>)'
pop: true
- match: '< *script *>'
push: Packages/JavaScript/JavaScript.sublime-syntax
with_prototype:
- match: '(?=</ *script *>)'
pop: true
- match: '< *style *>'
push: Packages/CSS/CSS.sublime-syntax
with_prototype:
- match: '(?=</ *style *>)'
pop: true
File_extensions is a list of extensions that uses this syntax,
scope is source for programming languages and text for markup and everything else.
The match is the regular expression passed to the push element.
with_prototype is something like an exception in the syntax highlighting where you can define a snippet with a different syntax for different contexts
This example uses generally the xml syntax, between the tag <zscript> ... </zscript> uses the java syntax highlighting and in the <style> ... </style> context uses the css syntax.
I saved this file in C:\Users\username\AppData\Roaming\Sublime Text 3\Packages\User and then I founded this syntax file in my View -> Syntax -> User -> zul (the name of the file).

How can I create a new syntax language definition for Sublime Text 3?

I would like to setup a new language definition for log files with a certain format. I've been scouring the interwebs for a tutorial that actually works, and I've yet to find one. I've tried creating sublime-syntax files, AAAPackageDev files (both JSON and YAML), plus one other syntax package that I can't remember the name of -- none of them seem to work, with some of them leaving out important details, like where to save files, how to get the syntax definition to appear as an option (I suspect this is related to save location), or how the keyword.other.joelog scope relates to the keyword.other theme color (do you just drop the last bit? does the last bit have to match the scope set out in the header?). It's been very frustrating.
At its simplest, I'd like to take the following file and highlight it:
2015-11-25 14:35:11 [LOG] Blah Blah Blah
2015-11-25 14:35:11 [LOG] some log statement
2015-11-25 14:35:11 [LOG] some other log statement
2015-11-25 14:35:11 [DEBUG] some embedded filename: [[ /path/to/file ]]
2015-11-25 14:35:11 [INFO] .............. blah blah ..............
2015-11-25 14:35:11 [DEBUG] <<PASS>> Directory not found: [[ /some/dir/name ]]
2015-11-25 14:35:11 [ERROR] <<FAIL>> Directory found: [[ /some/other/dir/name ]]
2015-11-25 14:35:11 [WARNING] some strange condition occurred
Ultimately I'd like syntax definitions similar to the following:
LOG lines should be "normal" text
DEBUG lines should be treated entirely as comments
ERROR lines should be treated as something else (e.g. a variable)
INFO lines as normal except the word INFO should be highlighted as something (e.g. a keyword)
WARNING similar to either ERROR or INFO
have embedded strings in the format of [[ some string ]] highlighted as a string, irrespective of the type of line it's on
have <<PASS>> and <<FAIL>> highlighted with different colors, irrespective of the type of line they're on
I'd be happy getting any of this to work when manually selecting the mode, and especially happy if I could have the mode selected based on the full filename (it would be selected based on a prefix, rather than just a suffix, e.g. joelog-20151126-110719.log keying off of both the 'joelog' prefix and the 'log' suffix).
Is this possible to do? I can write this in JSON, YAML, XML, Martian -- any language or style definition would be fine, if only I knew the rules.
Edit: Sorry, I meant to include the pages I've tried to follow:
http://docs.sublimetext.info/en/latest/extensibility/syntaxdefs.html
http://www.sublimetext.com/docs/3/syntax.html
http://www.sublimetext.com/docs/3/packages.html
https://github.com/SublimeText/AAAPackageDev
https://github.com/SublimeText/AAAPackageDev/blob/master/Syntax%20Definitions/Sublime%20Text%20Syntax%20Def%20%28YAML%29.YAML-tmLanguage
https://packagecontrol.io/packages/SyntaxHighlightTools
With sublime-syntx files I couldn't figure out how to get it to show up as an option. With AAAPackageDev files, I could get my new syntax definition to show up, but it never highlighted anything, even when doing nothing more than following the tutorial (abandoning all of my own desires, just trying to get something to highlight in any fashion).
Thanks!
I started all over again and nothing worked...until I deleted all of my files and attempts, and then started yet again from scratch. Something in those files was preventing other definitions from being read in, maybe? No clue, but now it's working, finally.
The success I had was with PackageDev. Here's what I came up with (it's still in progress):
# [PackageDev] target_format: plist, ext: tmLanguage
---
name: JoeLog
scopeName: source.joelog
fileTypes: [joe]
uuid: 0fb395f8-9fb7-41c2-8b56-51f971de8505
patterns:
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[DEBUG\] (<<PASS>>).*$
name: comment.joelog
captures:
'1': {name: constant.other.symbol.joelog}
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[DEBUG\] (<<FAIL>>).*$
name: variable.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[DEBUG\].*$
name: comment.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[ERROR\].*$
name: variable.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[LOG\].*$
name: support.class.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[INFO\].*$
name: support.function.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[WARNING\].*$
name: keyword.other.joelog
- match: \[WARNING\]
name: keyword.other.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[FATAL\].*$
name: invalid.illegal.joelog
- begin: ^\d+-\d+-\d+ \d+:\d+:\d+ \[STACK TRACE\].*$
end: ^\s*$
name: invalid.illegal.joelog
...
I still don't know half of what I'm doing, but at least something is working. I'd like to be able to color in <<PASS>> and <<FAIL>> without having to duplicate the DEBUG definition, as those strings may appear on other lines, and I don't want to duplicate all of them, but I haven't figured out how to do that, yet (any pointers would be welcome).
Scopes were picked out because of the colors in use in my chosen theme, but that's probably a bad idea, and I should go with scopes that makes sense from a contextual standpoint. But I'll leave that for another day. The TmTheme Editor was a big help in seeing what scopes are defined in my theme and what the actual scope names were.
I used the ApplySyntax package (available via Package Control) to select this new syntax based on the full file name, not just the extension.
Here is a list of commonly used scopes that I compiled by re-engineering some color-themes and using your helpful link to the TmThemeEditor:
# Common generic scopes used in sublime-syntax / color-scheme
comment
comment.line
constant
constant.character
constant.character.escape
constant.language
constant.numeric
constant.other
constant.other.symbol
entity
entity.name.class
entity.name.filename
entity.name.function
entity.name.tag
entity.name.type.class
entity.other.attribute-name
entity.other.inherited-class
invalid
invalid.deprecated
keyword
keyword.control
keyword.control.import
keyword.operator
keyword.other
punctuation
punctuation.definition.string.begin
punctuation.definition.string.end
punctuation.definition.tag
punctuation.definition.tag.begin
punctuation.definition.tag.end
punctuation.definition.variable
storage
storage.modifier
storage.type
storage.type.class
storage.type.function
string
string.regexp
support
support.class
support.constant
support.function
support.other.variable
support.type
variable
variable.function
variable.language
variable.other
variable.parameter

Resources