I have the following code :
{% set aSessionKeys = app.session.get('aBasket')|keys %}
onkeyup="calculatePrice({{ product['product_price'] }},{{ product['product_id'] }},{{ aSessionKeys }})
And I have an error :
Notice: Array to string conversion
Can you help me please? Thx in advance. So exist a solution to passe this array?
I do like this :
{% set aKeys = aSessionKeys|join(',') %}
{{ dump(aKeys) }}
It shows good, but if I passe to jquery :
calculatePrice({{ product['product_price'] }},{{ product['product_id'] }},{{ aKeys }})
When I do in js methode calculatePrice() :
function calculatePrice(price, product_id, aKeys) {
console.log(aKeys);
var x = document.getElementById("product_quantity_"+product_id);
var total_price = price * x.value;
$("#total_price_"+product_id).html(total_price+" <small> MDL</small>");
sum = 0;
$("#total_price_basket").html();
}
It shows only the first value of aKeys
You have very popular error. You are trying to display array. But how do you imagine that? You can use {{ dump(aSessionKeys) }} for that purpose or use first element: {{ aSessionKeys.0 }} (if it is not an array or object).
But if you want to print all values from this array you can run foreach loop:
{% for element in aSessionKeys %}
{# something here #}
{{ element }}
{# something here #}
{% endfor %}
Obviously (as you are using the |keys filter) aSessionKeys contains an array, and by using {{ aSessionKeys }} in the template Twig will try to output the array as a string, which doesn’t work (or at least is not what you want).
To convert the array to a string, you should use Twig’s join() filter, which will concatenate the array values using a given delimiter string. So, if – for instance – you want to join the values using a comma, this would be the code:
{{ aSessionKeys|join(',) }}
Related
If I get an array of types using Doctrine like this:
$types = $this->getDoctrine()
->getRepository('Model:Type')
->findAll();
And then pass $types to the Twig template (as 'types') and loop through it:
{% for type in types %}
-- WHAT GOES HERE?
{% endfor %}
I've been doing a bit of reading, and I'm not even sure if this is possible? Can I only pass associative arrays through to Twig or do arrays of objects work? And if so, how can I access the public functions of the object in Twig?
Basically I want to call getName(), getUsage(), getId() and a few other public functions on the Type object.
Thanks
Then you could do it like below:
{% for type in types %}
{{ type.name }}
{{ type.usage }}
{{ type.id }}
{% endfor %}
{{ type.getName() }} also works, it's same with {{ type.name }}.
I am bored searching for an answer so this is my firs question here.
In Symfony 2 , in my twig template i iterate over an array of objects:
{% for client in clients %}
i have the variable client.curs = to the string "Cursul 1 = 4.1234"
i want to split this string so i use
{% set cursarr = client.curs|split(' = ') %}
now, if i dump my array i get
array (size=2) 0 => string 'Cursul 1' (length=8) 1 => string '4.1234' (length=6)
wow! cool! just what i wanted. i continue my work, i just need the second part of
the array (4.1234) so i do this:
{{ cursarr[1] }}
ooops! Key "1" for array with keys "0" does not exist.
Ok! i am an idiot so i try:
{{ cursarr.1 }} same error here. Hmmmm! WTF?!
i try {{ cursarr[0] }} pops out 'Cursul 1' WTF?!
{{ cursarr.0 }} also working
i don't get it, what am i doing wrong? Why is life so complicated?
is it because it is late and i am tired? Need help!
{% endfor %}
I manage to make it work this morning with a clear mind :)
{% set cursarr = client.cursuri|split(' = ') %}
{% set cSpecial = '' %}
{% for curs in cursarr %}
{% set cSpecial = curs %}
{% endfor %}
I have to iterate the array in order to get the second value;
I didn't realized it is a multidimensional mdfkr.
Hope this will help someone in need.
I want to render dynamicly a form in Symfony. I passing a array with elements of element names to the render method 'formElements' => array('formelement1', 'formelement2').
How i want to use the element names in my template to show the form labels.
{% for elementName in elementNames %}
<div class="form-lable">
{{ form_label({{ elementName }}) }}
</div>
{% endfor %}
I received the following exception:
A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{" in onBillBundle:Customer:new.html.twig at line 17
Is it not posible to render the form dynamicly without {{ form(delete_form) }}?
Untested...
{% for elementName in elementNames %}
<div class="form-lable">
{{ form_label(attribute(form, elementName)) }}
</div>
{% endfor %}
Docs
You don't need to surround variables in a twig statement with {{ and }}. So your code should be:
{{ form_label(elementName) }}
But of course elementName needs to be a Form Object and not a string. You can generate them in your Controller like this:
public function testAction()
{
// ...
$form = $this->createFormBuilder()
->add('name', 'text');
return ['form' => $form->createView()];
}
I stored a format of shopping order in database, it's like #{{ number }} (it's just a string), how can use this {{ number }} as a execution of Twig.
For example, in my Controller, when I render the view, I also pass a $number variable:
return $this->render('MyBundle:View:index.html.twig', array('number' => 123));
and in my index.html.twig file, it's something like
{% set orderFormat = some_function_to_get_order_format() %}
// orderFormat will be #{{ number }}
// What can I do to print orderFormat to #123
Try template_from_string function.
{{ include(template_from_string("Hello {{ name }}") }}
{% set var_name1 = "hello" %}
{% set var_name2 = "there" %}
{% array1|merge({var_name1: var_name2}) %}
I was hoping the code above would add this to array1:
hello:there
...but it adds:
var_name1:there
I've tried wrapping {{ }} around var_name1. Is it possible to add a record to an array and use a variable for the key?
Enclose the key name in brackets:
{% array1|merge({(var_name1): var_name2}) %}
Note that if var_name1 is a numeric value, it won't work.
You'll have to concat it with a string value :
{% set array1 = array1|merge({(var_name1~'_'): var_name2}) %}