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 all 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
53 changes: 27 additions & 26 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8800,6 +8800,11 @@ sub display_nutrient_levels($) {

my $html = '';

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

# 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 @@ -8845,6 +8850,9 @@ sub display_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 +8904,7 @@ HTML
;
if (defined $product_ref->{nutriscore_data}) {
$html_nutrition_grade .= display_nutriscore_calculation_details($product_ref->{nutriscore_data});

}
}

Expand All @@ -8904,47 +8913,39 @@ 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 => 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
;
}

# Nutrient Levels Template
my $nutrient_levels_html;
$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 '')) {
$html = <<HTML
<div class="row">
<div class="small-12 xlarge-6 columns">
$html_nutrition_grade
</div>
<div class="small-12 xlarge-6 columns">
$html_nutrient_levels
</div>
$html .= "<div class='row'>";
if ($html_nutrition_grade ne '') {
$html .= <<HTML
<div class="small-12 xlarge-6 columns">
$html_nutrition_grade
</div>
HTML
;
}
else {
$html = $html_nutrition_grade . $html_nutrient_levels;
if ($html_nutrient_levels ne '') {
$html .= $nutrient_levels_html;
}

$html .= "</div>";

return $html;
}




sub add_product_nutriment_to_stats($$$) {

my $nutriments_ref = shift;
Expand Down
12 changes: 12 additions & 0 deletions templates/nutrient_levels.tt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!-- Nutrient levels template -->
<div class="small-12 xlarge-6 columns">
<h4>[% lang('nutrient_levels_info') %]
<a href="[% lang('nutrient_levels_link') %]" title="[% lang('nutrient_levels_info') %]">
[% display_icon('info') %]
</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>