Hugo: How to set dict value by interpolation? - dictionary

In Go (Hugo) templates, how can I set the value of a single entry in a dict?
In this example, I start with a pre-populated dict $vitality and maybe a string .Params.vitality. If the string has anything, I'd like to set the dict entry that the string is the key to to 1.
<figure class="histogram">
{{ $vitality := (dict
"institutional" .Params.count_institutional
"stable" .Params.count_stable
"endangered" .Params.count_endangered
"dying" .Params.count_dying
"extinct" .Params.count_extinct)
}}
{{ if ne .Params.vitality "" }}
{{ $vitality.(.Params.vitality) <!--problem line-->
:= .Params.vitality
}}
{{ end }}
{{ range $key, $value := $vitality }}
<div class="histogram-bar"
data-category="{{ $key }}"
data-count="{{ $value }}">
<div class="histogram-bar-label">{{ $key }}</div>
</div>
{{ end }}
</figure>
This doesn't parse, with Hugo choking on the first . in $vitality.(.Params.vitality). I believe this attempt is called interpolation, and the goal isn't possible without interpolation. Is it possible in Go templates, and if so, how?

It appears you are wanting to add .Params.vitality to the $vitality dict. I think what you want is merge. Your problem line becomes.
$vitality = merge $vitality(dict "vitality" .Params.vitality)

Related

How do I avoid having to use isset() in Laravel Blade templates?

I've got a Blade file like this:
<a href="{{ $url }}" class="{{ $wrapperClass }}">
<p class="{{ $paraClass }}">{{ $title }}</p>
</a>
And call it like this:
<x-mycomponent
url="{{ $book->url }}"
title={{ $book->title }}
wrapper-class=""
para-class = ""
/>
Works great, but whenever I want to add a variable to the component, I need to add this to each file where I call this component.
How can I write it without having to specify all variables and without having to use isset() or {{ $myvar ?? '' }} in Blade? like this:
<x-mycomponent
url="{{ $book->url }}"
title={{ $book->title }}
/>
You can assign default values for your properties like the below's peace of code:
#props([
"product" => null
])
{{ $product }}
and then, you can call your component in both these ways:
<x-mycomponent />
<x-mycomponent product="$product" />
Note: #props is a Blade directive that allows you to pass data from a parent component to a child component.

Handlebars: Using Parent Variable as a Parameter to a Custom Helper Wrapped in an Each Block

Check out ../billerId below. If I put {{ billerId }} before the {{# each }} block, I can see that billerId is there and defined.
I want to user billerId in my custom helper to see if the option's value should be pre-selected.
<select class="searchbar-control col-6 form-control" id="searchbar-select-biller" style="display:none">
<option value=''>Select a biller...</option>
{{# each billers }}
<option value='{{ _id }}' {{ ifEqual _id ../billerId "selected" "" }} >{{ name }}</option>
{{/ each }}
</select>
Here's the helper code:
ifEqual: function (obj, value, trueString, falseString) {
return ( (obj===value) ? trueString : falseString );
},
I have tried various syntaxes: billerId, ../billerId, ../../billerId, {{ billerId }}.
No luck.
Not exactly an answer, but on researching, I don't think Handlebars allows this type of usage.
Instead, I put the billerId in a hidden span and referenced the span by its id from within javascript. I assigned "selected" from within the script.

Timber & WPML string translation

Hi I'm using Timber and what it used to be as simple as
_e('string', 'theme')
to translate strings with WPML, seems to not been working on Timber any idea on how to translate strings?
I have tried the following and nothing is working
{{ _e('string') }} & {{ _('string') }}
{{ _e('string', 'theme') }}
{{ function("icl_translate", 'theme', 'string_identifier', 'string) }}
{{ dump(ICL_LANGUAGE_CODE) }} // Doesn't return anything, so not an option either
Thanks!
Yes I use this;
{{ __('All items', 'theme') }}
And it's working perfectly.
I just did a quick test and was able to get this to work....
Created files with a translation for "thingy" ==> "foobar" in en_US.mo and en_US.po in wp-content/themes/mytheme/languages from there...
single.php
$lang_dir = get_stylesheet_directory().'/languages';
load_theme_textdomain('mytheme', $lang_dir);
Timber::render("single.twig");
single.twig
I like {{ __('thingy', 'mytheme') }}
Output
I like foobar
Try replicating that to see if it works. At least at that point we can isolate things to WPML versus native translate stuff

How can I print Google Books api description?

Hie I am trying to get the synopsis and other items like author and published date printed. But I am Able to achieve this only with certain search terms, an error occurs with other words or terms
Key "description" for array with keys "title, subtitle, authors, publishedDate, industryIdentifiers, readingModes, pageCount, printType, categories, maturityRating, allowAnonLogging, contentVersion, imageLinks, language, previewLink, infoLink, canonicalVolumeLink" does not exist.
I am using symfony and twig. this is what the twig file looks like :
{% for item in items %}
<article>
<img src="{{ item.volumeInfo.imageLinks.thumbnail}}"/>
<h4>{{ item.volumeInfo.title}}</h4>
{{ item.volumeInfo.description }}
<strong> {{ item.volumeInfo.publishedDate }}</strong><br/>
<b>{{ item.volumeInfo.authors | join }}</b>
</article>
What am I doing wrong? why does this work only sometimes ? how can I make it work correctly all the time?
class GoogleBooksController extends Controller
{
public function getVolumeAction($title)
{
$client =new client();
$response = $client- >get("https://www.googleapis.com/books/v1/volumes?q=$title");
$data=$response->json();
$items=$data['items'];
return $this->render('BookReviewBundle:GoogleBooks:volume.html.twig', array('items'=>$items
// ...
)); }
Thanks
I belive the description field is not mandatory, so you can do follow
{% if item.volumeInfo.description is defined %}
{{ item.volumeInfo.description }}
{% endif %}

Symfony2 : Auto htmlentities using Twig

I'm displaying some variable retrieved in my database using Twig :
<p>{{ my_variable }}</p>
The thing is this variable may contain html tags, such as "<br />".
Twig seems to automatically call some htmlentities-like function when displaying variables.
Is there any way to disable it so that when I display a variable containing "Hello<br />world !" I get :
Hello
world !
rather than :
Hello<br />world !
Thanks
Use {{ my_variable|raw }} to prevent my_variable from being automatically escaped.
See Twig documentation: http://twig.sensiolabs.org/doc/filters/raw.html
Try using this
{% autoescape false %}{{ my_variable}}{% endautoescape %}
even better: {{ '<br />|raw('html') }} to avoid unescaping other sensible stuff.
If you just want to use linebreaks in the text stored in your database but don't care to use html , you can also use the nl2br filter as in {{ var|nl2br }}. Allows you to use string linebreak character \n in your text. Filter converts it to <br/>

Resources