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);
Related
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 assumed this would be easy but I am stumped.
I have a custom content type that includes an id field. I render these with a basic page template.
I have written a small module that creates a block which is populated with data from an external API. Everything works except I cannot seem to figure out how to pass the value of the id from the content of a given page to my module so it can make the API call.
It would be a couple of lines of code in straight php, it can't be that complicated in Drupal 8 and twig can it?
I managed to find a solution here
I am re-posting it in case it is useful to anyone else.
If you are generating a custom block you can access content fields via the routing system inside your block build function like this:
public function build() {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$field_my_custom_value = $node->field_my_custom_value->value;
}
//do something with the variable, like make the API call
//Make sure to set the cache to the context or even to zero if you need
return array(
'#markup' => $this->t('my content to render'),
'#cache' => array(
'contexts' => ['contexts' => ['route']],
),
);
}
I think you can reach what you want with a HOOK_preprocess.
use:
YOUR_MODULE_preprocess_node(&$variables){ ... } or
YOUR_MODULE_preprocess_block(&$variables){ ... }
to access your variable from the content type and pass it to your function oder template.
I'm trying to activate twig's useful "dump()" method but can't seem to find a way to do it in fuelphp.
Anybody knows how ?
Its very easy to do,
Just add twig as dependency requirement in your composer like
"twig/twig": "1.14.1"
and later run
php composer.phar update
and after that
open your config.php file in config/config.php and uncomment always_load array and also uncomment package array with always_load array . Your final always_load array should look somewhat like this
'always_load' => array(
'packages' => array(
'parser',
),
),
Thats all twig is activated, you can use methods with in your controller and actions.
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?
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.