How can I add a custom twig function to Craft CMS version 3?
Twig outlines how to add a function but not sure how to apply to Craft.
https://twig.symfony.com/doc/2.x/advanced.html#functions
$twig = new Twig_Environment($loader);
$function = new Twig_Function('function_name', function () {
// ...
});
$twig->addFunction($function);
Related
Currently using the SlimPHP 3 MVC framework and trying to XML output of a URL to the Twig template. I'm using the latest build of Guzzle if it helps.
I have my Controller set up and the function as follows:
public function getaws($request, $response, $args) {
$bucketname = $args['bucketname'];
$client = new \GuzzleHttp\Client();
$bucket = $client->request('GET', $bucketname . '.s3.amazonaws.com');
if($bucket) {
$buck = $bucket->getBody();
return $this->view->render($response, 'aws.twig', ['buck' => $buck]);
}
else {
$res = Array();
$res['message'] = 'Bucket Disabled or Unknown';
return $this->view->render($response, 'aws.twig', ['buck' => $res]);
}
}
In the Twig file - aws.twig I have only been able to output the entire XML response/body using:
{{ buck }}
I have tried typing:
{{ buck.Name }}
or
{{ buck.Key }}
Neither have worked. I've tried the ForEach loop as well and nothing comes from that at all. I'm completely at a loss. I've noticed that there doesn't seem to be an awful lot of help on SlimPHP but I like it and prefer using it to Laravel and no I won't switch to Laravel just to make life easier otherwise asking for help with this would be a waste of time.
Anyone who has had experience with SlimPHP 3, Twig templates and Guzzle - I'd very much like help with this.
Thanks!
I am trying not to have the column description for field type "URL" in SharePoint 2016. I want to use this OOTB column URL, but not get "Type the description". The only work around i saw is using JavaScript. I wanted to know, is there any other way remove it?
Programmatically, or some property in schema.xml?
Any suggestions and help is appreciated.
Thanks,
Menon
Sample JSlink script for you.
You could upload jQuery library and the custom jslink library to SharePoint library, I upload to layouts folder just for easy testing.
script:
(function () {
var JSHyperlinkFieldCtx = {};
JSHyperlinkFieldCtx.Templates = {};
JSHyperlinkFieldCtx.Templates.Fields = {
"JSHyperlink": {
"NewForm": HideJSHyperlinkTemplate
}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(JSHyperlinkFieldCtx);
})();
function HideJSHyperlinkTemplate(ctx) {
var result = SPFieldUrl_Edit(ctx);
var $f = $(result);
$f.find('span:eq(1)').css('display', 'none');
$f.find('input:eq(1)').css('display', 'none');
return $f.html();
}
use the script:
~sitecollection/_layouts/15/jslinks/jquery-1.12.4.js|~sitecollection/_layouts/15/jslinks/HyperlinkField.js
Is it possible to select different template (i.e. twig template) for same content type?
An Example:
3 blog posts, each displayed using different template...?
Template selected from a dropdown list during blog post creation?
use mixin
mixin different_blog_view_type(type)
....
if type !== "view1"
.supacoolEl23
.....
if type === "view2"
#moreCoolest2view
Don't forget to define locals.type variable, and change it your preferred way (dropdown,radio, so on)
exports = module.exports = function(req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
locals.type = req.params.type;
//this wil get from :type param
//or you can use req.body or req.query
previously i made a navigation bar in a view page for which i wrote the function in corresponding controller but now i have to put this in main file (layout). But now i don't know where to write the function.
also i have to pass two variable to the layout file through the function that will contain the value of navigation bar.
I have already tried some method but it allows me to return only on value.
Basically i want to know that where can i write the below function to use it in main layout of yii2 basic
public function actionMenutest()
{
$query = new Query;
$data= $query->select('name,id')
->from('menu')->all();
$query2 = new Query;
$data2= $query2->select('name,menu_id')
->from('submenu')->all();
return $this->render('menutest',[
'data'=>$data, 'data2'=>$data2
]);
}
You can use EVENT_BEFORE_RENDER for this purpose. For advanced app the below code need to go into common\config\bootstrap.php file.
use yii\base\Event;
use yii\base\View;
Event::on(View::className(), View::EVENT_BEFORE_RENDER, function() {
$query = new Query;
$data= $query->select('name,id')
->from('menu')->all();
$query2 = new Query;
$data2= $query2->select('name,menu_id')
->from('submenu')->all();
Yii::$app->view->params['data'] = $data;
Yii::$app->view->params['data2'] = $data2;
});
Then in your main layout you can use your model as:
$data= $this->params['data'];
$data2= $this->params['data2'];
I have not used basic template as yet. But you can try the following:
Create a bootstrap.php file in config folder.
After that update the web/index.php file. Put the below code in that:
require(__DIR__ . '/../config/bootstrap.php');
Then put the above code in bootstrap.php file. Try it and let me know if you need any more help.
Background
I got a page where I’m showing two list views from two separate lists which both have Custom List as their ListTemplate. They got their separate jslink file cause I don’t want them to look alike.
Problem
The js link file targets both listviews since they use the same Template.
Code
(function () {
var listContext = {};
listContext.Templates = {};
listContext.ListTemplateType = 100;
listContext.Templates.Header = "<div><ul>";
listContext.Templates.Footer = "</ul></div>";
listContext.Templates.Item = LinkTemplate;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(listContext);
})();
Question
Is there any way to make the js only target a specific list?
Ended up going with Paul Hunts solution that he writes about on myfatblog.co.uk. http://www.myfatblog.co.uk/index.php/2013/09/listview-web-part-issues-with-jslink-and-display-templates-a-solution/
The script ended up looking like this and I pasted it into the jslink function where I define what listContext to override.
// Override the RenderListView once the ClientTemplates.JS has been called
ExecuteOrDelayUntilScriptLoaded(function(){
// Copy and override the existing RenderListView
var oldRenderListView = RenderListView;
RenderListView = function(ctx,webPartID)
{
// Check the title and set the BaseViewId
if (ctx.ListTitle == "List")
ctx.BaseViewID = "list";
//now call the original RenderListView
oldRenderListView(ctx,webPartID);
}
},"ClientTemplates.js");