how do I use loader for twig templates using dompdf - twig

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?

Related

Rendering Twig template returns "cache is corrupted" and outputs PHP code

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.

Twig can't find extension... what namespace should I use?

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

What's the path for twig extends?

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

Declaring function in Blocks in Drupal 6

I am facing an issue in declaring a fucation in block which I have added . I am calling a function by including an file which ia placed in the theme. I have also tried it by placing out of the theme folder. The function is alredy being user in front page. But when I am using the same function in that block. The screen gets blank and nothing displays. some part of my block coding is written below. Please help me.
<?php
global $base_url;
include($_SERVER['DOCUMENT_ROOT']."/travellar/geoiploc.php"); // indluded file
$ip = "203.189.25.0"; // Australia IP test
$country_code = getCountryFromIP($ip, "code");
I've had no problems loading functions from modules into custom blocks, but I've never tried loading one from a theme before. It's not clear to me whether or not theme functions are loaded before the page content is loaded.
You might have to create a custom module or include file to hold the function. Check out the module_load_include() function for how to load a specific include file.
A custom module would be a good approach as it is loaded before the theme layer and can be accessed from almost anywhere in Drupal except other modules with a lower weight than the custom module. It is also likely to come in handy for hooks and other overrides.
However, if you must have it in the theme layer, another option is adding it to template.php of your theme which should make it available within page.tpl.php and such, but not blocks I don't believe.
/sites/all/modules/mymodule/mymodule.info
name = My Module
package = !
description = It is MY module, not yours!
core = 6.x
The package "!" will make this module appear at the top of the modules page
/sites/all/modules/mymodule/mymodule.module
<?php
// Load mymodule.morePHP.inc
module_load_include('inc', 'mymodule', 'mymodule.morePHP');
// A custom function
function mymodule_my_custom_function($args) {
/* do custom stuff here */
return 'output';
}
/sites/all/modules/mymodule/mymodule.morePHP.inc
<?php
// An included custom function
function mymodule_other_custom_stuff() {
}

Kohana 3 - Can I change a configuration file's settings at runtime?

NOTE: This question has been asked on the kohana forums at: http://forum.kohanaframework.org/comments.php?DiscussionID=6451
Hey everyone!
I am attempting to use HTML Purifier - I have it installed and working correctly. I have two helper functions, clean_all and clean_whitelist.
/config/purifier.php
<?php defined('SYSPATH') or die('No direct access allowed.'); return array( 'settings' => array( 'HTML.Allowed' =>'b,i,p,ul,ol,li' ), ); ?>
Clean_whitelist -
public static function clean_whitelist($dirty_data) { //Whitelist is defined at APPPATH/config/purifier.php return Security::xss_clean($dirty_data); }
This works as intended, as I have setup the htmlpurifier config file with the HTML.Allowed directive configured for my needs.
Clean_all should work similarly, except I want my configuration to set the HTML.Allowed to none.
QUESTION: Is there a way for me to change the configuration file at runtime?
Thanks, all!
I'm the guy who answered you on the message board (Colonel-Rosa).
Straightforward
$config->set($key, $new_value);
OR ...
Pass the config data as an argument or store it as a class member then merge this data with the config file data.

Resources