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
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 have a layout included in another one that display my menu. The labels of my menu items need to be dynamic (like the unread messages number of a mailbox). Then I did this :
My orders
(
{{ render(controller('MyController', {'etat':2})) }}
<span style="color:red">with {{ render(controller('MyController', {'etat':2})) }} in late</span>
)
I would like to display labels according to the number that return my controller. I don't know how to get it in a variable.
When rendering your template in your controller
return $this->render('twig_template_name.html.twig', array('variable_name' => $variable);
you pass a variable to the twig template in the array of options as I showed. Your code
{{ path('mypath',{'etat': '2' }) }}
prints a path defined in the routing.yml under the 'mypath' section and ends up adding a GET request variable to the link ('?etat=2'), if 'mypath' showed an absolute route 'www.website.com/yourpath',
{{ path('mypath',{'etat': '2' }) }} would produce 'www.website.com/yourpath?etat=2', which would send your controller for a route /yourpath/{etat} a variable etat with a value of 2 so all you need to do now is change 2 with an actual dynamic value which you receive from another controller.
I am not sure what etat is but lets say it's an article and it has it's id, you have a blog page with lots of articles and the controller that prints them all out sends an array of articles to the twig template, on your twig template you do something like:
{% foreach article in articles %}
{{ article.title }}
{{ article.story }}
read more
{% endforeach %}
And you end up something like:
Catchy Title
Awesome story about code without bugs and where deadlines depend on how creative and well designed and implemented the solutions are
[read more]
and you ofcourse click on "read more" and end up on the url ~/article/2 because the article had an id of 2, your controller for that url receives a variable id in the request, you do a $id = $_GET['id']; and grab the article from the repository and send it to the template.
Hopefully this answered your question, I am very tiered so forgive me if i was confusing which I surely was.
When I use the path function in Twig, there is a strange problem with the '?' when I want to set GET parameter.
Example:
href="{{ path(brData.router, {(brData.slug): brData.url}) }}"
If I set now as brData.url: 'search?q=bla',
then twig is coding the url in this way:
domain.com/search%3Fq=bla , and the search can not be executed. How can I prepare the url in the righ form: domain.com/search?q=bla
Thanks
Nik
The use of domain.com/search?q=bla involves you have a route defined with the path /search.
If you have the route, just pass a parameter and its value without wrapping it in a ?key=[value].
Example assuming search_route is the name of your /search route:
{% set fieldValue = 'bla' %}
{% set url = path('search_route', {'q': fieldValue}) %}
So href="{{ url }}" will give href="domain.com/search?q=bla".
Is it possible to translate the value of twig variables in a template with the 'trans' tag?
Say for instance I am passing a product to my template. This product has a definition with a trans tag e.g {{ product.definition|trans }}. This definition could either be in EN or DE or some other language. How could I translate the definition.
What are you trying to do is not a good way, It would look like this:
messages.en.yml
product:
definition:
some_value1: Some value 1
some_value2: Some value 2
and in template, you would do something like this:
{% set definition_value = product.definition %}
{% set trans_definition = 'product.definition.' ~ definition_value %}
{{ trans_definition|trans }}
it'll work, if it finds the key. What if it cant find it?
That's why you should use DoctrineBehaviors from KnpLabs, which handles all the dynamic translations for you..
If {{ product.definition }} equals 'cellphone' the following should work.
message.language.yml:
'cellphone': This will work!
However if you want to map it with the 'product' key in your message file like this:
product:
'cellphone': This also works
add the key to the twig template like so:
{{('product.'~product.definition)|trans }}
I need to have this type of data in my form :
value11, value12, value13 ...
value21, value22, value23
value31, value32, value33
value41, value42, value43
...
This is how the form should look like :
Is there any form type that can do that automatically or the form must be created manually? There is collection type but its seems cant fit in my case. I am planing to serialize data from this table sheet and story it to single db table column (single property of entity class).
Is there any better way to store table sheet data type presented in this example? Preserving all values in their own entity properties is not an option, because there will be plenty of table data in that form.
You should create an entity class with x , y and value properties.
x & y being unique combinations, representing the position in your matrix.
This entity does not have to be stored in your database and can only be used to generate the form.
Then create a form-collection which renders your fields in this matrix form.
Finally: use a DataTransformer to transform the collection of entities to an array as desired.
Now save the array in your database.
I have tried couple of solutions and this is best result by my opinion:
Step 1.
Creation of macro for generating tableSheet in separate twig file (for later usage in other forms).
{# in Default:form.html.twig #}
{% macro tablesheet(name, rows, colums, titles) %}
... titles header ...
{% for i in 1..rows %}
{% for j in 1..colums %}
<input name="{{ name }}_{{ i }}{{ j }}" type="text" class="flat-form" />
{% endfor %}
{% endfor %}
{% endmacro %}
Step 2.
Import macro and create tables of input fields, in add/edit template :
{% import "ProjectSomeBundle:Default:form.html.twig" as forms %}
...
{{ form_widget(form) }}
{{ forms.tablesheet('table1', 4, 3, {0:'col1', 1:'col2', 2:'col3'}) }}
{{ forms.tablesheet('table2', 7, 3, {0:'col1', 1:'col2', 2:'col3'}) }}
...
</form>
Step 3.
Create jQuery function to serialize/deserialize form (input elements with flat-form class attr in this case). For serilize I used something like this http://css-tricks.com/snippets/jquery/serialize-form-to-json/ . Serialized values are stored in single entity filed as string and saved to db like that.
How it's works :
on opening edit form, jQuery deserialized data from that entity field and loaded to tablesheet input fields. Before sending it back (post) to server values are collect and put in that same field via serialized function.