Fetch url parameters in twig - twig

this is my URL:
192.168.0.101/user1/opc_autopart/upload/index.php?route=product/category&path=20
I want to fetch path parameter and below is my code
{{ app.request.get('path') }}
This code is not working

You may use:
{{ app.request.query.get('path') }}
to get the path query string in twig.
You would better define a default value, in case there is no path parameter:
{{ app.request.query.get('path')|default('1') }}

Related

Setting default value for CKEditor in Flask App wtforms

I'm trying to set the default value for a CKEditor html text input based on an existing object. The use case is for updating an existing row using flask-sqlalchemy so the user doesn't have to retype all of the existing rich text. As the code is written, the default value for form.description is blank even when object.description is not.
The relevant part of the form looks something like this:
<form method="POST">
{{ ckeditor.load() }}
{{ form.hidden_tag }}
{{ form.name.label }}
{{ form.name(class="form-control", value=object.name) }}
{{ form.description.label }}
{{ form.description(class="form-control", value=object.description) }}
{{ edit_loc_form.submit(class="btn btn-primary") }}
</form>
{{ ckeditor.config(name='description') }}
Thanks!
From the docs:
If you are using Flask-WTF/WTForms, it’s even more simple, just pass
the value to the form field’s data attribute:
#app.route('/edit')
def edit_post():
form = EditForm()
form.body.data = "default value" # default value entered here.
return render_template('edit.html', form=form)

Interpret variable inside another

I am trying to interpret a variable inside another. Something like:
// list of variables
foo: 'test';
bar: 'this is {{ foo }}'
// Twig
{{ bar }}
// result :
this is a test
I try this but I think it is old : Twig variables in twig variable
You can do this with template_from_string Twig function.
{% set foo = 'test' %}
{{ include(template_from_string('this is {{ foo }}')) }}
The template will render with the context of the containing template, meaning, it will have access to all variables that the main template has access to.
Note that template_from_string Twig function is not available by default. It only becomes available once you activate Twig\Extension\StringLoaderExtension extension found in Twig package.

getting route name not the route path in twig symfony

i'm developing an application with symfony3.
I want to get route name in twig. i did this :
{% set current_path = path(app.request.get('_route')) %}
{{ current_path }}
it displays the url of the current page. But i want to get route name not the path.
example :
personnel_index:
path: /liste
defaults: { _controller: "PersonnelBundle:Personnel:index" }
methods: GET
must return : personnel_index
so how can i get the route name
This is because you put the path function try like this
{% set current_path = app.request.get('_route') %}
{{ current_path }}
In the twig template, there is a global variable activeRoute.
For example {{ dump(activeRoute) }} will give you the route name.
There is one example at vendor/shopware/storefront/Resources/views/storefront/component/address/address-personal.html.twig:40
GitHub Link
With this source code.
{% if activeRoute == 'frontend.account.login.page' %}
{% set isLoginPage = true %}
{% endif %}

How to get route link in Slim3 Twig?

I defined my route so:
$app->get('/about', function ($request, $response, $args) {
return $this->view->render($response, 'about.twig');
})->setName('about.page');
I'm interested to get route link by name like: {% get_route('about.page') %}
How can I achieve this?
In Slim3 there is the path_for(name) function. F.ex:
{{ path_for('about.page') }}
Reference: http://www.slimframework.com/docs/features/templates.html
The slim/twig-view component exposes a custom path_for() function to your Twig templates. You can use this function to generate complete URLs to any named route in your Slim application. The path_for() function accepts two arguments:
1 A route name
2 A hash of route placeholder names and replacement values
Note: The path_for uses the function of the router which is $router->pathFor(..)
Why not to use {%
{% is a control structur in twig, Message: Unknown "path_for" tag in "base.twig" at line XX. is displayed because there is no such tag as control structur defined so twig doesn't know that this is actually a function.
So use the output structur in twig {{.
Is this possible?
Yes.
IIRC
{{ path_for('about.page') }}
Reference:
https://github.com/slimphp/Twig-View/blob/master/src/TwigExtension.php#L37

Symfony Translations

i'm using the last version of symfony (3.1.3)
i want to trans multi vars.
but i didn't find and good solution
right now i'm doing:
{% block h1 %}{{ 'service.create'|trans }} {{ ('service'|trans) }}{% endblock %}
I try :
{% block h1 %}{{ 'service','service.create'|trans }} }} { %endblock %}
but no luck.
I also try with
{% trans %}service.create|service{% endtrans %}
thanks
You can use parameters in your translation like this:
{{ 'service.create'|trans }}
{{ 'service.create'|trans({'%separator%': 'any text'}) }}
And in your messages.ru.yml
...
service:
create: ... %separator% ...
...
If your separator is in html, add raw like this :
{{ 'service.create'|trans({'%separator%': '<br>'})|raw }}
You have more details in symfony doc here
You can translate strings in backend:
public function indexAction($name)
{
$translated = $this->get('translator')->trans('Hello '.$name);
return new Response($translated);
}
Or in twig templates:
<h1>{{ 'service.create'|trans }}</h1>
Each time you create a new translation resource (or install a bundle that includes a translation resource), be sure to clear your cache so that Symfony can discover the new translation resources:
php bin/console cache:clear
More info: http://symfony.com/doc/current/translation.html
I needed to just trans multi vars.
twig not support that...
If you have a "service.yml.en" file or something like that :
{{ service.create|trans({}, "service") }}
Here the translator will search in your service.yml.en file and load the correct translation key.
If you want to do advance translations, you can use vars like that (for example) :
{{ (className|lower ~ "." ~ field)|trans({}, className|lower) }}
Here if you have a "user.yml.en" and you want to load something like "user.width", it'll work.
Good luck :)

Resources