I want to pass constant as an attribute in the twig template.
Code looks like this:
{% if presenter.hasErrors(constant('Admin\App\ViewPresenters\EditorPresenter::FORM_ERRORS') )%} and it throwing error Message: Argument 1 passed to Admin\App\ViewPresenters::hasErrors() must be of the type string, null given, called in /vendor/twig/twig/src/Extension/CoreExtension.php on line 1527, as far as I understand twig did not recognize it's an constant. Is there any wat to pass constant in twig as an argument?
In twig a backslash is used to escape special characters e.g. {{ '\'' }} will output '. So to create a litteral backslash in twig you need to "escape" the backslash
{{ constant('Admin\\App\\ViewPresenters\\EditPresenter::FORM_ERRORS') }}
You can see the difference in the outputted source code twigfiddle:
{{ constant('Admin\App\ViewPresenters\EditPresenter::FORM_ERRORS') }}
PHP source
echo twig_escape_filter($this->env, twig_constant("AdminAppViewPresentersEditorPresenter::FORM_ERRORS"), "html", null, true);
{{ constant('Admin\\App\\ViewPresenters\\EditorPresenter::FORM_ERRORS') }}
PHP source
echo twig_escape_filter($this->env, twig_constant("Admin\\App\\ViewPresenters\\EditorPresenter::FORM_ERRORS"), "html", null, true);
demo
Related
In Liquid, one can remove a character X from a liquid string like so,
{{ 'random_string' | remove:'X' }}
E.g. the { character can be removed in this way.
However, removing the } character this way gives the following error,
Liquid syntax error (line 15): Variable '{{ 'random_string' | remove:"}' was not properly terminated with regexp: /\}\}/
Which probably has to do with the usage of the } character in the Liquid language.
Question
How do I remove the } character from the string?
One solution would be using the url_encode filter, then removing the encoded string:
{% capture my_variable %}hello}}worl}d{% endcapture %}
{{ my_variable | url_encode | remove: "%7D"}}
First, create your own tag.
<project>/<app>/templatetags/clean_string.py
Insert this code into it.
import re
from django import template
register = template.Library()
#register.filter
def clean_string(value):
regex = '[^-_.,!?#%\w\d]+'
return re.sub(regex, '', value)
After that, register it in the settings...
<project>/<projectName>/settings.py
'OPTIONS': {
'context_processors': [
...
],
'libraries': {
'clean_string': '<project>.templatetags.clean_string'
}
},
At the beginning of your .html template Add tag loading
{% load clean_string %}
This filter can be applied to any string.
For example:
{{ '[tes{t}!'|clean_string }}
The filter can be adjusted in a variable ...
regex = '[^-_.,!?#%\w\d]+'
Output:
test!
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.
I'm struggling to find the correct Twig concatenation syntax.
Here's what I've got:
{{ this.page.baseFileName|page({ (__SELF__.pageParam): page }) }}
it gives the url with the pagination number at the end of the url:
http://sites.local12/category/style/rock/2
Now I want to insert a colon before the pagination number like this:
http://sites.local12/category/style/rock/:2
How to achieve that ?
Ok I've got it. I was confused by the "page" filter (I'm new to Twig).
{{ this.page.baseFileName|page( {(__SELF__.pageParam): ':' ~ page }) }}
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') }}
This question maybe kind of silly but I'm a newbie for symfony anyway.
prescenario I pass a variable from controller into index.html.twig by doing this
return $this->render('index/index.html.twig', [ 'department'=>$departments,'URILink'=>$URILink,'departmentDetail'=>$departmentDetails,'contentCell'=>$this->mContentCell ]);
After using {% dump %} it shows me
"department" => array:3 [▶]
"URILink" => "http://localhost/index/department/"
"departmentDetail" => array:1 [▶]
"contentCell" => "department.html.twig"
Then I need to reuse the variable contentCell as string in template to form syntax similar to this ;
<div>{{ include ('department.html.twig'),[departmentDetail:departmentDetail]</div>
For my first attempt I tried this,
<div> {{ include ({{ContentCell}}),[departmentDetail:departmentDetail]}} </div>
Unfortunately it showed me this error
A hash key must be a quoted string, a number, a name, or an expression
enclosed in parentheses (unexpected token "punctuation" of value "{".
Any idea how could I use the variable
contentCell as string value appropriately?
try this:
{% include contentCell with { departmentDetail : departmentDetail} %}
Answer here pass data to twig
You can include a template like this per:
{{ include('YourBundle:ControllerName:yourAction.html.twig', {'variableName': yourData}) }}
Or like this per http://twig.sensiolabs.org/doc/tags/include.html
{% include 'template.html' with {'foo': 'bar'} %}
after try'n try at last I found the the trick how to do it
i take similar analogy for
{{dump(var)}}
so attempt to do this
{{include (contentCell,{'departmentDetail' : departmentDetail}) }}
and it work like charm :) nice