Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nutrient Levels Templatization #3728

Merged
merged 7 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8800,6 +8800,10 @@ sub display_nutrient_levels($) {

my $html = '';

my $template_data_ref = {
lang => \&lang,
};

# Do not display nutriscore and traffic lights for some categories of products
# do not compute a score for baby foods
if (has_tag($product_ref, "categories", "en:baby-foods")) {
Expand Down Expand Up @@ -8842,9 +8846,11 @@ sub display_nutrient_levels($) {

my $html_nutrition_grade = '';
my $html_nutrient_levels = '';

if ((exists $product_ref->{"nutrition_grade_fr"})
and ($product_ref->{"nutrition_grade_fr"} =~ /^[abcde]$/)) {

$template_data_ref->{nutrition_grade} = "exists";

my $grade = $product_ref->{"nutrition_grade_fr"};
my $uc_grade = uc($grade);

Expand Down Expand Up @@ -8896,6 +8902,7 @@ HTML
;
if (defined $product_ref->{nutriscore_data}) {
$html_nutrition_grade .= display_nutriscore_calculation_details($product_ref->{nutriscore_data});

}
}

Expand All @@ -8904,21 +8911,23 @@ HTML

if ((defined $product_ref->{nutrient_levels}) and (defined $product_ref->{nutrient_levels}{$nid})) {

$html_nutrient_levels .= '<img src="/images/misc/' . $product_ref->{nutrient_levels}{$nid} . '.svg" width="30" height="30" style="vertical-align:middle;margin-right:15px;margin-bottom:4px;" alt="'
. lang($product_ref->{nutrient_levels}{$nid} . "_quantity") . '">' . (sprintf("%.2e", $product_ref->{nutriments}{$nid . $prepared . "_100g"}) + 0.0) . " g "
. sprintf(lang("nutrient_in_quantity"), "<b>" . $Nutriments{$nid}{$lc} . "</b>", lang($product_ref->{nutrient_levels}{$nid} . "_quantity")). "<br>";
$html_nutrient_levels = '1';
push @{$template_data_ref->{nutrient_levels}}, {
nutrient_level => $product_ref->{nutrient_levels}{$nid},
nutriment_prepared => $product_ref->{nutriments}{$nid . $prepared . "_100g"},
areeshatariq marked this conversation as resolved.
Show resolved Hide resolved
nutriment => $Nutriments{$nid}{$lc},
nutriment_prepared => sprintf("%.2e", $product_ref->{nutriments}{$nid . $prepared . "_100g"}) + 0.0,
nutriment_quantity => sprintf(lang("nutrient_in_quantity"), "<b>" . $Nutriments{$nid}{$lc} . "</b>", lang($product_ref->{nutrient_levels}{$nid} . "_quantity")),

};
}
}
if ($html_nutrient_levels ne '') {
$html_nutrient_levels = <<HTML
<h4>$Lang{nutrient_levels_info}{$lc}
<a href="$Lang{nutrient_levels_link}{$lc}" title="$Lang{nutrient_levels_info}{$lc}">@{[ display_icon('info') ]}</a>
</h4>
$html_nutrient_levels
HTML
;
}

my $nutrient_levels_html;

# Nutrient Levels Template

$tt->process('nutrient_levels.tt.html', $template_data_ref, \$nutrient_levels_html) || return "template error: " . $tt->error();

# 2 columns?
if (($html_nutrition_grade ne '') and ($html_nutrient_levels ne '')) {
Expand All @@ -8927,24 +8936,20 @@ HTML
<div class="small-12 xlarge-6 columns">
$html_nutrition_grade
</div>
<div class="small-12 xlarge-6 columns">
$html_nutrient_levels
</div>
$nutrient_levels_html
</div>
HTML
;
}
else {
$html = $html_nutrition_grade . $html_nutrient_levels;
$html = $html_nutrition_grade . $nutrient_levels_html;
}


return $html;
}




sub add_product_nutriment_to_stats($$$) {

my $nutriments_ref = shift;
Expand Down
28 changes: 28 additions & 0 deletions templates/nutrient_levels.tt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!-- Nutrient levels template -->
[% IF (nutrition_grade == "exists") && (nutrient_levels != '') %]
areeshatariq marked this conversation as resolved.
Show resolved Hide resolved
[% IF nutrient_levels != '' %]
<div class="small-12 xlarge-6 columns">
<h4>[% lang('nutrient_levels_info') %]
<a href="[% lang('nutrient_levels_link') %]" title="[% lang('nutrient_levels_info') %]">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon" aria-hidden="true" focusable="false"><path d="M0 0h24v24H0z" fill="none"/><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/></svg>
areeshatariq marked this conversation as resolved.
Show resolved Hide resolved
</a>
</h4>
[% FOREACH level IN nutrient_levels %]
<img src="/images/misc/[% level.nutrient_level %].svg" width="30" height="30" style="vertical-align:middle;margin-right:15px;margin-bottom:4px;"
alt="[% lang('${level.nutrient_level}_quantity') %]"> [% level.nutriment_prepared %] g [% level.nutriment_quantity %] <br>
[% END %]
</div>
[% END %]

[% ELSIF (nutrition_grade != "exists") && (nutrient_levels != '') %]
<h4>[% lang('nutrient_levels_info') %]
<a href="[% lang('nutrient_levels_link') %]" title="[% lang('nutrient_levels_info') %]">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon" aria-hidden="true" focusable="false"><path d="M0 0h24v24H0z" fill="none"/><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/></svg>
</a>
</h4>
[% FOREACH level IN nutrient_levels %]
<img src="/images/misc/[% level.nutrient_level %].svg" width="30" height="30" style="vertical-align:middle;margin-right:15px;margin-bottom:4px;"
alt="[% lang('${level.nutrient_level}_quantity') %]"> [% level.nutriment_prepared %] g [% level.nutriment_quantity %] <br>
[% END %]

[% END %]