I would like to know if it was possible to cast a String to an Int in twig. I try to see if a user has enough credit or not to buy a cours. For that, I calculate the amount of credit with a render in the template (because I need the value in the template, and I didn't found a better way to do it...) like this :
{% set creditUser %}
{{render(controller('L3L2EntraideBundle:Credits:sommeCredits'))}}
{% endset %}
But when I try to compare creditUser :
{% if creditUser < c.idCompetenceCours.prix %}disabled="false"{% endif %}
Symfony return me a beautiful error : An exception has been thrown during the rendering of a template ("Notice: Object of class Twig_Markup could not be converted to int") in L3L2UserBundle:Profile:modal_prendre_rdv.html.twig at line 21.
Any idea ? Thank you in advance for my first question on Stackoverflow and sorry for my english.
This is not string but Twig_Markup
{% if creditUser.__toString < c.idCompetenceCours.prix %}
but this is not good approach you should get this value from object/variable not from rendered template
Related
When I have an error in my custom WordPress theme I would like to output the webmaster email address which would be webmaster#mydomainname.com but I am a bit baffled on how to do this in Twig/Timber in the most straightforward way: <p class="text-danger fw-bold">PAGE ERROR - Please contact Webmaster at webmaster#{{ #notsure# }}</p>
webmaster#{{ site.url }} just outputs: webmaster#https://mywordpress.local which obviously won't work.
UPDATED: To get by I am using webmaster#{{ site.url[8 :] }} as that strips away the https:// and outputs webmaster#mywordpress.local but seems there should be a cleaner way somehow?
There are two ways to do this:
You can use Advanced custom field. Make a email field and then print that value inside the twig file. Inside advanced custom field, you can add any email you have no need to extract a domain name. For more information follow this reference: https://timber.github.io/docs/guides/acf-cookbook/
Second method is the way you doing is correct but you need to split domain name from site.url using slice method:
{% set website = "https://mywordpress.local" %} //
//calcualting length of string
{% set lengthOfWebsite = website|length %}
//using length here to split the string accordingly. 8is for split "https://" from actual domain name.
{% set domainName = website|slice(8,lengthOfWebsite) %}
webmaster#{{domainName}}
For the first line of code in your case will be:
{% set website = site.url %}
For the last line
webmaster#{{domainName}}
can also be replaced by:
{% set actualDomainName = 'webmaster#' ~ domainName %}
{{actualDomainName}}
I've got this code:
Twig template:
<div class="content-items-wrapper">
{% for item in home.latest_posts %}
{% include "partials/content-item.twig" with item %}
{% endfor %}
</div>
In my theme file:
$context['home']['latest_posts'] = new Timber::get_posts([
'posts_per_page' => 6,
]);
When using these line of code I get the following notice:
Warning: array_merge(): Argument #2 is not an array in //content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 86
Followed by a fatal error:
Catchable fatal error: Argument 1 passed to Twig_Template::display() must be of the type array, null given, called in /content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 86 and defined in //content/plugins/timber-library/vendor/twig/twig/lib/Twig/Template.php on line 401
The strange thing is: when I don't use the include in the Twig template, but just use the contents of this include directly the errors are gone.
Also just putting one character (or even nothing) in the included partial gives the errors.
Also when not using Timber::get_posts(), but just filling an array with the correct data, I don't get any errors.
But obviously both are not really wanted solutions :-)
What could this be?
PS: I've got a frontend-only version (running of gulp-twig), feeded by JSON data, which is just running fine.
Edit: fixed the example (copy/paste mistake)
Don't you need to add the context home.items like $context['home'] or $context['home']['latest_posts'] just like with $context['posts']?
Problem solved!
Changing
{% include "partials/content-item.twig" with item %} into {% include "partials/content-item.twig" with { 'item' : item } %} did the trick.
This question already has answers here:
Twig - dynamically replace GET parameter's value
(2 answers)
Closed 6 years ago.
In a Symfony 2.7 app, with Twig, I want to create generate pagination buttons.
I have a search page, with optional GET parameters :
Empty :
www.[...].com/search
Search by name :
www.[...].com/search ?name=john
Search by name and sort by relevance :
www.[...].com/search ?name=john&sort=relevance
Search by name and sort by relevance, page 2 :
www.[...].com/search ?name=john&sort=relevance&p=2
More than 10 possibles parameters, so I don't want to define a route pattern with all optional parameters.
Then, when I display the search results, I want to link next pages :
www.[...].com/search?name=john&sort=relevance &p=2
www.[...].com/search?name=john&sort=relevance &p=3
www.[...].com/search?name=john&sort=relevance &p=4
In the TWIG template I tried it so :
{% set param = app.request.get('_route_params') %}
{% if param is null %}
{% set param = {'p': page} %}
{% else %}
{% set param = param|merge({'p': page}) %}
{% endif %}
{{ page }}
But, because the optional parameters are not defined in the route, they are not in request.get('_route_params') and the link builded only define ?p=X.
Is there any other way to get queried GET parameters and to edit them creating a new url with an other p ?
In your twig template, you can use:
app.request.query.all
to get all your query parameters
I want to edit the default form template to include a thumbnail preview of an image when I display an upload field, so I put a method called getFormThumbnail() in my entity that returns the path. When the entity has the method it works, but when it doens't I get an error:
An exception has been thrown during the rendering of a template ("Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'Acme\UserBundle\Entity\UserTranslation' does not have a method 'getFormThumbnail' in C:\...\symfony\vendor\knplabs\doctrine-behaviors\src\Knp\DoctrineBehaviors\Model\Translatable\TranslatableMethods.php line 140") in kernel.root_dir/Resources/views/Form/form_div_layout.html.twig at line 43.
500 Internal Server Error - Twig_Error_Runtime
I am editing the code in {% block form_widget_simple %}
I added :
{% if type == 'file' %}
{% if form.vars.form.parent.vars.value.getFormThumbnail is not null %}
<img src="{{ form.vars.form.parent.vars.value.getFormThumbnail | imagine_filter('thumb_250') }}" style="display: block; margin-bottom: 10px">
{% endif %}
{% endif %}
This code works perfectly when getFormThumbnail exists. So I tried adding is defined and it doesn't work, I even tried things like:
form is defined
and
form is not null
and
form.vars is defined
and
form.vars is not null
and
form.vars.form is defined
and
form.vars.form is not null
and
form.vars.form.parent is defined
and
form.vars.form.parent is not null
and
form.vars.form.parent.vars is defined
and
form.vars.form.parent.vars is not null
and
form.vars.form.parent.vars.value is defined
and
form.vars.form.parent.vars.value is not null
and
form.vars.form.parent.vars.value.getFormThumbnail is defined
and
form.vars.form.parent.vars.value.getFormThumbnail is not null
Still, I always get the same error, the line in the error by the way is the line where I test if getFormThumbnail is defined
So if the error is does not have a method is there a way to test if it has a method?
You can create a custom fragment only for that field. So you don't have to check if the method exists or not http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field.
Or you can create a form type extension: http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html
I have added the translator service for my Symfony2 project. I use it both in controllers and in twig templates. It's configured fine and all the {% trans %} tags work as they mean to. But in some cases I need to use the {% transchoice %} tag, and it's not getting the translation. Here is an example from my code.
{% transchoice post['comments']['count'] %}
{0} Comments| {1} Comment| ]1,Inf] Comments
{% endtranschoice %}
Also have tried writing this in one line.
I get the correct choice for the comment count, but the word itself is not beeng translated. Like the translator is not able to find the corresponding translation. In the messages.de.yml I have
Comment: "Kommentar"
Comments: "Kommentare"
Is it something wrong with my transchoice syntax? Maybe I need to place spaces somewhere, or anything like that?
In your translation file, you should write this:
{0} Comments| {1} Comment| ]1,Inf] Comments: "{0} Kommentare| {1} Kommentar| ]1,Inf] Kommentare"
UPDATE:
An xliff example that works for me :
<trans-unit id="search.results.summary">
<source>search.results.summary</source>
<target>{0}Pas d'annotations trouvée pour "%search_text%"|{1}Une annotation trouvée pour "%search_text%"|]1,Inf]%search_count% annotations trouvées pour "%search_text%"</target>
</trans-unit>
How I use it:
<h2>{{ 'search.results.summary' | transchoice(search_count, {
'%search_text%': search_text,
'%search_count%': search_count}) }}</h2>
As you see, I don't use the complicated notation as the source for my translation, since it is pretty useless and would make the template less readable. Instead, I put a dot-separated string representing the semantical value of my string.
In your case, I guess the correct thing to use would be something like this:
comment.summary: "{0} Kommentare|{1} Kommentar|]1,Inf] Kommentare"
and
{% transchoice post['comments']['count'] %}
'comment.summary'
{% endtranschoice %}
Good luck.