The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Grab the route as a variable and use in a Twig template

James's Avatar


15 Apr, 2012 12:02 PM

Hi all

First of all, thanks for a great framework. First caveat, I'm not a professional coder, but have a technical background, so I am trying to pickup a framework and decided upon Slim as I had some (non-professional) PHP experience. I have been through the net-tuts tutorial which is all working fine and have now started to build my mini-web-app. The web app has a currency conversion aspect to it and the database has essentially got a row with a date and the exchange rate, USD, EUR, GBP etc

I have setup a route called like so: $app->get(‘/(:currency1)/(:currency2)/(:amount)’, function() use ($app) which then calls a database record and I can display the rows and the columns as needed. However, I would like to use the currency1, currency2 and amount in the template itself i.e. reference {{ data.currency2 }} to dynamically grab the right column name from the whole row depending on the route that was called – is this possible?

Thanks in advance

  1. 1 Posted by mauro.pizzamigl... on 17 Apr, 2012 06:26 AM

    mauro.pizzamiglio's Avatar

    hello James

    both currency and amount can be parameters of the closure

    $app->get('(:currency1)/(:currency2)/(:amount)'), function($currency1, $currency2, $amount) { ... });

    be careful, as long as you declare currencyN as optional you would consider to give a default value like:

    function ($currency1 = DEF1, $currency2 = DEF2, $amount = DEFA)

    finally, i think you are using Twig as template engine, so, having both currency in the closure you can pass it to twig like

    $data['currency1'] = $currency1; $data['currency2'] = $currency2; $data['amount'] = $amount; $app->render(TEMPLATE, $data);

    Finally number 2
    consider to use route conditions [ ] as long as you cannot really trust the end user input

  2. 2 Posted by James on 17 Apr, 2012 07:27 AM

    James's Avatar

    Thanks Mauro for your explanation, I have made some changes based on your suggestions (I am using Twig, apologies should have stated this). I am still having one problem though. I will try to explain it:

    1) My data passed to the Twig template looks like this as an example:

              DateTime     GBP    EUR  USD  ZAR

    Row 1 2012-04-14 0.63 0.75 1 7.88

    2) I pass the data above as an array to the Twig template and now am also passing the URL as another array (called them rowdata and data respectively)

    3) The next bit is the problem - I can display the row data in Twig as {{ rowdata.DateTime }} or {{ rowdata.EUR }} and now the URL as {{ data.currency1 }} and so on, but I need to be able to dynamically reference the rowdata depending on the URL, for example:

    if the user requested EUR/GBP/1 then I need to grab the rowdata.EUR value, but dynamically i.e. rowdata.currency1 to then do the conversion (in this case 1 Euro in pounds)

    I hope my explanation makes sense, once again thanks for the input, I'm thinking that the solution is easy for an experience coder, but I'm learning, so thanks for the patience!

  3. 3 Posted by James on 17 Apr, 2012 07:30 AM

    James's Avatar

    Oops the data didnt come out correctly, it should read

    '            DateTime     GBP    EUR  USD  ZAR
    Row 1 2012-04-14 0.63 0.75 1 7.88'
  4. 4 Posted by mauro.pizzamigl... on 17 Apr, 2012 08:06 AM

    mauro.pizzamiglio's Avatar

    Hello James,

    this is relative to Twig, but anyway, you first should take a look at this:

    so the twig syntax is right here
    but If you need only what the user asks for (in the URI), you can try another way to do that (assume i dont know what you really need):
    you can do a query like (in mysql)
    SELECT $currency1 AS base, $currency2 as convert FROM table suppose the result in PHP is an array like $rowdata['mysql_field_name']
    you can now refer what the user asks like
    $rowdata['base'] in PHP and within the template {{ rowdata.base }} where base depends on the URI (be damn careful with user input)

  5. 5 Posted by James on 17 Apr, 2012 09:04 AM

    James's Avatar

    Thanks Mauro!

    I ended up going with your second option (I think it's better to do the work in Slim than in Twig, sticking to the fat model, no logic in the view thing) - I think I may have been overcomplicating things by using Paris/Idiorm (as per the net-tuts tutotrial) - what I ended up doing was using your second option and just grabbing the columns needed for the URL inputted - now what I'll do as you suggested in your first response is validate the URL using the routing conditions you mentioned.

    Thanks once again

  6. Andrew Smith closed this discussion on 01 Aug, 2012 12:53 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac