Only load styles for a certain page. If page is - Using Timber/Twig - timber

Using timber/twig for wordpress. Trying to load JS but only for a certain page.
I figured this would be something like
{% if page = mypage %}
<link......./>
{% endif %]

Actually the answer to your question is not depending whether you use Timber or not. You should use conditional statements with the combination of WordPress enqueue functions.
You can see the examples of such approach for example here:
http://geoffgraham.me/wordpress-load-files-on-specific-pages/
The topic is broad and you have a variety of solutions you may use here. Nevertheless I hope my answer helps you move into the right direction.

Related

Including Drupal taxonomy term links in a twig template

I am trying to put some Drupal taxonomy links into a twig template, but have hit a wall, hoping someone can help me out.
Here is the setup, and what I have tried so far...
I have a taxonomy vocabulary called "FAQ Category". Inside of it, there are several terms, for example, one of them is named "The Basics".
I also have a Drupal twig template named views-view--faqs.html.twig, which is displaying all the FAQ content types that have been tagged for a particular term. (...as a side question, I do not see a Drupal view that has been setup for this, so I dont know how views-view--faqs.html.twig is even working in the first place, but it is...)
What I need, is to have a simple navigation to each of the taxonomy terms on that template, but I dont know how to do it. I have tried things like:
{{ node.field_basics.value }}
{{ node.field_faq-category.entity.label }}
{{ content.faq_category.0.value }}
...but nothing is working. I am guessing there is a very simple solution, if anyone can help it would be much appreciated. This site was not developed by me, so I am doing a bit a reverse engineering.
If there is any more info you need to help get an answer, just let me know, I will provide as much info as I can.
Thanks in advance.
I would suggest you to take a look at the following guide:
https://www.drupal.org/docs/theming-drupal/twig-in-drupal/discovering-and-inspecting-variables-in-twig-templates
Here you can learn how to output values or even reading field keys.
I believe you should try either:
{{ node.field_basics.0.uri }}
or
{{ content.faq_category.0.uri }}

wordpress simple shortcodes not rendered when using Timber plugin

I've recently taken over dev of a Wordpress site using Timber (which I wasn't familiar with). I'm attempting to use a new plugin and accompanying shortcode, which of course doesn't work.
I've been researching this for a couple hours and there doesn't seem to be a simple answer. In order to use a simple shortcode like this:
[sp_faq category="7"]
Do I really need to create a custom shortcode function in functions.php, add some sort of template file for it, etc? This seems counterintuitive to Twig's making things 'simple'.
The documentation for this is less than stellar unless I'm missing something obvious.
You could try this (example using a gravity form shortcode):
{% filter shortcodes %}
[gravityforms id="1"]
{% endfilter %}
If you want to render a shortcode from a custom field you can do it like this:
{{post.custom_shortcode_field|shortcodes}}
Taken from the docs:
https://timber.github.io/docs/guides/filters/#shortcodes
Or if it's comming from the main-editor try:
{{ post.content|wpautop }}
Seems like this is too late for OP, but for anyone else coming across this: The correct solution is combined in the Luckyfella's answer and the comments on it. I think OP would have got it working if he had tried Luckyfella's final suggestion.
You need to put {{post.post_content|wpautop|shortcodes}}* into your Twig file(s). This will render both auto paragraphs and shortcodes that are put into the main WYSIWYG editor, by default.
*post is simply the conventional default name in Timber for a TimberPost, you will also need to check in your PHP template files to see what the Timber context and Timber post variables are called. For OP it seems to have been page, not post.
Just thought I'd chime in here.
post.post_content contains the raw data that is contained in the database (before any filters have been applied to it) and post.content contains the data after the filters have been applied, so using the filter |shortcodes shouldn't be needed to be run on that.
I was using post.post_content for some reason and found this question because I was trying to figure out why my shortcodes weren't working and hence it led me to do some more research.
Now, reading the answers in this question, I wasn't really satisfied with as I had also been using |e('wp_kses_post') to sanatise the data that I output, but if I used something like this:
{{ post.content|wpautop|e('wp_kses_post') }}
...then obviously I would get the correct filtered data with the shortcodes processed, but it would also at the same time strip out any non-allowed data with the e('wp_kses_post') filter.
Sure, you could add allowable tags within this filter, but obviously that isn't very realistic as you don't know exactly what output the shortcodes will be outputting nor do you want to keep updating it.
So, we had a problem... we want to allow shortcodes to be parsed, but also sanatise the content at the same time - what to do!?
The solution is the below:
{{ post.post_content|wpautop|e('wp_kses_post')|apply_filters('the_content') }}
Here we use post.post_content so we have the content before it has been filtered, then after the wpautop filter it is followed by e('wp_kses_post'); this will sanatise the data, but the important part being it will leave shortcodes alone, so they can be still be filtered!
Lastly, we apply the filters for the content with the apply_filters filter, and this then takes care of all the filters applied to the_content including parsing the shortcodes.
Though if you are already using universal escaping you will have to consider how the above will apply to your situation.
Reads like you want to use a shortcode outside of post.content.
You can use {% function('do_shortcode', '[shortcode here]') %} to process a shortcode where ever you want to in a template.

