DRY embed/block/conditions

dakur
Member | 301
+
0
-

Hi,

I have basically an element where I want to render user's name and some icon (SVG markup) with tooltip (plain text) – in 5 modifications based on current state. The code looks something like this:

<span n:class="selector1, selector2, $condition ? $selector3" title="{$tooltipContent}">
  {$userName}

  {$icon}
</span>

$icon and $tooltipContent are tied together in conditional logic – like this:

if ($condition1) {
  $tooltipContent = '...';
  $icon = '...';
} elseif ($condition2) {
  $tooltipContent = '...';
  $icon = '...';
} elseif ($condition3) {
  $tooltipContent = '...';
  $icon = '...';
} elseif ($condition4) {
  $tooltipContent = '...';
  $icon = '...';
} else {
  $tooltipContent = '...';
  $icon = '...';
}

Now, the point is to:

  • do not repeat whole condition block twice for tooltip and icon
  • do not repeat wrapping span five times in each if-block

I thought I could go with {embed} but then I was disabused it won't work by design. So my question is: how would you do it?

dakur
Member | 301
+
0
-

Temporarily I've solved it with {capture $tooltipContent} and {capture $icon} but still it seems to me that variables are for data whereas blocks are for HTML – which I am not able to do. 🙁

Honza Kuchař
Backer | 1661
+
0
-

This looks like an candidate for DTO. So This logic would be split into two parts — on with conditions and rest (in latte).