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 }}") }}
Related
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(',) }}
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 have a one to one relation between Player and Image. I have also this line in my controller:
var_dump($players[0]->getLinkedImage1()->getName());
It shows the name of an image correctly.
And I have also this line in the template:
{% for players in player %}
{{ player.age }}
{{ player.linkedImage1.name }}
{% endfor %}
but I get this error:
Impossible to access an attribute ("name") on a NULL variable ("")
I expected the last line shown the same name as in the controller.
EDIT: finally I found out that the property was public, that was the reason. Anway I still understand it..
My bad !
Look your for loop..
Try
{% for player in players %}
How did you write the property ? linkedImage1?
If you wrote something like linked_image_1 or linkedImage_1 you should call
{{ player.linked_image_1 }}
or
{{ player.linkedImage_1 }}
then twig will call the related getter according to : http://api.symfony.com/2.4/Symfony/Component/DependencyInjection/Container.html#method_camelize
If its a virtual getter you can directly access with :
{{ player.getLinkedImage1().name }}
or {{ player.getLinkedImage1().getName() }} `
You should try
{{ player.getLinkedImage1().getName() }}
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 have entity like
/**
* #ORM\Column(type="text",nullable=true)
*
*/
private $freeText;
then I fetch this column in php and put this in twig.
{{ freeText }}
But it ignores the line feed code.
I think I have to change line feed code to 'br' tag though,where and how should I do this?
Is there any special function on twig?
I'm guessing you mean that even if you put newlines in your text, the browser ignores it and displays text in the same line. Yes, you'll need to replace \n characters with <br /> tags, there's a filter for that:
{{ "I like Twig.\nYou will like it too."|nl2br }}
{# outputs
I like Twig.<br />
You will like it too.
#}
I guess the string is being (auto)escaped ... try:
{{ var|raw }} {# var won't be escaped #}
... or ...
{% autoescape false %}
{{ var }}
{% endautoescape %}