Typehint (intellisense) objects within a twig loop

I've searched for a solution but I can't find any suitable for Twig, I know it's a quality of life thing. But it would make templating slightly easier.
Example
{% for userObj in userObjCollection %}
{{ userObj.property }}
{# I want to type hint userObj so I can use intellisense to get the right property #}
{% endfor %}
I'm currently using the PHPStorm IDE,
any suggestions or pointers are greatly appreciated.
Edit
Note that this is pure a question for an easier way to code within the loop. Not something a user of a site would come in contact with. (Also added phpstorm tag to clarify)
Edit 2
I've got the Symfony2 and PHP Annotations plugins installed (Sorry for not pointing this out earlier)
Use this in your twig:
{# #var foo \FooObject #}
Personally I use the Symfony Support plugin for PhpStorm, it has auto-complete for twig objects and even repositories.

How to render a submenu in symfony-cmf

I am desperately trying to render a submenu in symfony-cmf.
Example
Structure:
page1
├─p1-subpage1
├─p1-subpage2
└─p1-subpage3
page2
├─p2-subpage1
└─p2-subpage2
Whenever the current page is somewhere within the page1 hierarchy it should use p1-subpage* to render the menu, when I am within the page2 hierarchy it should use p2-subpage* to render the menu. Technically that means it should set the current item to the parent of the 1st level (if it's not already on it) and render one level of nodes (e.g. knp_menu_render('main', { depth: 1 })).
The problem can be split in two parts:
Rendering a (sub-)menu from a given node
Retrieving the current node
Thoughts and Trials
TWIG: It has been suggested to support rendering submenus as a functionality of the KnpMenu itself, but it hasn't been done. As a workaround registering a twig extension has been provided by someone in the issue. However this extension is based on the getCurrentItem Method which has been removed with KNP-Menu 2.0. Although the cmf currently uses v1.1 of the knp-menubundle, this is going to change soon
TWIG: The CnertaBreadcrumbBundle would bring back this functionality, but depends on KNP-Menu 2.0 as well.
TWIG: Using a hack similar as suggested here. It checks the current URI, counts the number of slashes and decides based on that what to use. This could probably work. Problem here: I don't have cmfMainContent variable defined, nor can I find anything similar in my {{ dump() }} (nothing containig a menu either).
RouteVoter: The cmf itself has some MenuVoters itself, which are well documented what they are, but not how to use them. I don't think that there is any way to access that functionality whithin twig nor do I know how to intercept the menu building.
Thanks for any help.
have a look here for an example of using voters to make decision about what to highlight:
https://github.com/dbu/conference-tutorial-1.0/pull/20
aside from this we are making good progress on a KnpMenu 2.x compatible version of our MenuBundle but it might be until January until we make a stable release of it (but we might make one earlier .. we will see):
https://github.com/symfony-cmf/MenuBundle/pull/214
I have created a bundle yesterday for my own, similiar, use case.
However as all of my pages share the same route you might need to adapt it quite a bit.
I still think you might find some inspiration, especially for the second part of your problem.
My Bundle:
https://github.com/burki94/RecursiveMenuBundle/blob/master/README.md
AbstractRecursiveBuilder: https://github.com/burki94/RecursiveMenuBundle/blob/master/Menu/AbstractRecursiveBuilder.php:
This is not really a solution because it's not following my requirements of being compatible with KnpMenu 2.*. But this deprecated solution is easy:
{% set currentItem = knp_menu_get('main').currentItem %}
{% if currentItem is not null %}
{% if currentItem.getLevel() == 1 %}
{% set main = currentItem %}
{% else %}
{% set main = currentItem.getParent() %}
{% endif %}
{{ knp_menu_render(main, { 'template': 'ComBundle:Default:left_menu.html.twig', 'currentClass': 'uk-active' }) }}
{% endif %}

Django- CMS: Plugin position in a placeholder

I was looking for a way to check the plugin's position within a placeholder in Django-CMS. I found this question Detect last plugin in a placeholder
Is "plugin" a django cms keyword? I can't find documentation about it.
My question is: How can I obtain information about the plugins rendered within a Placeholder?
Thanks
if you don't override your plugins render method (2.4 and up), you'll have your plugin as instance in your context. using the following, you'll get the 1 based position of your plugin:
{{ instance.get_position_in_placeholder }}
also interesting: is_first_in_placeholder and is_last_in_placeholder. in fact, #paulo already showed you the direction in his comment ;) this is the code, with new line number: https://github.com/divio/django-cms/blob/develop/cms/models/pluginmodel.py#L382

Resources