Fuelphp - Use dump() in twig template - twig

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.

Related

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

Kohana 3 experts, handling request arguments Route::set(), request->params() in K3 v. >= 3.1

Originally in Kohana 3 you were able to pass controller action arguments/parameters through URL as in:
http:/website/controller/actionname/param1/param2/.../paramX
and handle it by simply defining the action as in:
public action_actionname($params)
{
$params_array = explode("/", $params);
//you can now use $params_array[0], $params_array[1], ...
}
Now it seems that since v3.1 they decided to deprecate this feature (here is the link) and it should be eliminated in v3.2
And it seems they want you to use Route::Set() and request->param() methods instead. Does this mean that every time you define a method/action in a controller, you have to define a separate routing for each argument someplace else in your application?
Can anyone please explain to me how this works in simple terms. It just seems like a lot of unnecessary "hassle" to do all of that to simply call a function.
Maybe you should consider using the regex param in your route to override the default matching behavior... I typically use this to capture multiple URL parameters in one KO3 "param". Example:
Route::set('route1', '<controller>/<action>(/<param_list>)', array('param_list'=>'.*'))
->defaults(array(
'controller' => 'my_default_controller',
'action' => 'my_default_index'
));
Now in your controller, $this->request->param("param_list") will equal whatever matches the regex expression .* which means you can capture the rest of the URL just like you were hoping, with slashes and all!

Question from Pro Drupal 7 Book: Annotate module

So this is likely a shot in the dark, but for anyone out there who knows a bit of Drupal and better yet has implemented the annotate module from Ch. 2 of Pro Drupal 7 Development
Do you know how to alter the annotate module so all users can annotate? Right now, only the admin can annotate and it's being presented as an extension to editing.
The specific code being used is in this repository (pretty much straight from the book): http://github.com/dsharkey/Drupal-Module-Development--Annotate-Module
Further, I'm not really seeing how the annotate module is told to be presented at all? I believe its by the following lines of PHP (from annotate.admin.inc):
$instance = array(
'field_name' => 'annotation',
'entity_type' => 'node',
'bundle' => $key,
'label' => t('Annotation'),
'widget_type' => 'text_textarea_with_summary',
'settings' => array('display_summary' => TRUE),
'display' => array(
'default' => array(
'type' => 'text_default',
),
'teaser' => array(
'type' => 'text_summary_or_trimmed',
),
),
);
$instance = field_create_instance($instance);
But I'm not sure how that does anything more than create an instance and attach itself to a node. Why does it display where it does (as an option next to edit)?
Thanks all!
I'm not sure what you're referring to when you say "as an option next to edit", but the code you uploaded (and double-checking the book itself, the code used) wouldn't cause that. In fact, you should just see a field below the body field when you edit a node with annotations enabled:
The reason it only shows up when you edit an existing node (and not when you create a new node) is related to your first question about it not letting all users annotate the node: in the hook_node_load() implementation, it specifically checks to see if the user editing the node is the same as the owner of the node; if it isn't, it hides the annotation field:
/**
* Implements hook_node_load()
*/
function annotate_node_load($nodes, $types) {
global $user;
// Check to see if the person viewing the node is the author. If not then
// hide the annotation.
foreach ($nodes as $node) {
if ($user->uid != $node->uid) {
unset($node->annotation);
}
}
}
So the only person who should ever see the annotate field is the owner. If you want to allow anyone with edit access annotate the node, remove that function.
As for allowing anyone to make annotations to the node as a separate function to editing the node itself, that's not what the example was about and is entirely separate from the code used. You'll have to seek elsewhere for that and look at examples like the Drupal.org project Annotate for ways to do it. Basically, the annotations would be their own separate entities that would reference the node, much in the same way comments work.
But if I may be so bold, you've run into a big problem with Pro Drupal 7 Development in that it's not as good a reference for development as the previous editions were: it doesn't explain things very well, spends too much time on minor things and not enough time on really major things, introduces really bad practices (including several in the annotate example), and completely misses large sections of what Drupal 7 introduced. I'd recommend checking out Drupal 7 Module Development instead.

How not to repeat myself in Kohana 3's routes?

Let's say I want to make a system which can afford a multilingual web project. The system will consist of the modules that are put in Kohana's standard directory modules. Let's say that the standard access to the particular language can be done via lang parameter (i.e. somesite.com/en/somepage). The problem is that I have to repeat myself in defining my modules routes prepending each uri with (<lang>). Is there any way to avoid that? I thought about a separate language route declaration (for example in bootstap.php file), but I guess it won't solve the problem.
It's all about Kohana 3. Thanks to all.
UPDATE:
I think that the way suggested by The Pixel Developer is what one need if some part of the rule in route repeats everywhere.
Move up a level and extend the route class.
http://github.com/kohana/core/blob/master/classes/kohana/route.php#L69
public static function set($name, $uri, array $regex = NULL)
{
return parent::set($name, '(<lang>)'.$uri, $regex);
}
Not tested, but that's the general idea.
If lang is required in route, why don't you just put it in the default route? Surely that's the easiest way to go about. Something like:
Route::set('default', '<lang>(<controller>(/<action>(/<id>)))', array('lang'=> '[a-z]{2}'))
->defaults(array(
'controller' => 'somepage',
'action' => 'index',
'lang' => 'en',
));
Where lang is any 2 letters alphabets which defaults to 'en'.

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