Bolt CMS access Bolt\Twig\TwigExtension from twig file - twig

I try to use localdatetime in my template twig file, but I get an error that localdatetime does not exists:
Unknown "localizeddate" filter. Did you mean "localdate" in "listing.twig" at line 51?
However when I look into the code, the TwigExtension class is already there and defines the localdatetime Twig_SimpleFilter too.
So I cannot see why I cannot use them in the code. Do someone know whats going on?
This is my twig template code:
{{ record.datepublish | localizeddate('full', 'none', app.request.locale ) }}

Use localedatetime instead of localizeddate or localdatetime.

Related

Call a service from twig template in symfony 4

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.

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

Passing variable from jade to ng-init not working

I'm trying to pass an object from jade to ng-init in angular
This: doesn't work:
ng-init='tables=!{JSON.stringify(tables)}'
This: expands but,
ng-init='tables=#{JSON.stringify(tables)}'
the output is unescaped and filled with "s
ng-init="tables={"12":{"id":....
and the view isn't updated in either of the cases. This article implies that first one should work, but like I said, it doesn't even expand,
ng-init='tables=!{JSON.stringify(tables)}'
in source code shows up exactly the same in the HTML source
ng-init='tables=!{JSON.stringify(tables)}'
Actually, the #{...} approach seems to work fine.
It is probably the way console.log prints attributes' values that confused you.
ng-init="tables=#{JSON.stringify(tables)}"
Take a look at this short demo.
In what use-case you want to pass data directly from Jade to angular? I think you could to this job in controller like this :
$scope.init = function () {
// init stuff
}
...and in your view :
ng-init = init()

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