3

In symfony2 I used config yaml below:

// config.yml
parameters:
  something: 
    content: 
      price:  2.30
      mainText: 'Some text here.'
      redText:  'This is a text here plus price: ' %price%

The %price% is wrong and gives me an error but system tells its an array, so how to point to something['content'][price]?

3
  • And why would you want to put text in a config file? Why not put it in a translation file? Commented Jul 8, 2012 at 17:22
  • My site does not use multiple languages. However it supports different brands and some text in yml is easier to configure for multiple versions Commented Jul 8, 2012 at 20:39
  • You could also construct the necessary parameters in an Extension class (symfony.com/doc/current/cookbook/bundles/extension.html). However, it takes a lot more effort, especially if you haven't worked with configuration exposing before. Commented Jul 13, 2012 at 20:53

3 Answers 3

7

Here's how you can do it

parameters:
    something.content.price: 2.30
    something:
        content:
            mainText: 'Some text here.'
            redText:  'This is a text here plus price: %something.content.price%'

Here we only have 2 parameters, something.content.price that contains a float, and something that contains an array.

This means you will only be able to access directly those 2, inside the DI configuration.

Sign up to request clarification or add additional context in comments.

5 Comments

It means I can also define price: 2.30 and then use %price% which may not be as readable as it should :/
I looked at the DI component code, and that is the most reasonable thing that worked i found i could use.
And the way you use mean that you will only be able to inject %something%, but not %something.content.mainText%. This limitation may be to be sure that performance are at least reasonable/good.
I am afraid you are right. It looks like you will be the winner
Ugh, i thought i'd win something :p
1

Same answer, different post

A bit late, but here is the solution you were looking for :P

// parameters.yml
parameters:
    something: 
    content: 
        price:  2.30
        mainText: 'Some text here.'
        redText:  'This is a text here plus price: '

The way to use the price defined in your array would be something like:

//config.yml 
twig:
    globals:
        fee: content['price']

Explanation:
When you import your parameters file, into your config.yml file automatically you can access to all variables defined there. To keep in mind, when you use the structure you defined.

You are defining an key value pair array called content that contains many key and value pairs, and the way to refer to them is the one described above.

Hope this be useful for those who may be looking for this! :)

Comments

-2

Maybe %something.content.price%

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.