From bb7248285a0501987ef28b6dacdd2e3a5ad0eb8d Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 22 Feb 2023 15:39:52 -0500 Subject: [PATCH 1/6] fix: Fixed an issue where the Nutritional Information display didn't take into account the number of serviings ([#66](https://github.com/nystudio107/craft-recipe/issues/66)) --- src/templates/recipe-nutrition-facts.twig | 32 +++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/templates/recipe-nutrition-facts.twig b/src/templates/recipe-nutrition-facts.twig index 81caae1..66269bc 100644 --- a/src/templates/recipe-nutrition-facts.twig +++ b/src/templates/recipe-nutrition-facts.twig @@ -77,6 +77,7 @@ .recipe-plugin-nutrition-label table tr.recipe-plugin-thick-row td { border-top: 4px solid #000; } + .recipe-plugin-nutrition-label table tr.recipe-plugin-micros li:nth-child(1n+0) { padding-right: 8px; } @@ -92,6 +93,7 @@ .recipe-plugin-nutrition-label table tr.recipe-plugin-micros li { border-top: 1px solid #000; } + .recipe-plugin-nutrition-label table tr.recipe-plugin-micros li { box-sizing: border-box; float: left; @@ -110,7 +112,13 @@ {% set result = (numerator * 100) / denomoninator %} {{ result | number_format(0) ~ '%' }} {% endmacro %} -{% from _self import percentage %} +{% from _self import percentage %} + +{% macro servesValue(value, serves) %} + {% set result = value / serves %} + {{- result | number_format(0) -}} +{% endmacro %} +{% from _self import servesValue %}
@@ -134,7 +142,7 @@ {% if value.calories | length %} Calories: - {{ value.calories }} + {{ servesValue(value.calories, value.serves) }} {% endif %} @@ -142,14 +150,14 @@ {% if value.fatContent | length %} - Total Fat: {{ value.fatContent ~ 'g' }} + Total Fat: {{ servesValue(value.fatContent, value.serves) ~ 'g' }} {{ percentage(value.fatContent, rda.fatContent) }} {% endif %} {% if value.saturatedFatContent | length %} - Saturated Fat: {{ value.saturatedFatContent ~ 'g' }} + Saturated Fat: {{ servesValue(value.saturatedFatContent, value.serves) ~ 'g' }} @@ -157,46 +165,48 @@ {% if value.transFatContent | length %} - Trans Fat: {{ value.transFatContent ~ 'g' }} + Trans Fat: {{ servesValue(value.transFatContent, value.serves) ~ 'g' }} {% endif %} {% if value.cholesterolContent | length %} - Cholesterol: {{ value.cholesterolContent ~ 'mg' }} + + Cholesterol: {{ servesValue(value.cholesterolContent, value.serves) ~ 'mg' }} {{ percentage(value.cholesterolContent, rda.cholesterolContent) }} {% endif %} {% if value.sodiumContent | length %} - Sodium: {{ value.sodiumContent ~ 'mg' }} + Sodium: {{ servesValue(value.sodiumContent, value.serves) ~ 'mg' }} {{ percentage(value.sodiumContent, rda.sodiumContent) }} {% endif %} {% if value.carbohydrateContent | length %} - Total Carbohydrate: {{ value.carbohydrateContent ~ 'g' }} + Total + Carbohydrate: {{ servesValue(value.carbohydrateContent, value.serves) ~ 'g' }} {{ percentage(value.carbohydrateContent, rda.carbohydrateContent) }} {% endif %} {% if value.fiberContent | length %} - Dietary Fiber: {{ value.fiberContent ~ 'g' }} + Dietary Fiber: {{ servesValue(value.fiberContent, value.serves) ~ 'g' }} {{ percentage(value.fiberContent, rda.fiberContent) }} {% endif %} {% if value.sugarContent | length %} - Sugars: {{ value.sugarContent ~ 'g' }} + Sugars: {{ servesValue(value.sugarContent, value.serves) ~ 'g' }} {{ percentage(value.sugarContent, rda.sugarContent) }} {% endif %} {% if value.proteinContent | length %} - Protein: {{ value.proteinContent ~ 'g' }} + Protein: {{ servesValue(value.proteinContent, value.serves) ~ 'g' }} {{ percentage(value.proteinContent, rda.proteinContent) }} {% endif %} From 1d0ce759b7cfb6f3fef81e9896dcaca51af52152 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 22 Feb 2023 15:40:19 -0500 Subject: [PATCH 2/6] chore: Version 4.0.7 --- CHANGELOG.md | 4 ++++ composer.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 230a042..886031b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Recipe Changelog +## 4.0.7 - UNRELEASED +### Fixed +* Fixed an issue where the Nutritional Information display didn't take into account the number of serviings ([#66](https://github.com/nystudio107/craft-recipe/issues/66)) + ## 4.0.6 - 2023.02.22 ### Fixed * Fix API Requests ([#65](https://github.com/nystudio107/craft-recipe/pull/65)) diff --git a/composer.json b/composer.json index 0153395..fee6a4d 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft-recipe", "description": "A comprehensive recipe FieldType for Craft CMS that includes metric/imperial conversion, portion calculation, and JSON-LD microdata support", "type": "craft-plugin", - "version": "4.0.6", + "version": "4.0.7", "keywords": [ "craft", "cms", From 818cb1188c7dc0c3ebdcc79b85f5d5b03630921f Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 22 Feb 2023 18:07:28 -0500 Subject: [PATCH 3/6] refactor: Factor serves into percentage of RDA too --- src/templates/recipe-nutrition-facts.twig | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/templates/recipe-nutrition-facts.twig b/src/templates/recipe-nutrition-facts.twig index 66269bc..77ecada 100644 --- a/src/templates/recipe-nutrition-facts.twig +++ b/src/templates/recipe-nutrition-facts.twig @@ -108,8 +108,12 @@ } -{% macro percentage(numerator, denomoninator) %} - {% set result = (numerator * 100) / denomoninator %} + +{% macro percentage(numerator, denomoninator, serves = 1) %} + {% if serves is empty %} + {% set serves = 1 %} + {% endif %} + {% set result = ((numerator / serves) * 100) / denomoninator %} {{ result | number_format(0) ~ '%' }} {% endmacro %} {% from _self import percentage %} @@ -151,7 +155,7 @@ {% if value.fatContent | length %} Total Fat: {{ servesValue(value.fatContent, value.serves) ~ 'g' }} - {{ percentage(value.fatContent, rda.fatContent) }} + {{ percentage(value.fatContent, rda.fatContent, value.serves) }} {% endif %} {% if value.saturatedFatContent | length %} @@ -174,40 +178,41 @@ Cholesterol: {{ servesValue(value.cholesterolContent, value.serves) ~ 'mg' }} - {{ percentage(value.cholesterolContent, rda.cholesterolContent) }} + {{ percentage(value.cholesterolContent, rda.cholesterolContent, value.serves) }} {% endif %} {% if value.sodiumContent | length %} Sodium: {{ servesValue(value.sodiumContent, value.serves) ~ 'mg' }} - {{ percentage(value.sodiumContent, rda.sodiumContent) }} + {{ percentage(value.sodiumContent, rda.sodiumContent, value.serves) }} {% endif %} {% if value.carbohydrateContent | length %} Total Carbohydrate: {{ servesValue(value.carbohydrateContent, value.serves) ~ 'g' }} - {{ percentage(value.carbohydrateContent, rda.carbohydrateContent) }} + {{ percentage(value.carbohydrateContent, rda.carbohydrateContent, value.serves) }} + {% endif %} {% if value.fiberContent | length %} Dietary Fiber: {{ servesValue(value.fiberContent, value.serves) ~ 'g' }} - {{ percentage(value.fiberContent, rda.fiberContent) }} + {{ percentage(value.fiberContent, rda.fiberContent, value.serves) }} {% endif %} {% if value.sugarContent | length %} Sugars: {{ servesValue(value.sugarContent, value.serves) ~ 'g' }} - {{ percentage(value.sugarContent, rda.sugarContent) }} + {{ percentage(value.sugarContent, rda.sugarContent, value.serves) }} {% endif %} {% if value.proteinContent | length %} Protein: {{ servesValue(value.proteinContent, value.serves) ~ 'g' }} - {{ percentage(value.proteinContent, rda.proteinContent) }} + {{ percentage(value.proteinContent, rda.proteinContent, value.serves) }} {% endif %} From 088b9db98a85880ff43a00de580a3f42f4dcee3d Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 22 Feb 2023 19:42:03 -0500 Subject: [PATCH 4/6] refactor: Factor servings into serving size, too --- src/templates/recipe-nutrition-facts.twig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/templates/recipe-nutrition-facts.twig b/src/templates/recipe-nutrition-facts.twig index 77ecada..95e8027 100644 --- a/src/templates/recipe-nutrition-facts.twig +++ b/src/templates/recipe-nutrition-facts.twig @@ -130,7 +130,8 @@

