diff --git a/CHANGELOG.md b/CHANGELOG.md index f1c4301..5d9be62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Recipe Changelog +## 1.4.4 - 2023.02.23 +### Fixed +* 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)) + ## 1.4.3 - 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 2da43b4..4c4124a 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": "1.4.3", + "version": "1.4.4", "keywords": [ "craft", "cms", diff --git a/src/templates/recipe-nutrition-facts.twig b/src/templates/recipe-nutrition-facts.twig index 81caae1..4c8e13c 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; @@ -106,11 +108,20 @@ } -{% 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 %} +{% from _self import percentage %} + +{% macro servesValue(value, serves) %} + {% set result = value / serves %} + {{- result | number_format(0) -}} +{% endmacro %} +{% from _self import servesValue %}
@@ -118,7 +129,10 @@

Nutrition Facts

{% if value.servingSize | length %} -
Serving Size: {{ value.servingSize }}
+
Serving + {% set servingSizeParts = value.servingSize | split(' ') %} + Size: {{ servesValue(servingSizeParts[0], value.serves) ~ ' ' ~ servingSizeParts[1] ?? '' }} +
{% endif %} {% if value.serves | length %}
Serves: {{ value.serves }}
@@ -134,7 +148,7 @@ {% if value.calories | length %} Calories: - {{ value.calories }} + {{ servesValue(value.calories, value.serves) }} {% endif %} @@ -142,14 +156,14 @@ {% if value.fatContent | length %} - Total Fat: {{ value.fatContent ~ 'g' }} - {{ percentage(value.fatContent, rda.fatContent) }} + Total Fat: {{ servesValue(value.fatContent, value.serves) ~ 'g' }} + {{ percentage(value.fatContent, rda.fatContent, value.serves) }} {% endif %} {% if value.saturatedFatContent | length %} - Saturated Fat: {{ value.saturatedFatContent ~ 'g' }} + Saturated Fat: {{ servesValue(value.saturatedFatContent, value.serves) ~ 'g' }} @@ -157,47 +171,50 @@ {% 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' }} - {{ percentage(value.cholesterolContent, rda.cholesterolContent) }} + + Cholesterol: {{ servesValue(value.cholesterolContent, value.serves) ~ 'mg' }} + {{ percentage(value.cholesterolContent, rda.cholesterolContent, value.serves) }} {% endif %} {% if value.sodiumContent | length %} - Sodium: {{ value.sodiumContent ~ 'mg' }} - {{ percentage(value.sodiumContent, rda.sodiumContent) }} + Sodium: {{ servesValue(value.sodiumContent, value.serves) ~ 'mg' }} + {{ percentage(value.sodiumContent, rda.sodiumContent, value.serves) }} {% endif %} {% if value.carbohydrateContent | length %} - Total Carbohydrate: {{ value.carbohydrateContent ~ 'g' }} - {{ percentage(value.carbohydrateContent, rda.carbohydrateContent) }} + Total + Carbohydrate: {{ servesValue(value.carbohydrateContent, value.serves) ~ 'g' }} + {{ percentage(value.carbohydrateContent, rda.carbohydrateContent, value.serves) }} + {% endif %} {% if value.fiberContent | length %} - Dietary Fiber: {{ value.fiberContent ~ 'g' }} - {{ percentage(value.fiberContent, rda.fiberContent) }} + Dietary Fiber: {{ servesValue(value.fiberContent, value.serves) ~ 'g' }} + {{ percentage(value.fiberContent, rda.fiberContent, value.serves) }} {% endif %} {% if value.sugarContent | length %} - Sugars: {{ value.sugarContent ~ 'g' }} - {{ percentage(value.sugarContent, rda.sugarContent) }} + Sugars: {{ servesValue(value.sugarContent, value.serves) ~ 'g' }} + {{ percentage(value.sugarContent, rda.sugarContent, value.serves) }} {% endif %} {% if value.proteinContent | length %} - Protein: {{ value.proteinContent ~ 'g' }} - {{ percentage(value.proteinContent, rda.proteinContent) }} + Protein: {{ servesValue(value.proteinContent, value.serves) ~ 'g' }} + {{ percentage(value.proteinContent, rda.proteinContent, value.serves) }} {% endif %}