How can I test for a literal string in Hugo front matter? - hugo

I want to know if the value of .field is the same as my string literal. What's the right syntax to do that?
{{ if .Params.field matchesText "random"}}
<h1>Success!</h1>
{{ end }}
Obviously, I made up matchesText.
I found a function for containing strings in the Hugo docs, but that's not the same as comparing.

The in function
{{ if in .Params.field "random"}}
<h1>Success!</h1>
{{ end }}
The in function checks if an element is in an array, slice, or substring of a string. In some languages (e.g., Java), you have to worry about some comparisons failing because human-equivalent strings are equivalent in memory. Thankfully, that's not problem with Hugo's in.

Related

Use apply with a method in Hugo

I don't know if the terminology is correct, but essentially it comes down to using something like site.GetPage or path.Base as opposed to something like humanize with apply in Hugo:
I have a part of a layout in Hugo that looks like the following:
{{ range .Params.related_plays }}
{{ with site.GetPage . }}<li>{{.Title}}</li>{{ end }}
{{ end }}
where related_plays is a list of strings containing the path to pieces of content (e.g. ["english/play-1.md", "english/play-2.md"]. This currently works.
Instead of the above, I'd like to assign the list of page objects into a variable. I've tried something like the following:
{{ $related_plays := apply .Params.related_plays "site.GetPage" "." }}
but I get the following error:
error calling apply: can't find function site.GetPath
Is there any way to call apply for something like this?
I'm guessing my alternative is to build up a Scratch slice via the range and with functions, but if it can be done with apply, I would prefer that.

Julia: String interpolation of Array element

I can interpolate a variable into a string to print the value with other text like this:
a = "sheriff";
println("Howdy, I'm the $a.")
Howdy, I'm the sheriff.
I need the syntax for interpolating an array element, I'm currently getting this:
A = ["carb", "sheriff", "mumchance"]
println("Howdy, I'm the $A[2].")
Howdy, I'm the String["carb", "sheriff", "mumchance"][2].
I'm posting this Q/A because I spent too much time looking for this.
println("Howdy, I'm the $(A[2]).")
String interpolation in Julia is similar to unix shells. You can interpolate expressions by wrapping them in parentheses.
Julia Documentation: Strings/Interpolation

React/JSX attrs w/strings vs braces

The docs say that a JSX attributes with strings are the same as attributes with braces...
<Thing attr='val' /> === <Thing attr={'val'} />
I thought I read something that said only use braces when needed because strings are more performant, but I can't find the reference now. Is there an evaluation cost for braces?
No there is no performance difference. Look at the code that's generated by each style:
<div first="abc" second={"def"}/>
// Compiles to:
React.createElement("div", { first: "abc", second: "def" });
Nicer to avoid unneeded braces though.
JSX is actually parsing it to a JS object anyway, so it's either string creation overhead for the parser or string creation overhead in your component. It's trivial either way. With a string literal, though, it's just visually noisy in the code. It only really serves a purpose if the val is a variable or expression.
There is, however, a perfomance hit in a templating engine, such as that employed by the new interpolated strings. E.g.:
`It is a ${'cat'}`
would be slower than:
`It is a dog`
which should be slower still than just a plain literal:
'It is a dog'
I'll leave it as an exercise for the reader to find the interpolation part in the JSX engine. ;) https://github.com/jsx/JSX/tree/master/src
Is there an evaluation cost for braces
Yes, As you can see anything inside {} will be considered as javascript code, so that will be executed , if you just want to assign the string then
just simply use the attr='val' rather than attr={'val'}
but if you have assignment condition based then you can use attr={'val'}
like
attr={ (condition) ? 'val1' : 'val2'}
I Hope , this will clear your thoughts.
For more details :
https://reactjs.org/docs/jsx-in-depth.html
Here you can read behind the scenes of string interpolation and speed performance :
https://koukia.ca/string-interpolation-vs-string-format-string-concat-and-string-builder-performance-benchmarks-c1dad38032a

parse array in laravel blade

my view page consist {{$data->attachment}} which render
[{"filename":"hello.jpg", "location":"/home/my_folder"}].
Here i tried to display file name in view page using
#foreach($data->attachment as $attachment)
$attachment->filename
#endforeach
which gives me
Invalid argument supplied for foreach()
i trying
{{$data->attachment->filename}}
which gives me
Trying to get property of non-object
what i'm doing wrong? how can i display filename? thanks.
As you mentioned in the comments, the value of $data->attachment is actually a string. In order to iterate over it in a loop you will need to convert it back to an array.
So if you change your loop to:
#foreach(json_decode($data->attachment) as $attachment)
{{ $attachment->filename }}
#endforeach
you should get what you want.
I would add that the correct place for this conversion should really be in your controller, not your view.
Your question was not entirely clear so this is all assuming you knew what you were doing when you used a foreach loop, meaning you actually have multiple entries in the $data->attachment array. If it is just the one attachment and you are really just trying to get the filename, then you don't need a loop, all you need is to say:
{{ json_decode($data->attachment)->filename }}
and again I would add that really that all belongs in your controller so that in your view you would end up with something like {{ $filename }}

Iteration of array in Liquid

I'm using an analog of Shopify and I'm stuck with syntax of Liquid.
I need to output in the template the field with an id product[product_field_values_attributes][][value]
So I need to write a loop to get the i value of this array.
I'm confused with this empty element in the brackets.
I've looked through the examples of loop syntax in Liquid but all of those arrays are simple and they are not my case.
For example, the Title field has id product[title] and in Liquid template i call this variable product.title and it works fine.
But all my tries to write a loop for this array failed.
Please, help to write a loop to get the values of the array stated above.
Try outputting the array directly onto the page using {{ product }} or {{ product[product_field_values_attributes] }} somewhere in the HTML. That will do a JSON-like string representation of the array. From there you can figure out what the keys for the array are.
I'm not sure what you're saying about the i value. You don't reference i anywhere else in your question. If you can clarify that then we can see if something can be done about it.

Resources