What would the TWIG equivalent of <?php the_field('field_name'); ?> be? - php

I'm using Wordpress with the "Advanced Custom Fields" plugin, and I'm trying to call variables from the plugin into my TWIG file.
The documentation for the plugin says to use <?php the_field('field_name'); ?> in the PHP, but I can't figure out how to translate that code to TWIG.
I tried the following:
Running code as is (but it appears TWIG doesn't run pure PHP.)
{% post.the_field('my_fields_name') %}, but to no avail
{{ post.my_fields_name }}, but it printed/logged "Array" to the frontend.
{{ post.get_field('the_field', my_fields_name) }} and
{{ post.get_field('the_field', 'my_fields_name') }}
Any help will be greatly appreciated

Took me a while, but ended up being an easy solution! Turns out these custom fields are stored in the meta-data, so all I had to do was {{ post.meta('my_fields_name') }}

Related

How to run php code in Twig template file [duplicate]

I have a do_shortcut and I need to embed it in a twig template. I tried by coping the code in a php file my-code.php:
<?php do_shortcut('[my-code]'); ?>
Next, in the twig page over.twig:
{{ include ('options/my-code.php') }}
/* I also tried */
{% php %}
<?php do_shortcut('[my-code]'); ?>
{% endphp %}
But doesn't work. Any suggestion? Thanks.
You can't do that, you should create a twig extension and transform the php function into a twig function: http://symfony.com/doc/current/cookbook/templating/twig_extension.html
About the include part, create a my_code.html.twig file at app/Resources/views/my_code.html.twig and copy-paste your code from my-code.php
Then you can include that code anywhere like :
{% include 'my_code.html.twig' %}
EDIT: tested and working in symfony3
Try this code:
{{ wp.do_shortcode('[shortcode]')|raw }}

Find Symfony Widget Content File Location

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 ;) )

Symfony2 turn autoescaping off in php template

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 }}

how can I var dump a block/node content in drupal 8

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.

Laravel Multi js Include

I am new to Laravel so my problem is that I am trying to add multiple script files to my blade.php page using this code:
{{
HTML::script('js/bootstrap.min.js');
HTML::script('js/Chart.js');
}}
without any results , am I doing anything wrong or misunderstood some concept, please specify the best way to achieve my goal
only first include is working, the second one is not including
Thanks
You can't have line breaks inside Blade tags (at least not in Laravel 3). What you need to do is to add {{ ... }} for every HTML:: you have.
{{ HTML::script('js/bootstrap.min.js'); }}
{{ HTML::script('js/Chart.js'); }}

Categories