Nutrition Facts

{% if value.servingSize | length %} -
Serving Size: {{ value.servingSize }}
+
Serving + Size: {{ servesValue(value.servingSize, value.serves) }}
{% endif %} {% if value.serves | length %}
Serves: {{ value.serves }}
From 678739ae52c3beb034b282fd4eda705899b560bc Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 23 Feb 2023 14:34:26 -0500 Subject: [PATCH 5/6] refactor: Handle `servingSize` that can be either a number, or a number and a unit as a string --- src/templates/recipe-nutrition-facts.twig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/templates/recipe-nutrition-facts.twig b/src/templates/recipe-nutrition-facts.twig index 95e8027..fe7a172 100644 --- a/src/templates/recipe-nutrition-facts.twig +++ b/src/templates/recipe-nutrition-facts.twig @@ -131,7 +131,9 @@

Nutrition Facts

{% if value.servingSize | length %}
Serving - Size: {{ servesValue(value.servingSize, value.serves) }}
+ {% set servingSizeParts = value.servingSize | split(' ') %} + Size: {{ servesValue(servingSizeParts[0], value.serves) ~ ' ' ~ servingSizeParts[1] ?? '' }} +
{% endif %} {% if value.serves | length %}
Serves: {{ value.serves }}
From 147b2abf9b406ca78ac0cca0a0ff635751b525cd Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 23 Feb 2023 22:31:30 -0500 Subject: [PATCH 6/6] chore: Version 4.0.7 --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 886031b..a1fa57f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Recipe Changelog -## 4.0.7 - UNRELEASED +## 4.0.7 - 2023.02.23 ### Fixed -* Fixed an issue where the Nutritional Information display didn't take into account the number of serviings ([#66](https://github.com/nystudio107/craft-recipe/issues/66)) +* Fixed an issue where the Nutritional Information display didn't take into account the number of servings ([#66](https://github.com/nystudio107/craft-recipe/issues/66)) ## 4.0.6 - 2023.02.22 ### Fixed