twig uses ~ for its concatenation, is there any way to change this to another symbol?
I know there is a way to change other delimiters, like, the blocks, comments, etc, but I didn't find anything on concatenation. So if someone knows, that would be great!
For the delimeters your talking about, the TwigLexer only define this symbols:
$this->options = array_merge(array(
'tag_comment' => array('{#', '#}'),
'tag_block' => array('{%', '%}'),
'tag_variable' => array('{{', '}}'),
'whitespace_trim' => '-',
'interpolation' => array('#{', '}'),
), $options);
As #DarkBee mentioned, you could define your own operator using https://twig.symfony.com/doc/2.x/advanced.html#operators
You can found already defined operators on this php class to help you define your own:
vendor/twig/twig/lib/Twig/Extension/Core.php class Twig_Extension_Core::getOperators
Your operator would be
class Project_Twig_Extension extends Twig_Extension
{
public function getOperators()
{
return array(
array(),
array(
'~' => array('precedence' => 40, 'class' => 'Twig_Node_Expression_Binary_Concat', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
),
);
}
// ...
}
Related
$output = $modx->runSnippet('getImageList',array(
'tvname' => 'workOrders',
'where' => $_GET['search'] ,
'tpl' => 'workOrdersList',
'docid' => 3
));
One of the fields is a string with parameters. How can I check whether my search string is a part of that field? I have looked up how to use "where" parameter to accomplish this task but I am still stuck.
If you just use this extra (getUrlParam), you can call this instead of refering to the GET directly:
So your call could look like this:
$output = $modx->runSnippet('getImageList', array(
'tvname' => 'workOrders',
'where' => $modx->runSnippet('getUrlParam', array('name' => 'search`)),
'tpl' => 'workOrdersList',
'docid' => 3
));
This also takes care of malicious url parameters.
Where needs to be formatted as a JSON value, so you would need to define which field you are querying against and format as JSON. E.g. 'where' => $modx->toJSON(array('pagetitle'=>$_GET['search']))
I'm trying to do something simple. I think my code looks sound, but for some reason, the $wpdb->insert_id; keeps being empty? Am I doing something wrong here? Am I doing the passing of the variable correctly? I even tried storing $wpdb->insert_id; in a $_SESSION but it was still empty.
function insert_stuff() {
global $wpdb;
$wpdb->insert('mytable',
array(
'column1' => $_REQUEST['formitem1'],
'column2' => $_REQUEST['formitem2'],
)
);
global $lastid;
$lastid = $wpdb->insert_id;
}
add_action('add_to_cart', 'insert_stuff');
function update_stuff() {
global $wpdb;
global $lastid;
$wpdb->update('mytable', array('column3' => 'newvalue'), array('id' => $lastid), array('%s'), array('%d'));
}
add_action('thank_you_page', 'update_stuff');
Looks like you are missing an array with the $wpdb->insert..
$wpdb->insert(
'table',
array(
'column1' => $var1,
'column2' => $var2,
),
array(
'%d',
'%s',
)
);
Note: %d = numbers.. %s = string..
Best of luck.
In my CakePHP app when I try to get data like this:
$this->loadModel('Radio');
$posts = $this->Radio->find('all');
the integers are displayed like strings (in debug) :
'Radio' => array(
'idSong' => '4',
'name' => 'batman',
'title' => 'Batman Theme Song'
),
why? the type is int in the DB. I need integers correctly displayed in my JSON files
Not sure if there's a straightforward solution, but you could change the model data using afterfind
Something like
public function afterFind($results, $primary = false) {
foreach ($results as $key => $val) {
if (isset($val['Radio']['idSong'])) {
$results[$key]['Radio']['idSong'] = (int)$results[$key]['Radio']['idSong'];
}
}
return $results;
}
I am using perl script to update sections in conf file . my script i working properly but when i execute script the ordered on sections changed automatically while i dont want to change the order.script is following .
#!/usr/bin/perl
use Config::Tiny;
$file = 'sss.conf';
my $Config = Config::Tiny->new;
$Config = Config::Tiny->read( $file );
my $rootproperty = $Config->{_}->{rootproperty};
$Config->{amrit}->{host} = 'Not Bar!';
$Config->write( $file );
and file contents following line;
[amrit]
type=friend
host=111.118.253.145
port=2776
username=amrit
secret=password
disallow=all
allow=gsm
context=sip-calling
qualify=yes
call-limit=22
Please help me here i dont want to change order of fields
From the documentation for Config::Tiny:
...Config::Tiny does not preserve your comments, whitespace, or the order of your config file.
See Config::Tiny::Ordered (and possibly others) for the preservation of the order of the entries in the file.
So, do as the documentation suggests, and see Config::Tiny::Ordered.
Update: Based on the comments I'll try to provide additional help here.
First, your existing script is mostly just a copy-paste from the Config::Tiny synopsis, without any deep understanding of what most of it does. The relevant parts... or at least those parts you should keep are:
use Config::Tiny;
$file = 'sss.conf';
$Config = Config::Tiny->read( $file );
$Config->{amrit}->{host} = 'Not Bar!';
$Config->write( $file );
If you add use Data::Dumper; at the top of the script, and immediately after reading the config file you add print Dumper $Config;, the structure would look like this:
{
'amrit' => {
'call-limit' => '22',
'host' => '111.118.253.145',
'secret' => 'password',
'context' => 'sip-calling',
'port' => '2776',
'username' => 'amrit',
'allow' => 'gsm',
'qualify' => 'yes',
'type' => 'friend',
'disallow' => 'all'
}
}
Modifying the structure with the script I've posted above would work if you don't mind the key/value pairs to have their orders rearranged. But you need to preserve order. So the suggestion was to switch to Config::Tiny::Ordered. That module preserves order by rearranging the structure differently. If you change the script to look like this:
use Data::Dumper;
use Config::Tiny::Ordered;
$file = 'conf.conf';
$Config = Config::Tiny->read( $file );
print Dumper $Config;
You will see that the structure now looks like this:
{
'amrit' =>
[
{
'value' => 'friend',
'key' => 'type'
},
{
'key' => 'host',
'value' => '111.118.253.145'
},
{
'value' => '2776',
'key' => 'port'
},
{
'value' => 'amrit',
'key' => 'username'
},
{
'value' => 'password',
'key' => 'secret'
},
{
'value' => 'all',
'key' => 'disallow'
},
{
'key' => 'allow',
'value' => 'gsm'
},
{
'key' => 'context',
'value' => 'sip-calling'
},
{
'key' => 'qualify',
'value' => 'yes'
},
{
'value' => '22',
'key' => 'call-limit'
}
]
}
Or in other words, the internal structure of the Config::Tiny::* object has changed from a hash of hashes, to a hash of arrays of hashes. ("All problems in computer science can be solved by another level of indirection" -- David Wheeler) This change in the shape of the datastructure exists to move away from the problem of hashes being unordered containers.
So now instead of convenient hash lookups for the key named "host", you have to iterate through your structure to find the array element that has an anonymous hash with a key field named host. More work:
use List::Util qw(first);
use Config::Tiny::Ordered;
$file = 'sss.conf';
$Config = Config::Tiny::Ordered->read( $file );
my $want_ix = first {
$Config->{amrit}[$_]{key} eq 'host'
} 0 .. $#{$Config->{amrit}};
$Config->{amrit}[$want_ix]{value} = 'Not Bar!';
$Config->write( $file );
This works by running through the amrit section of the config file looking for the element in the structure's anonymous array that has an anonymous hash with a field named 'key', and once that array element is found, modifying the 'value' hash element to 'Not Bar!'
If this is a one time thing for you, you're done. If you want to be able to do it yourself next time, read perldoc perlreftut, perldoc perldsc, as well as documentation for any of the functions used herein that aren't immediately clear.
From the CPAN page:
Lastly, Config::Tiny does not preserve your comments, whitespace, or the order of your config file.
Use COnfig::Tiny::Ordered instead
See Config::Tiny::Ordered (and possibly others) for the preservation of the order of the entries in the file.
I have a problem with searching in Yii. I have two models: Teams and Workers. On website there is a page called 'Team Workers' where I want to display CGridView widget with searching that displays Workers from the team (team id is passed as a _GET parameter).
I did this in TeamsController:
public function actionWorkers($id)
{
$model = Teams::model()->findByPk($id);
$workers = Workers::model();
$workers->unsetAttributes();
if(isset($_GET['Workers']))
{
$_GET['Workers']['idTeam'] = $id;
$workers->attributes = $_GET['Workers'];
}
else {
$workers->attributes = array('idTeam' => $id);
}
$teamWorkers = $workers;
$this->render('workers', array(
'model' => $model,
'teamWorkers' => $teamWorkers
));
}
And in the view file:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'team-workers-grid',
'dataProvider'=>$teamWorkers->search(),
'filter' => $teamWorkers,
'columns'=>array(
'name',
'surname',
array(
'id' => 'idWorker',
'class' => 'CCheckBoxColumn',
'checked' => '$data->confirmer',
'selectableRows' => '2',
// 'headerTemplate' => '{item}'
)
),
)); ?>
I got the error:
CDbCommand nie zdołał wykonać instrukcji SQL: SQLSTATE[23000]: Integrity constraint
violation: 1052 Column 'idTeam' in where clause is ambiguous. The SQL statement
executed was: SELECT COUNT(DISTINCT `t`.`idWorker`) FROM `workers` `t` LEFT OUTER JOIN
`teams` `Team` ON (`t`.`idTeam`=`Team`.`idTeam`) WHERE ((idTeam=:ycp0) AND (Team.name
LIKE :ycp1))
When I dont set idTeam attribute - it works fine. It's pretty weird - at the regular CRUD admin page - idTeam attribute is passed and that works fine.
Hot to deal with it?
In Workers::search() you have something like
$criteria->compare('idTeam',$this->idTeam);
Change it to
$criteria->compare('t.idTeam',$this->idTeam);
i.e prefix sql attribute with t. if it is from current model or with relation name if from other table/model
Also instead of:
$workers->attributes = array('idTeam' => $id);
yould could keep it simpler with:
$workers->idTeam = $id;
You have defined the column idTeam in Team and Workers. By joining those tables you would have a duplicate ("ambiguous") column in the result. That's what the error message tells you.
To solve this you have to use an alias for one of the columns.