3

I have an array contains static data related to an entity Product:

public static $category = array(
    1 => 'animal.png',
    2 => 'blague.png',
    3 => 'devinette.png',
    4 => 'enfant.png',
    5 => 'h-f.png',
    6 => 'nationalite.png',
    7 => 'politique.png',
    8 => 'sport.png',
    9 => 'name',
    10 => 'travail.png',
    11 => 'vulgaire.png',
    12 => 'autre.png',
);

Where i should declare the array ?

And how i can accede to data from the Twig view ?

Thanks

3
  • 1
    Is the Product entity passed to the Twig template? (in other words, is it passed as an argument with the $this->render(...) function?) Commented Jul 3, 2014 at 13:36
  • That what i acutely did, but i thought that there is an other method with twig ? Commented Jul 4, 2014 at 8:18
  • 1
    Yes, we can define constants with Twig (named global variables). But I think that if your data is related to an entity it's logical to keep it inside the Entity. Commented Jul 4, 2014 at 15:31

2 Answers 2

5

I don't know if that's the best way but I used something similar to your code:

class Product
{
    protected static $category = array( 
        1 => 'animal.png',
        2 => 'blague.png',
        3 => 'devinette.png',
        // ...
        )
    );
}

Then you can add some functions in this class in order to get data from the array

    public function getCategoryImageFromIndex($a)
    {
        return self::$category[$a];
    }

    // if you have a getter getCategory() which returns the category of the Product
    public function getCategoryImage()
    {
        return self::$category[$this->getCategory()];
    }

Then you can call these functions from Twig:

{{ product.categoryImageFromIndex(1) }}

will display:

animal.png

And

{{ product.categoryImage }}

will display the corresponding image from the category.

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

1 Comment

Hi @Haithem Rihane if this answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
2

I've always used a Twig extension function to access the static array.

For example, in my Order entity, I have something like this:

class Order
{
    const ORDER_STATUS_PENDING = 0;
    const ORDER_STATUS_AWAITING_PAYMENT = 1;
    const ORDER_STATUS_COMPLETE = 2;

    public static $ORDER_STATUS_DISPLAY = [
        self::ORDER_STATUS_PENDING => 'Pending',
        self::ORDER_STATUS_AWAITING_PAYMENT => 'Order placed',
        self::ORDER_STATUS_COMPLETE => 'Order completed',
    ];

then assuming you already have a registered Twig_Extension class, create a new filter function:

public function displayOrderStatus($orderStatus)
{
    return Order::$ORDER_STATUS_DISPLAY[$orderStatus];
}

Finally, use the filter in your Twig template:

{{ order.orderStatus|displayOrderStatus }}

Comments

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.