Trying to load a WP Plugin Shortcode from a .twig file - wordpress

I tried asking this question on a related thread, but it was deleted without any notification (I understand I wasn't offering a solution, but a heads up that you were deleting my post would've been helpful...I'm new here). I'm retrying here:
I have never used Timber before and have the Easy Accordion Pro plugin with a generated shortcode of "[sp_easyaccordion id="352"]" and have tried the following in my .twig template file (which lives in Themes>My Theme>templates) and all that renders is plain text [sp_easyaccordion id="352"]. Any thoughts?
{% function('do_shortcode', '[sp_easyaccordion id="352"]') %}
OR
{{ function('do_shortcode', '[sp_easyaccordion id="352"]') }}
OR
{% filter shortcodes %}
{{ wp.do_shortcode('[sp_easyaccordion id="352"]') }}
{% endfilter %}
OR
{{ wp.do_shortcode('[sp_easyaccordion id="352"]') }}
AND simply
{% filter shortcodes %} [sp_easyaccordion id="352"] {% endfilter %}
None of these worked. Please also note that I did not add anything to my function.php file...is that something I'm missing?

According to the documentation it should be:
{% filter shortcodes %}
[sp_easyaccordion id="352"]
{% endfilter %}
Otherwise you should also be able to call a function as such:
{{ function('do_shortcode', '[sp_easyaccordion id="352"]') }}
https://timber.github.io/docs/guides/functions/#function

Related

Symfony-twig template form theme error

I have everything working correctly and now I'm trying to work with form themes. This is my code to generate the form without a theme.
{% extends 'base.html.twig' %}
{% block body %}
{% include 'menu/menu.html.twig' %}
{% if addpost is defined %}
<div id='add_post_form'>
{{ form_start(addpost) }}
{{ form_widget(addpost) }}
{{ form_end(addpost) }}
</div>
{% endif %}
{% endblock %}
But when I'm adding a form-theme with the following code
{% form_theme form 'form/form_div_layout.html.twig' %}
I get this error:
Variable "form" does not exist
When i execute this without the line, I'm getting the following error:
Unknown "form_help" function. Did you mean "form_rest", "form_end"?
the form_div_layout.html.twig contains the code found at github symfony twig form theme
At my config.yml I've added the following under the twig section,
form_themes:
- 'form/form_div_layout.html.twig'
.
either not, i still have this error
what is missing ???
My file structure
If all your forms are going to use the same theme you only need to add the line in your config, but if you want a particular form theme in a particular template you can use the template tag.
The reason you're getting the 'form is not defined error' is because you don't have a variable called form passed the the template, your form variable is called addpost, so you need to use
{% form_theme addpost 'form/form_div_layout.html.twig' %}

Using db arrays in set and include in symfony3

I'm a beginner in symfony. I love use database-arrays to fill out the pages in symfony as
<p>{{ cars.texte | nl2br }}</p>
Is it possible to use the same proceed in "set" and in "include", as
{% set tag = 'url' %}
letting the db complete the url
and
{% include "twigtemplate" %}
letting the db complete the template? This, as I have understood, doesn't work:
{% set tag = {{ cars.color }} %}
Thanks for any help!
Yes it is possible simply remove the {{ and the }}. When you are inside of the {% %} signs you don't need the {{ and }}.
{% set tag = cars.color %}

How are labels on ArrayCollections in forms rendered?

I have a Customer object, which has many Emails.
I'm building a form for my customer, and I've added his emails as a collection. In my template, I render the emails portion like this:
<h4>Emails</h4>
{% for email in form.emails %}
<li>
{{ form_row(email.addr) }}
{{ form_row(email.isPrimary) }}
</li>
{% endfor %}
...
{{ form_rest(form) }}
This works fine, except if the customer has no emails. Then, form_rest() renders the label 'emails' at the bottom of my template.
Why does this only get rendered when form.emails is empty? How can I customize it? (Note I've already customized my label rendering for other form elements, and I don't want it to be the same for these 'collection labels'.)
I usually solved this problem this way:
{% for email in form.emails %}
{# ... #}
{% else %}
{{ form_widget(form.emails) }}
{% endfor %}
Unless someone suggests a better way of doing this.

symfony2 - twig - how to render a twig template from inside a twig template

I have a xxx.html.twig file which shows a page, but when I want to refresh the page with different data and just update it with new data, I have a select and a submit button for it.
The thing is that I don't know how do I call an action in the controller which I pass parameters to from my twig and call for new data and then I render the same twig template again with new parameters.
How do I do so?
Here are a few different ways:
{{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": hostid } ) }}
or
{% include 'MyCoreBundle:Helper:test.html.twig' with {"hostid2": hostid } only %}
or
{% render controller("MyCoreBundle:Helper:test", {'hostid2': hostid}) %}
Symfony 2.1:
{% render 'YourBundle:YourController:yourAction' with {'var': value} %}
Symfony 2.6+:
{{ render(controller('YourBundle:YourController:yourAction', {'var': value})) }}
And, of course, read the documentation.
I think some parts are depricated here.
To make the include work in latest Symfony 3.1.10, I solved it like this:
{% extends 'base.html.twig' %}
{% block body %}
{{ include('AppBundle:Default:inner_content.html.twig') }}
{% endblock %}
Note: include() with parentheses.
Then all the variables are included from the parent template. If you like to restrict some variables in the child template, you use with ... only (look over)

Is there a complete list of variables that can be used in form_div_layout.html.twig?

I want to get all the variables available in the Symfony form theme file form_div_layout.html.twig, I read the Symfony official documention and searched on the web, but couldn't find any useful information on this, can someone help me?
Well, you can get all the available variables in each block by iterating the context:
{% block form_widget_simple %}
<ol>
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
</ol>
{% spaceless %}
{% set type = type|default('text') %}
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% endspaceless %}
{% endblock form_widget_simple %}
And if you want to use yours, then you'll have to overwrite the classes that actually are rendering those widgets, just take a look at AbtractType::buildView...
As #Gregoire suggested, you can use {{ dump(_context) }} from version 1.5 (http://twig.sensiolabs.org/doc/functions/dump.html), but be aware that it will print a big amount of info.
I hit the same problem recently, being the lack of documentation on the available variables (attributes) when working in themes. In the end I found my solution by searching through the vendor folder (took a while) for the variables I did know, to see what else is available.
The best place for me was to look in here: Symfony\Component\Form\Extension\Core\Type
The base type, being FieldType provides these variables via buildView
$view
->set('form', $view)
->set('id', $id)
->set('name', $name)
->set('full_name', $fullName)
->set('errors', $form->getErrors())
->set('value', $form->getClientData())
->set('read_only', $form->isReadOnly())
->set('required', $form->isRequired())
->set('max_length', $form->getAttribute('max_length'))
->set('pattern', $form->getAttribute('pattern'))
->set('size', null)
->set('label', $form->getAttribute('label'))
->set('multipart', false)
->set('attr', $form->getAttribute('attr'))
->set('types', $types)
;
prototype is an attribute that only exists in the collection type, as is allow_add and allow_delete, see CollectionType in the same folder.
After the base FieldType, this appears to be the complete list.
CheckboxType.php: ->setAttribute('value', $options['value'])
ChoiceType.php: ->setAttribute('choice_list', $options['choice_list'])
ChoiceType.php: ->setAttribute('preferred_choices', $options['preferred_choices'])
ChoiceType.php: ->setAttribute('multiple', $options['multiple'])
ChoiceType.php: ->setAttribute('expanded', $options['expanded'])
ChoiceType.php: ->setAttribute('required', $options['required'])
ChoiceType.php: ->setAttribute('empty_value', $emptyValue)
CollectionType.php: ->setAttribute('prototype', $prototype->getForm());
CollectionType.php: ->setAttribute('allow_add', $options['allow_add'])
CollectionType.php: ->setAttribute('allow_delete', $options['allow_delete'])
DateTimeType.php: ->setAttribute('widget', $options['widget']);
DateType.php: ->setAttribute('formatter', $formatter)
DateType.php: ->setAttribute('widget', $options['widget']);
FormType.php: ->setAttribute('virtual', $options['virtual'])
MoneyType.php: ->setAttribute('currency', $options['currency'])
PasswordType.php: ->setAttribute('always_empty', $options['always_empty']);
RadioType.php: ->setAttribute('value', $options['value'])
TimeType.php: ->setAttribute('widget', $options['widget'])
TimeType.php: ->setAttribute('with_seconds', $options['with_seconds'])
See my answer here: https://stackoverflow.com/a/41020474/5758328
You simply need to use
{% dump %}
and all variables available in the template will be dumped to the profiler
You can pull all of the ones out of the original file, and only overload the ones that you need:
vendor/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Resources