I am trying to override block theme and rebuild it with html and twig.
I cant seem to find the variables from the block type or content type and cant find the image url for example.
how can i reach it using kint?
The easiest way to dump everything is with
{{ dump() }}
inside your twig template.
I work on fairly large Drupal sites, and I use this to not exhaust the memory from looping through vars.
<ol>
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
</ol>
This will dump everything into a nice ordered list.
Hope this helps!
Also I'm not sure if you're already doing this, but if not -- turn on the twig debug tool, then check out your inspector of choice, and it'll give you suggestions and override data.
You can do this inside your sites/default/services.yml with
twig.config:
debug: true
If you have kint (of Devel module) installed, just use:
{{ kint(_context) }}
Its better than {{ dump() }}, cause kint can manage when the recursion is too long, avoiding memory issues. Second, have a nice way to display the information.
Related
EDIT: Doing some digging, and it looks like my problem stems from the fact that the form I'm creating builds off of the FOSUserBundle's registration form. Why is that an issue? Because while their RegistrationController throws a REGISTRATION_FAILURE event if the form isn't valid, it doesn't look like the event has a listener. So, the invalid state doesn't actually do anything aside from not being handled at all by the system. It doesn't even return the form (with errors).
I have a relatively simple form theme I want to use in order to display my form errors in a block at the top of my form:
{% form_theme form _self %}
{% block form_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<div class="errors pt-2 mb-2">
<ul>
{% for error in errors %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
I reference it in the form template with:
{% form_theme form 'Form/errors.html.twig' %}
And then I try to invoke it with a simple:
{{ form_errors(form) }}
But, when I intentionally provide incorrect data to my form inputs and try to submit, there's no error div present. The submission fails, due to there being 6 validation errors, but no errors are displayed/in the HTML source.
Looking at the documentation, I think I'm doing it right, but I'm obviously missing something.
You have two problems and you have to solve both of them in order to make the form validation work.
1st Problem:
First problem is that the listener is not working. If you intentionally make errors in the form data and the form validation is not kicking in, then you will never see the form_errors template. You have to go over the documentation for the FOSUserBundle.
First, check if you enabled/configured the validation.yml correctly.
Secondly, check the PHP class that generates the form. Make sure that the validation criteria are set properly.
Now, the most reliable way to check if the form is producing errors is to {{dump(form)}} in the Twig template. It will show all the data for the form, including the errors.
Keep working at it until you manage to see some errors. Otherwise you will not be able to do the next step.
2nd Problem:
The second problem is that you probably not including the correct template. I can not count how many times I had the same problem. You may have 3 or 4 templates that contain html/twig code for <form> but Symfony actually uses like 1 or 2 of them. Add your code to every one of them and make sure that your Twig block is not overridden by some other form block.
Also keep in mind that you have to clean both the app cache (php bin/console cache:clean -e prod) and the browser cache after every change in the templates. You might have done everything right and the damn cache is showing the cached results.
One last advice: use the browser developer tools. Often you find a lot of information and errors are shown in the tools' console.
what is the error of form?
For throwing error you have to set asserts on your entity.
for example:
class User extend baseUser
{
/**
* #Assert\NotBlank
*/
public $name;
}
for more information: https://symfony.com/doc/current/validation.html
I am using a PHP product called Mautic which leverages the TWIG template language.
They have some tokens that are accessed similar to
{subject}
and
{contactfield=company}
I want to be able to use the | raw function but when I try
{contactfield=company | raw}
it breaks on me.
If I set a variable then I can use the double curly braces and the functions like raw.
So I am thinking that I need to set a variable to the contactfield=company and then I can access it via the double curly braces. This is how I tried to do it but no luck.
{% set myvar = contactfield=company %}
I tried to use the dump() to guess at what the variable name might be in the context but it appears that command is disabled.
I'm not sure what the syntax should be.
UPDATE
I ran the following code
<ol>
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
</ol>
and got this result
isNew
slots
content
email
template
basePath
app
cfos
_parent
I suspect that the {contactfield=company} is probably some shorthand for an attribute on one of these variables but not sure how I can dig into the top level context variables to see what other data may be lurking underneath.
I tried using {{dump()}} in the template but that seems to be disabled.
If I knew how to inspect these top level variables I might be able to figure out where this information lives. Unfortunately I don't have access to the PHP environment so I have to discover the information through trial and error.
For the first time, i am working on Symfony widgets.
{% render "EducateToolsBundle:Shared:selectMenu" with {'entity': 'Stores'} %}
its Meaning is SharedController -->selectMenuAction
{% form_theme form 'EducateToolsBundle:shared:_form_theme.html.twig' %}
What is the meaning of this.
From where i am getting the values to this form .?
You must look at selectMenuAction() method in the Shared controller. It is that function that set what template is used. if using the default Symfony coding standards it should be something like selectMenu.html.twig in a Shared sub folder in the Resources/views of the bundle.
You should looks at the documentation first :
https://symfony.com/doc/current/form/form_customization.html
Because we won't teach you symfony here, it's very well explain in the documentation and it is a task too big for us.
In your case theForm.id is the field of the formType we want to displays.
If we want to display the <input> of an form of user lastname we would use
form_widget(userForm.lastname)
form_widget tells symfony to display only the widget (the ) of the id given.
if you want to have a label + input + the errors of the field you could use
form_row(userForm.lastname)
wich is almost equal to
<div>
{{ form_label(form.lastname) }}
{{ form_errors(form.lastname) }}
{{ form_widget(form.lastname) }}
</div>
(it depends of the form theme, but you should read the doc for the details ;) )
I have picked up the basics of using TWIG to create my site. I suppose I know how to use {%extend%} {%block%} {%include%} and {%set%}, in the most general sense.
I would like to include a block of code from within another twig file without including the whole file - as opposed to {% include 'file.twig' %}.
I have figured out how to set a variable in file.twig and output it using {{ variable | raw }}. I would like to do that in another file, like you would with using jQuery's .load function.
I swear the twig documentation does not seem to touch on this, it seems like really obvious and basic functionality. I have messed around with various combinations of include, for, with, in and only, colons and commas and whatever | is, and nothing.
I believe you are looking for horizontal inheritance via the use tag:
The use statement tells Twig to import the blocks defined in blocks.html into the current template (it's like macros, but for blocks)
The confusing part is that by itself, {% use ... won't actually insert the content of the blocks in the referenced template. To do that, you must use the block(...) function:
index.twig
{% use "blocks.twig" %}
{{ block('name') }}
blocks.twig
{% block name %}
<h1>Alex Weissman</h1>
{% endblock %}
{% block hobby %}
<p>Blanchin'</p>
{% endblock %}
For a working example, see my TwigFiddle (yes, it's a real thing!): http://twigfiddle.com/jjbfug
There is way to turn autoescaping off in twig template in Symfony2. Like so:
{% autoescape false %}
{{ child.vars.label }}
{% endautoescape %}
How can I do something like this in PHP template? I got expanded choice type in my form, and I need to use images as labels. But Symfony keeps escaping my img tags.
Edit:
Sorry for misunderstood, but php templates have no autoescaping. If you want escaping in php templates you must do:
<?php echo $view->escape($var) ?>
Original answer:
In Twig use raw filter - http://twig.sensiolabs.org/doc/filters/raw.html
{{ child.vars.label | raw }}