0

After lots of searching this is the closest I got. I'm using {$smarty.server.REQUEST_URI} to get second part of the URL after http. Naturally am ending up with lots of text1/text2/text3. Now I'm after extracting last string of text located after the last forward slash.

from my understanding I have to use strpos to get the position of the last forwardslash, ideally start from the end and stop on the first slash which in reality would be the last since we started right-to-left, and then use substr to get anything from that strpos onwards.

Community
  • 1
  • 1
Joe Borg
  • 65
  • 2
  • 12
  • possible duplicate of [Regular Expression to collect everything after the last /](http://stackoverflow.com/questions/1150559/regular-expression-to-collect-everything-after-the-last) – Amal Murali Nov 05 '14 at 14:07
  • just a thought: are you sure that you want to do this using smarty? It does not seem like something that should be the job of the template to calculate. – fejese Nov 05 '14 at 14:14
  • My friend Amal, the link you posted refers to using PHP. Although smarty is built on top of PHP it has its own limited list of reserved variables which I'm still researching. – Joe Borg Nov 05 '14 at 14:14
  • Am after a solution with regards to frontend rather than achieving this from PHP backend. am aware that it can also be achieved using javascript yet, that would be more weighty on the code than using smarty with regards to frontend solutions. – Joe Borg Nov 05 '14 at 14:17
  • @JoeBorg: just to be clear: `smarty` is generated in the backend in terms of on the server, not on the client side. – fejese Nov 05 '14 at 14:32

2 Answers2

2

You can do it this way (if you using Smarty2):

{assign var="dirs" value="/"|explode:$smarty.server.REQUEST_URI}
{math equation ="x-y" x=$dirs|@count y=1 assign="last"}
{$dirs[$last]}

and this way:

{assign var="dirs" value="/"|explode:$smarty.server.REQUEST_URI}
{$dirs[$dirs|@count-1]}

if you are using Smarty 3

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • wow that was challenging since I've hadn't an idea that smarty utilized the equation and the explode vars. Am now studying how the equation achieved it. Thanks man, noted it down for future reference in my coding diary ;) – Joe Borg Nov 06 '14 at 08:02
0

The template is not the proper place for such processing. Parse the request string in PHP and assign to the template the values it needs to display. The template systems were invented to separate the logic (processing of data, done in the code) from the presentation (HTML and stuff, done in the templates).

axiac
  • 68,258
  • 9
  • 99
  • 134