TYPO3 - get last element of a splitted string

Viewed 51

Trying to get the last part of the url as a body class. So far I got the slug of the page and split it by /

page.bodyTag >
page.bodyTagCObject = TEXT
page.bodyTagCObject.field = slug
page.bodyTagCObject.split {
  token = /
  cObjNum = 1
  1.10 = TEXT
  1.10.current = 1
  1.10.noTrimWrap = | ||
  1.10.dataWrap = | 
  1.10.required = 1
}

With this code, I am getting: url: page/foo/bar/

class="page foo bar"

I need to get class="bar"

1 Answers

You'll need to use optionSplit for that. For example:

lib.test = TEXT
lib.test {
  value = 1,2,3,4
  split {
    token = ,
    cObjNum = 1 |*| 2 |*| 3
    1 = TEXT
    1 {
      current = 1
      wrap = First item:|<br/>
    }
    2 = TEXT
    2 {
      current = 1
      wrap = Middle item:|<br/>
    }
    3 = TEXT
    3 {
      current = 1
      wrap = Last item:|<br/>
    }
  }
}

1 |*| 2 |*| 3 means: use 1 for the first item, 3 for the last item and 2 for the rest.

You can just not set the ones you don't want, so this will only show the last item:

    cObjNum = 1 |*| 1 |*| 2
    2 = TEXT
    2 {
      current = 1
      wrap = Last item:|<br/>
    }
Related