New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Override Timber templating sytem #1769
Comments
Hi, Just write your template (PHP file) as usual, without calling any Twig file. Your template will render correctly in accordance with the usual WordPress template hierarchy. Timber is powerful as it allows you to "twig" the files you want and to use the classic full PHP method when needed. Edit : The only problem you'll encounter is that the main structure of the website is usually contained in a Then the best solution would be to create your PHP file, render a Twig file which extends |
Thanks for the quick response. So I create a custom template and for testing I grab the normal WP Page code. any suggestions? |
So it is just printing out get_template_part. {% extends "base.twig" %} {% block content %} get_template_part('test.php') |
You can't use PHP like that in a template. To call methods like that you need to tell Twig that it's a PHP call and not merely a piece of text... {% extends "base.twig" %}
{% block content %}
{{ function('get_template_part("test.php")') }}
{% endblock %} |
@redsoxfan2499 exactly :) And in your template part, you just have to write what needs to be output in the main content of the page, as the global things (eg. header, footer, sidebar...) should be rendered by extending the |
Guys, thanks for all the input and help. So I am using Jareds exact code. I have the test.php file in same directory as my test.twig file and nothing is printing out. The header and footer from the base twig is working just not my test.php getting called. {% block content %} {{ function('get_template_part("test.php")') }} {% endblock %} test.php file . I am just echoing out Test. |
@redsoxfan2499 it's possible that there could be path issues (I've never tried to call a template_part from within a Twig file). Try making a function in your |
Well, I have tried multiple things without success. So since I a new to Timber, I went to learn using the Video Tutorials which are out of date. So then I went to the Documentation. Of course I have downloaded Timber plugin and activated it. I also downloaded the starter theme from the docs. The docs are out of date a bit from what I am seeing on in the starter theme. So following directions trying to learn template inheritance, I tried to extend the single.twig file and then created a single-test.twig file. The single.twig file of course extends the base.twig file. then following the directions, I added this to the single.twig
here is my single-test.twig file. {% block title %} TEST{% endblock %}` It does not work. I also noticed in the docs it states create files in the views folder but in the starter theme it is called templates. I am using Timber 1.7.1, WP 4.9.8, and ACF 4.4.12 and ACF Pro 5.7.2. |
Yes some of the docs are out of date, but we are working on fixing that. here is some code that I use for my base in my template. In the
I took out a lot of my other code such as next/previous functions, comment function and just put in the basics. My directory structure is all under
Here is my
Once again I removed a lot of the other code I have, but when I call the base, I am extending from that, not from single.twig. I am using bootstrap so for me I use calls to partial files with a reference to what I am calling. Using partials allows me to easily customize my base template for a client development job. Following the same logic here are the locations of the files I am calling. and
Here is my
Like I said earlier I use quite a bit of conditionals to easily modify per client. But for testing try this:
You can get the contents of the header blocks from the starter template. And as for
|
thanks for the help. I am using the timber starter theme from github. The link provided in the docs. The single.php is located in the starter theme root. then there is a templates directory where all of the twig files are located. There is the single.twig which extends base and works. Then following the directions from Timber, https://timber.github.io/docs/getting-started/theming/ . I am trying to extend the single twig following the directions. I added the code in single.twig and then created a new file called single-test.php I have also tried in the footer. I created a footer-test.twig which extends footer and created a block and nothing. I also went to the Twig documentation and it seems I am doing everything correctly. It does not want to extend pass the base twig |
Also, do not give up. The learning curve on Timber is not that bad. I used it originally in a project where the load times were ridiculous. Once Timber was used, load times went from 35 seconds to 5-6 seconds. After that I never looked back. I use my starter theme on all of my projects and my creatings separate partials I just call the ones I need. In your original code, it looks like you are extending the single.twig file, instead of the base file. It has been a while since I worked with the starter template. If you can zip up and send me your full template file for the starter theme, I can take a quick look. I have a free area in my local host right now. let me know |
Ok thanks will get to it in a few minutes. But I think the problem may be this from the starter theme
As it does not call the post name. I will get back to you soon |
Yes that was the problem.
to
|
On a side note I use debug bar whenever working with timer as it gives me the template files being called and all of the content variables. Install these two plugins: Debug Bar Then in wp-config.php set from
to
|
Jared, Maybe this should be noted to whoever is redoing the new starter template |
jake and everyone thanks again for all of the help. I changed the line single.php file as noted. But this did not fix my issue. I still cannot extend the single.twig template. Also, thanks for the advice on the Timber Debug Bar. I will attach screenshots. In the single.twig. I had the block headline in the single.twig. The title was already being displayed without the suggested changes. I then create a file called single-test.twig which I extend single and then add a echo TEST inside the block headline. when my page renders I am not seeing TEST. |
Strange. All I did was installed the template file you sent me, created a post called "Test", then I edited the single/php to include the slug. Here are my screen shots. Single.php Just a thought but do you have a plugin installed that is maybe conflicting? |
This is strange. So, I download a new copy of WP and spun up a fresh instance of WP on my local MAMP. I installed TImber and the starter theme I have been using since you can get it working on your local abut ti still does not work. What version of Timber plugin are you using? I am using version 1.7.1. I have minimal plugins now and still nothing. Using the Timber debug, it shows the single template and not the single-test one like your screen shot thus that is why TEST is not showing. The issues is WHY does the single-test.twig not work. |
I am using the same version as you. What is the slug of your post? Looks like it is hello-world. |
OK. that worked. When I title the new Post Test. It uses the single-test.twig template. When I create another new post and title it, it does NOT work and does NOT use the single-test.twig template. Does it have to do with the name of my twig file? I would except the single-test.twig to be used for all single posts? |
It has to do with the fact that that override is based upon the name of the WordPress post, hence the slug as explained in the documentation you read. What exactly are you trying to do? |
For all single posts, you would modify just single.twig. Then ALL single posts would change to that. The way you were setting it up, the post with the name "test" is the only one that would get modified. If you want to modify all singe posts, then just recode single.twig. But caution, all single posts will get modified. The template naming convention is the same as WordPress naming convention. |
Light Blub just went off! I gotcha. I thought that this would just extend every single post. In this current website I inherited, the previous developers has like 5 twig files making up one page. So I am just trying to learn Timber/Twig so I can support this website and was trying to follow directions. So if I wanted to create a file to be include in single I would need to use the includes function. Plus when I started watching the videos and then realized they were out of date. Then I started reading the docs and grabbed the starter theme and saw it was also some what out of date, I thought it was not working because the docs were out of date. Thus I reached out for help. I totally get it now. Makes total sense. Sorry for all of the confusion. Thanks so much for all fo your help. FYI, if I was a developer just checking out Timber I would have already jumped ship with the outdated videos and docs and starter theme out of date and then ran into an issue. I have to learn Timber so I am in it. I am sure after I finish this learning curve which does not seem to bad, I will really like Timber. Jake46a thanks so much for all fo your help thanks to everyone else who jumped in early. |
@redsoxfan2499 |
BTW, my original issue was there is a complicated template I need to create and I wanted to do it outside of Timber since I know normal WordPress and knew I could get it done faster. But without success, I then started to learn Timber and ran into that issue. So I am going to give it a try using Timber. So far I like this better then Genesis. Thanks again! |
I am new to Timber and I have inherited a site that is using Timber and Twig. I need to know if there is a way to overide or skip over the Timber system for one template. I have a very complicated custom template I need to create and I want to bypass the system for this one template. I have tried in both the functions, index file to check for if the template is being used to not load all of the Timber files but not having success.
Thanks in advanced for any help.
The text was updated successfully, but these errors were encountered: