I am not using symphony, just twig.
My structure:
root
- assets
- Twig (library)
- templates
- main_template.twig
- child_template.twig
my main_template.twig is rendering just fine, but is unable to find the child_template.twig which is in the same folder.
I have tried using relative / full path but I can't the child_template to load. What's the path supposed to be? Do I need to do anything else besides adding the following to the child template? (and adding the blocks?)
{% extends "child_template.twig" %}
The path starts relative from the one you have provided in the loader of twig
config.php
require_once '/path/to/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem(__DIR__ . '/templates');
$twig = new Twig_Environment($loader);
child.twig.html
{% extends "main_template.twig" %}
Related
I would like to know how I can call a service from twig template directly on Symfony 4. I am using it in each action like this:
public function indexAction(TranslatorInterface $translator, NavigationGenerator $navigationGenerator)
{
return $this->render('index/index.html.twig', [
'navigationItems'=>$navigationGenerator->getNavigation(self::class)
]);
}
In the template I call this:
{% for navigationItem in navigationItems.topNavigation['left'] %}
{{ navigationItem.label }}
{% endfor %}
In earlier bootstrap versions I could define the service as a global object in config.yml and use it directly from twig like this:
{ NavigationGenerator.getNavigation(ControllerName) }
Any hint how to do this in Symfony 4? There is no config.yml anymore.
You can still define twig global variables - they would just go in with the rest of the Twig configuration in config/packages/twig.yaml.
An alternative, and maybe better place to put that could be a Twig function. The service that defines the function (or filters) are as much a service as the controllers, and so you would type-hint your NavigationGenerator and anything else you needed, in the constructor, for use in the function that is being called from Twig.
I'm attempting to render out a Twig template that has no layout, just a few variables and some loops.
When I call the render() function on Twig, it outputs a block of PHP code for the following class:
php
/** path/to/my/template.html.twig */
class __TwigTemplate_435244378aba3a3f94258b7d2af4d53eb7a41acb741dd3ad0efcac038b621c67 extends Twig_Template
{
// bunch of methods for Twig_Template,
// including the compiled version of my template
}
After this it gives me a stack trace with the following exception:
Failed to load Twig template "path/to/my/template.html.twig", index "": cache is corrupted in "path/to/my/template.html.twig".
I'm not even using a cache with this app currently, though adding a cache doesn't seem to make a different. I'm using Twig like this:
// Factory to return a new Twig environment
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../../views/');
return new \Twig_Environment($loader);
// My class has $this->twig set to the above object
$this->twig->render('path/to/my/template.html.twig', [
'report' => $report,
'file' => $file
]);
Twig seems to be able to read in my template, as the block of PHP code it outputs in the error has a properly compiled version of the template. Attempting to set a cache directory that is writable still results in the same error.
I'm using Twig 1.34.4 and PHP 5.6.29 under Apache.
EDIT
A bit of success, in a way. It seems that Twig is never evaling the code that it's generating.
I edited the vendor/twig/twig/lib/Twig/Environment.php file and on line 448 added the following:
eval("?><?" . $content);
After that my template renders just fine. That leads me to the conclusion something else in Twig is breaking, but this isn't a long-term solution since I shouldn't be editing the vendor files.
The block starting at line 456 seems to indicate that $content should have the opening <? but mine doesn't. So that could be screwing with the compilation.
Time for more digging.
I finally figured it out. It wasn't Twig's fault.
Our deployment process was leaving old files on the disk, so I was running with only part of 1.34.4 upgraded.
Fixed that and everything works.
I am passing a BASE64 encoded string to a twig template that I would like to have a twig extension de-code.
I installed twig with composer and I'm not using any other frameworks and most of the extension examples I've found seem to assume you are, and I think that that is causing me trouble. I can't seem to get twig to find my extension.
So I think I'm having a name space issue. my setup:
root/
-index.php
-vendor/
-twig/
Given this setup, where should I put the extension file and what name space should be at the top of the file? What is the proper way to load it?
Many thanks in advance!
If your app isn't too complex, you can simply add extension in place where you register and load Twig itself.
// index.php
require_once __DIR__ . '/vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader, array(
'cache' => '/path/to/compilation_cache',
));
// an anonymous function:
$base64Decode = new Twig_Filter('base64_decode', function ($string) {
return base64_decode($string);
});
// or a simple PHP function:
$base64Decode = new Twig_Filter('base64_decode', 'base64_decode');
// add the function to your Twig environment:
$twig->addFilter($base64Decode);
I'm using this gulp plugin to use nunjucks to make HTML management easier.
https://github.com/carlosl/gulp-nunjucks-render
gulp.task('default', function () {
return gulp.src('src/templates/*.html')
.pipe(nunjucksRender({
path: ['src/templates/'] // String or Array
}))
.pipe(gulp.dest('dist'));
});
I want to keep my templates and partials in different folders so I tried this to keep as path
path: ['src/templates', 'src/partials']
but it's not working.
Template render error: (unknown path)
Error: template not found: partials/side_nav.nunjucks
My setup
As far as I can see, the issue is in your include. You're already defining your base paths as 'src/templates' and 'src/partials'. Now nunchucks is trying to import src/templates/partials/side_nav.nunjucks and afterwards src/partials/partials/side_nav.nunjucks, which don't exist.
Solution 1
So you would have to include it without the partials part:
{% include "side_nav.nunjucks" %}
Solution 2
If you want to be explicit about your folders (both templates and partials), you could just set you base path to src instead, and include your files like you did:
{% extends "templates/layout_with_sidenav.nunjucks" %}
...
{% include "partials/side_nav.nunjucks" %}
I am using dompdf and twig
I had some success with
how to make dompdf handle twig page
however I cannot use the loader to load the templates using filesystem
given
$twig = new Twig_Environment($loader, array(
'cache' => '/path/to/compilation_cache',
));
I want to impliment the loader above this as
$loader = new Twig_Loader_Filesystem('templates');
however this does not work
if I change loader simply to
$loader = new Twig_Loader_String();
it only renders the page name
so on the above mentioned page - how do I add loader to get templates to display?
Thank you in advance
More Info
Using Slim Framework but I do not think it is an issue
having these lines
$loader = new Twig_Loader_String();
dompdf works but just renders the page name
$loader = new Twig_Loader_Filesystem('/home/sites/******/public_html/templates');
I then get
Fatal error: Call to undefined method DOMText::getAttribute() in /home/sites/******/app/vendor/dompdf/dompdf/include/cellmap.cls.php on line 437
confused - using slim and twig why can dompdf not accept the html as the other page here suggests?